Update Spring Batch to upstream API changes

Fix Spring Batch tests following upstream changes related to Spring
Batch issue 4130.

Closes gh-32237
pull/32224/head
Phillip Webb 2 years ago
parent 127d320636
commit 6e239d551a

@ -98,9 +98,11 @@ class JobLauncherApplicationRunnerTests {
@Test @Test
void retryFailedExecution() { void retryFailedExecution() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context); JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context);
Job job = jobLauncherContext.jobBuilder() Job job = jobLauncherContext.jobBuilder()
.start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build()) .start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet())
.transactionManager(transactionManager).build())
.incrementer(new RunIdIncrementer()).build(); .incrementer(new RunIdIncrementer()).build();
jobLauncherContext.runner.execute(job, new JobParameters()); jobLauncherContext.runner.execute(job, new JobParameters());
jobLauncherContext.runner.execute(job, new JobParametersBuilder().addLong("run.id", 1L).toJobParameters()); jobLauncherContext.runner.execute(job, new JobParametersBuilder().addLong("run.id", 1L).toJobParameters());
@ -111,9 +113,10 @@ class JobLauncherApplicationRunnerTests {
@Test @Test
void runDifferentInstances() { void runDifferentInstances() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context); JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context);
Job job = jobLauncherContext.jobBuilder() Job job = jobLauncherContext.jobBuilder().start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet())
.start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build()).build(); .transactionManager(transactionManager).build()).build();
// start a job instance // start a job instance
JobParameters jobParameters = new JobParametersBuilder().addString("name", "foo").toJobParameters(); JobParameters jobParameters = new JobParametersBuilder().addString("name", "foo").toJobParameters();
jobLauncherContext.runner.execute(job, jobParameters); jobLauncherContext.runner.execute(job, jobParameters);
@ -128,9 +131,11 @@ class JobLauncherApplicationRunnerTests {
@Test @Test
void retryFailedExecutionOnNonRestartableJob() { void retryFailedExecutionOnNonRestartableJob() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context); JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context);
Job job = jobLauncherContext.jobBuilder().preventRestart() Job job = jobLauncherContext.jobBuilder().preventRestart()
.start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build()) .start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet())
.transactionManager(transactionManager).build())
.incrementer(new RunIdIncrementer()).build(); .incrementer(new RunIdIncrementer()).build();
jobLauncherContext.runner.execute(job, new JobParameters()); jobLauncherContext.runner.execute(job, new JobParameters());
jobLauncherContext.runner.execute(job, new JobParameters()); jobLauncherContext.runner.execute(job, new JobParameters());
@ -149,9 +154,11 @@ class JobLauncherApplicationRunnerTests {
@Test @Test
void retryFailedExecutionWithNonIdentifyingParameters() { void retryFailedExecutionWithNonIdentifyingParameters() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context); JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context);
Job job = jobLauncherContext.jobBuilder() Job job = jobLauncherContext.jobBuilder()
.start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build()) .start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet())
.transactionManager(transactionManager).build())
.incrementer(new RunIdIncrementer()).build(); .incrementer(new RunIdIncrementer()).build();
JobParameters jobParameters = new JobParametersBuilder().addLong("id", 1L, false).addLong("foo", 2L, false) JobParameters jobParameters = new JobParametersBuilder().addLong("id", 1L, false).addLong("foo", 2L, false)
.toJobParameters(); .toJobParameters();
@ -187,9 +194,11 @@ class JobLauncherApplicationRunnerTests {
JobLauncherApplicationRunnerContext(ApplicationContext context) { JobLauncherApplicationRunnerContext(ApplicationContext context) {
JobLauncher jobLauncher = context.getBean(JobLauncher.class); JobLauncher jobLauncher = context.getBean(JobLauncher.class);
JobRepository jobRepository = context.getBean(JobRepository.class); JobRepository jobRepository = context.getBean(JobRepository.class);
PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
this.jobs = new JobBuilderFactory(jobRepository); this.jobs = new JobBuilderFactory(jobRepository);
this.steps = new StepBuilderFactory(jobRepository, context.getBean(PlatformTransactionManager.class)); this.steps = new StepBuilderFactory(jobRepository);
this.step = this.steps.get("step").tasklet((contribution, chunkContext) -> null).build(); this.step = this.steps.get("step").tasklet((contribution, chunkContext) -> null)
.transactionManager(transactionManager).build();
this.job = this.jobs.get("job").start(this.step).build(); this.job = this.jobs.get("job").start(this.step).build();
this.jobExplorer = context.getBean(JobExplorer.class); this.jobExplorer = context.getBean(JobExplorer.class);
this.runner = new JobLauncherApplicationRunner(jobLauncher, this.jobExplorer, jobRepository); this.runner = new JobLauncherApplicationRunner(jobLauncher, this.jobExplorer, jobRepository);

@ -18,38 +18,36 @@ package smoketest.batch;
import org.springframework.batch.core.Job; import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step; 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.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 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.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus; import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.transaction.PlatformTransactionManager;
@SpringBootApplication @SpringBootApplication
@EnableBatchProcessing @EnableBatchProcessing
public class SampleBatchApplication { public class SampleBatchApplication {
@Autowired
private JobBuilderFactory jobs; private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps; private StepBuilderFactory steps;
@Bean private PlatformTransactionManager transactionManager;
protected Tasklet tasklet() {
return new Tasklet() { public SampleBatchApplication(JobBuilderFactory jobs, StepBuilderFactory steps,
@Override PlatformTransactionManager transactionManager) {
public RepeatStatus execute(StepContribution contribution, ChunkContext context) { this.jobs = jobs;
return RepeatStatus.FINISHED; this.steps = steps;
} this.transactionManager = transactionManager;
}; }
@Bean
protected Tasklet tasklet() {
return (contribution, context) -> RepeatStatus.FINISHED;
} }
@Bean @Bean
@ -59,7 +57,7 @@ public class SampleBatchApplication {
@Bean @Bean
protected Step step1() { 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) { public static void main(String[] args) {

Loading…
Cancel
Save