From 6e239d551a9cdafdee936a66d5f5d5bd1649c2cb Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 8 Sep 2022 18:21:56 -0700 Subject: [PATCH] Update Spring Batch to upstream API changes Fix Spring Batch tests following upstream changes related to Spring Batch issue 4130. Closes gh-32237 --- .../JobLauncherApplicationRunnerTests.java | 23 +++++++++++----- .../batch/SampleBatchApplication.java | 26 +++++++++---------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java index b16e636115..e73de72f88 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java @@ -98,9 +98,11 @@ class JobLauncherApplicationRunnerTests { @Test void retryFailedExecution() { this.contextRunner.run((context) -> { + PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class); JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context); Job job = jobLauncherContext.jobBuilder() - .start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build()) + .start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()) + .transactionManager(transactionManager).build()) .incrementer(new RunIdIncrementer()).build(); jobLauncherContext.runner.execute(job, new JobParameters()); jobLauncherContext.runner.execute(job, new JobParametersBuilder().addLong("run.id", 1L).toJobParameters()); @@ -111,9 +113,10 @@ class JobLauncherApplicationRunnerTests { @Test void runDifferentInstances() { this.contextRunner.run((context) -> { + PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class); JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context); - Job job = jobLauncherContext.jobBuilder() - .start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build()).build(); + Job job = jobLauncherContext.jobBuilder().start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()) + .transactionManager(transactionManager).build()).build(); // start a job instance JobParameters jobParameters = new JobParametersBuilder().addString("name", "foo").toJobParameters(); jobLauncherContext.runner.execute(job, jobParameters); @@ -128,9 +131,11 @@ class JobLauncherApplicationRunnerTests { @Test void retryFailedExecutionOnNonRestartableJob() { this.contextRunner.run((context) -> { + PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class); JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context); Job job = jobLauncherContext.jobBuilder().preventRestart() - .start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build()) + .start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()) + .transactionManager(transactionManager).build()) .incrementer(new RunIdIncrementer()).build(); jobLauncherContext.runner.execute(job, new JobParameters()); jobLauncherContext.runner.execute(job, new JobParameters()); @@ -149,9 +154,11 @@ class JobLauncherApplicationRunnerTests { @Test void retryFailedExecutionWithNonIdentifyingParameters() { this.contextRunner.run((context) -> { + PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class); JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context); Job job = jobLauncherContext.jobBuilder() - .start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build()) + .start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()) + .transactionManager(transactionManager).build()) .incrementer(new RunIdIncrementer()).build(); JobParameters jobParameters = new JobParametersBuilder().addLong("id", 1L, false).addLong("foo", 2L, false) .toJobParameters(); @@ -187,9 +194,11 @@ class JobLauncherApplicationRunnerTests { JobLauncherApplicationRunnerContext(ApplicationContext context) { JobLauncher jobLauncher = context.getBean(JobLauncher.class); JobRepository jobRepository = context.getBean(JobRepository.class); + PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class); this.jobs = new JobBuilderFactory(jobRepository); - this.steps = new StepBuilderFactory(jobRepository, context.getBean(PlatformTransactionManager.class)); - this.step = this.steps.get("step").tasklet((contribution, chunkContext) -> null).build(); + this.steps = new StepBuilderFactory(jobRepository); + this.step = this.steps.get("step").tasklet((contribution, chunkContext) -> null) + .transactionManager(transactionManager).build(); this.job = this.jobs.get("job").start(this.step).build(); this.jobExplorer = context.getBean(JobExplorer.class); this.runner = new JobLauncherApplicationRunner(jobLauncher, this.jobExplorer, jobRepository); diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-batch/src/main/java/smoketest/batch/SampleBatchApplication.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-batch/src/main/java/smoketest/batch/SampleBatchApplication.java index 9e6507cd46..a0c62f3be4 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-batch/src/main/java/smoketest/batch/SampleBatchApplication.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-batch/src/main/java/smoketest/batch/SampleBatchApplication.java @@ -18,38 +18,36 @@ package smoketest.batch; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; -import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; -import org.springframework.batch.core.scope.context.ChunkContext; import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.batch.repeat.RepeatStatus; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; +import org.springframework.transaction.PlatformTransactionManager; @SpringBootApplication @EnableBatchProcessing public class SampleBatchApplication { - @Autowired private JobBuilderFactory jobs; - @Autowired private StepBuilderFactory steps; - @Bean - protected Tasklet tasklet() { + private PlatformTransactionManager transactionManager; - return new Tasklet() { - @Override - public RepeatStatus execute(StepContribution contribution, ChunkContext context) { - return RepeatStatus.FINISHED; - } - }; + public SampleBatchApplication(JobBuilderFactory jobs, StepBuilderFactory steps, + PlatformTransactionManager transactionManager) { + this.jobs = jobs; + this.steps = steps; + this.transactionManager = transactionManager; + } + @Bean + protected Tasklet tasklet() { + return (contribution, context) -> RepeatStatus.FINISHED; } @Bean @@ -59,7 +57,7 @@ public class SampleBatchApplication { @Bean protected Step step1() { - return this.steps.get("step1").tasklet(tasklet()).build(); + return this.steps.get("step1").tasklet(tasklet()).transactionManager(this.transactionManager).build(); } public static void main(String[] args) {