Removed Job and Step Builder factories.
Now using builders directly
This commit is contained in:
@@ -20,9 +20,10 @@ import java.util.Map;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.core.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.step.builder.SimpleStepBuilder;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
@@ -49,25 +50,20 @@ import org.springframework.util.Assert;
|
||||
@AutoConfigureBefore(BatchAutoConfiguration.class)
|
||||
public class SingleStepJobAutoConfiguration {
|
||||
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
private SingleStepJobProperties properties;
|
||||
|
||||
@Autowired
|
||||
PlatformTransactionManager transactionManager;
|
||||
|
||||
@Autowired
|
||||
JobRepository jobRepository;
|
||||
|
||||
@Autowired(required = false)
|
||||
private ItemProcessor<Map<String, Object>, Map<String, Object>> itemProcessor;
|
||||
|
||||
public SingleStepJobAutoConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory,
|
||||
SingleStepJobProperties properties, ApplicationContext context) {
|
||||
public SingleStepJobAutoConfiguration(SingleStepJobProperties properties, ApplicationContext context) {
|
||||
|
||||
validateProperties(properties);
|
||||
|
||||
this.jobBuilderFactory = jobBuilderFactory;
|
||||
this.stepBuilderFactory = stepBuilderFactory;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@@ -83,15 +79,16 @@ public class SingleStepJobAutoConfiguration {
|
||||
@ConditionalOnProperty(prefix = "spring.batch.job", name = "job-name")
|
||||
public Job job(ItemReader<Map<String, Object>> itemReader, ItemWriter<Map<String, Object>> itemWriter) {
|
||||
|
||||
SimpleStepBuilder<Map<String, Object>, Map<String, Object>> stepBuilder = this.stepBuilderFactory
|
||||
.get(this.properties.getStepName())
|
||||
.<Map<String, Object>, Map<String, Object>>chunk(this.properties.getChunkSize()).reader(itemReader);
|
||||
SimpleStepBuilder<Map<String, Object>, Map<String, Object>> stepBuilder = new StepBuilder(
|
||||
this.properties.getStepName()).repository(this.jobRepository)
|
||||
.<Map<String, Object>, Map<String, Object>>chunk(this.properties.getChunkSize())
|
||||
.reader(itemReader);
|
||||
|
||||
stepBuilder.processor(this.itemProcessor);
|
||||
|
||||
Step step = stepBuilder.writer(itemWriter).transactionManager(this.transactionManager).build();
|
||||
|
||||
return this.jobBuilderFactory.get(this.properties.getJobName()).start(step).build();
|
||||
return new JobBuilder(this.properties.getJobName()).repository(this.jobRepository).start(step).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ public class SingleStepJobAutoConfigurationTests {
|
||||
SingleStepJobProperties properties = new SingleStepJobProperties();
|
||||
|
||||
try {
|
||||
new SingleStepJobAutoConfiguration(null, null, properties, null);
|
||||
new SingleStepJobAutoConfiguration(properties, null);
|
||||
}
|
||||
catch (IllegalArgumentException iae) {
|
||||
assertThat(iae.getMessage()).isEqualTo("A job name is required");
|
||||
@@ -66,7 +66,7 @@ public class SingleStepJobAutoConfigurationTests {
|
||||
properties.setJobName("job");
|
||||
|
||||
try {
|
||||
new SingleStepJobAutoConfiguration(null, null, properties, null);
|
||||
new SingleStepJobAutoConfiguration(properties, null);
|
||||
}
|
||||
catch (IllegalArgumentException iae) {
|
||||
assertThat(iae.getMessage()).isEqualTo("A step name is required");
|
||||
@@ -78,7 +78,7 @@ public class SingleStepJobAutoConfigurationTests {
|
||||
properties.setStepName("step");
|
||||
|
||||
try {
|
||||
new SingleStepJobAutoConfiguration(null, null, properties, null);
|
||||
new SingleStepJobAutoConfiguration(properties, null);
|
||||
}
|
||||
catch (IllegalArgumentException iae) {
|
||||
assertThat(iae.getMessage()).isEqualTo("A chunk size is required");
|
||||
@@ -90,7 +90,7 @@ public class SingleStepJobAutoConfigurationTests {
|
||||
properties.setChunkSize(-5);
|
||||
|
||||
try {
|
||||
new SingleStepJobAutoConfiguration(null, null, properties, null);
|
||||
new SingleStepJobAutoConfiguration(properties, null);
|
||||
}
|
||||
catch (IllegalArgumentException iae) {
|
||||
assertThat(iae.getMessage()).isEqualTo("A chunk size greater than zero is required");
|
||||
|
||||
@@ -33,17 +33,17 @@ import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
|
||||
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.explore.JobExplorer;
|
||||
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
|
||||
import org.springframework.batch.core.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.launch.support.RunIdIncrementer;
|
||||
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
|
||||
import org.springframework.batch.core.launch.support.TaskExecutorJobLauncher;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.JobRestartException;
|
||||
import org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer;
|
||||
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
@@ -86,10 +86,6 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
|
||||
|
||||
private TaskJobLauncherApplicationRunner runner;
|
||||
|
||||
private JobBuilderFactory jobs;
|
||||
|
||||
private StepBuilderFactory steps;
|
||||
|
||||
private Job job;
|
||||
|
||||
private Step step;
|
||||
@@ -97,11 +93,10 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
this.transactionManager = new ResourcelessTransactionManager();
|
||||
this.jobs = new JobBuilderFactory(this.jobRepository);
|
||||
this.steps = new StepBuilderFactory(this.jobRepository);
|
||||
Tasklet tasklet = (contribution, chunkContext) -> RepeatStatus.FINISHED;
|
||||
this.step = this.steps.get("step").tasklet(tasklet).transactionManager(this.transactionManager).build();
|
||||
this.job = this.jobs.get("job").start(this.step).build();
|
||||
this.step = new StepBuilder("step").repository(this.jobRepository).tasklet(tasklet)
|
||||
.transactionManager(this.transactionManager).build();
|
||||
this.job = new JobBuilder("job").repository(this.jobRepository).start(this.step).build();
|
||||
this.runner = new TaskJobLauncherApplicationRunner(this.jobLauncher, this.jobExplorer, this.jobRepository,
|
||||
new TaskBatchProperties());
|
||||
|
||||
@@ -119,7 +114,8 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
|
||||
@DirtiesContext
|
||||
// @Test
|
||||
public void incrementExistingExecution() throws Exception {
|
||||
this.job = this.jobs.get("job").start(this.step).incrementer(new RunIdIncrementer()).build();
|
||||
this.job = new JobBuilder("job").repository(this.jobRepository).start(this.step)
|
||||
.incrementer(new RunIdIncrementer()).build();
|
||||
this.runner.execute(this.job, new JobParameters());
|
||||
this.runner.execute(this.job, new JobParameters());
|
||||
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(2);
|
||||
@@ -128,7 +124,8 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
|
||||
@DirtiesContext
|
||||
// @Test
|
||||
public void retryFailedExecution() throws Exception {
|
||||
this.job = this.jobs.get("job").start(this.steps.get("step").tasklet(throwingTasklet()).build())
|
||||
this.job = new JobBuilder("job").repository(this.jobRepository)
|
||||
.start(new StepBuilder("step").repository(this.jobRepository).tasklet(throwingTasklet()).build())
|
||||
.incrementer(new RunIdIncrementer()).build();
|
||||
runFailedJob(new JobParameters());
|
||||
runFailedJob(new JobParametersBuilder().addLong("run.id", 1L).toJobParameters());
|
||||
@@ -138,8 +135,9 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
|
||||
@DirtiesContext
|
||||
@Test
|
||||
public void runDifferentInstances() throws Exception {
|
||||
this.job = this.jobs.get("job").start(
|
||||
this.steps.get("step").tasklet(throwingTasklet()).transactionManager(this.transactionManager).build())
|
||||
this.job = new JobBuilder("job").repository(this.jobRepository)
|
||||
.start(new StepBuilder("step").repository(this.jobRepository).tasklet(throwingTasklet())
|
||||
.transactionManager(this.transactionManager).build())
|
||||
.build();
|
||||
// start a job instance
|
||||
JobParameters jobParameters = new JobParametersBuilder().addString("name", "foo").toJobParameters();
|
||||
@@ -154,8 +152,8 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
|
||||
@DirtiesContext
|
||||
@Test
|
||||
public void retryFailedExecutionOnNonRestartableJob() throws Exception {
|
||||
this.job = this.jobs
|
||||
.get("job").preventRestart().start(this.steps.get("step").tasklet(throwingTasklet())
|
||||
this.job = new JobBuilder("job").repository(this.jobRepository).preventRestart()
|
||||
.start(new StepBuilder("step").repository(this.jobRepository).tasklet(throwingTasklet())
|
||||
.transactionManager(this.transactionManager).build())
|
||||
.incrementer(new RunIdIncrementer()).build();
|
||||
runFailedJob(new JobParameters());
|
||||
@@ -174,8 +172,8 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
|
||||
@DirtiesContext
|
||||
@Test
|
||||
public void retryFailedExecutionWithNonIdentifyingParameters() throws Exception {
|
||||
this.job = this.jobs
|
||||
.get("job").start(this.steps.get("step").tasklet(throwingTasklet())
|
||||
this.job = new JobBuilder("job").repository(this.jobRepository)
|
||||
.start(new StepBuilder("step").repository(this.jobRepository).tasklet(throwingTasklet())
|
||||
.transactionManager(this.transactionManager).build())
|
||||
.incrementer(new RunIdIncrementer()).build();
|
||||
JobParameters jobParameters = new JobParametersBuilder().addLong("id", 1L, false).addLong("foo", 2L, false)
|
||||
@@ -189,8 +187,8 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
|
||||
@DirtiesContext
|
||||
@Test
|
||||
public void retryFailedExecutionWithDifferentNonIdentifyingParametersFromPreviousExecution() throws Exception {
|
||||
this.job = this.jobs
|
||||
.get("job").start(this.steps.get("step").tasklet(throwingTasklet())
|
||||
this.job = new JobBuilder("job").repository(this.jobRepository)
|
||||
.start(new StepBuilder("step").repository(this.jobRepository).tasklet(throwingTasklet())
|
||||
.transactionManager(this.transactionManager).build())
|
||||
.incrementer(new RunIdIncrementer()).build();
|
||||
JobParameters jobParameters = new JobParametersBuilder().addLong("id", 1L, false).addLong("foo", 2L, false)
|
||||
@@ -285,7 +283,7 @@ public class TaskJobLauncherApplicationRunnerCoreTests {
|
||||
|
||||
@Override
|
||||
public JobLauncher getJobLauncher() throws Exception {
|
||||
SimpleJobLauncher launcher = new SimpleJobLauncher();
|
||||
TaskExecutorJobLauncher launcher = new TaskExecutorJobLauncher();
|
||||
|
||||
launcher.setJobRepository(getJobRepository());
|
||||
launcher.setTaskExecutor(new SyncTaskExecutor());
|
||||
|
||||
@@ -30,12 +30,13 @@ import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
|
||||
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
|
||||
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.explore.JobExplorer;
|
||||
import org.springframework.batch.core.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
|
||||
import org.springframework.batch.core.launch.support.TaskExecutorJobLauncher;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
@@ -184,23 +185,21 @@ public class TaskJobLauncherApplicationRunnerTests {
|
||||
public static class JobConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Bean
|
||||
public Job job() {
|
||||
return this.jobBuilderFactory.get("job").start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
}).transactionManager(transactionManager).build()).build();
|
||||
return new JobBuilder("job").repository(this.jobRepository)
|
||||
.start(new StepBuilder("step1").repository(this.jobRepository).tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
}).transactionManager(transactionManager).build()).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -214,29 +213,28 @@ public class TaskJobLauncherApplicationRunnerTests {
|
||||
public static class JobWithFailureConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Bean
|
||||
public Job jobFail() {
|
||||
return this.jobBuilderFactory.get("jobA").start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
|
||||
System.out.println("Executed");
|
||||
throw new IllegalStateException("WHOOPS");
|
||||
}
|
||||
}).transactionManager(transactionManager).build()).build();
|
||||
return new JobBuilder("jobA").repository(this.jobRepository)
|
||||
.start(new StepBuilder("step1").repository(this.jobRepository).tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
|
||||
throws Exception {
|
||||
System.out.println("Executed");
|
||||
throw new IllegalStateException("WHOOPS");
|
||||
}
|
||||
}).transactionManager(transactionManager).build()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job jobFun() {
|
||||
return this.jobBuilderFactory.get("jobSucceed")
|
||||
.start(this.stepBuilderFactory.get("step1Succeed").tasklet(new Tasklet() {
|
||||
return new JobBuilder("jobSucceed").repository(this.jobRepository)
|
||||
.start(new StepBuilder("step1Succeed").repository(this.jobRepository).tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
|
||||
System.out.println("Executed");
|
||||
@@ -270,7 +268,7 @@ public class TaskJobLauncherApplicationRunnerTests {
|
||||
}
|
||||
|
||||
protected JobLauncher createJobLauncher() throws Exception {
|
||||
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
|
||||
TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
|
||||
jobLauncher.setJobRepository(getJobRepository());
|
||||
jobLauncher.setTaskExecutor(new ConcurrentTaskExecutor());
|
||||
jobLauncher.afterPropertiesSet();
|
||||
|
||||
@@ -25,8 +25,9 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
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.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
@@ -73,9 +74,9 @@ public class PrefixTests {
|
||||
public static class JobConfiguration {
|
||||
|
||||
@Bean
|
||||
public Job job(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
|
||||
return jobBuilderFactory.get("job")
|
||||
.start(stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> {
|
||||
public Job job(JobRepository jobRepository) {
|
||||
return new JobBuilder("job").repository(jobRepository)
|
||||
.start(new StepBuilder("step1").repository(jobRepository).tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).transactionManager(new ResourcelessTransactionManager()).build()).build();
|
||||
|
||||
@@ -25,8 +25,9 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
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.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
@@ -72,9 +73,9 @@ class PrimaryKeyTests {
|
||||
static class JobConfiguration {
|
||||
|
||||
@Bean
|
||||
Job job(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
|
||||
return jobBuilderFactory.get("job")
|
||||
.start(stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> {
|
||||
Job job(JobRepository jobRepository) {
|
||||
return new JobBuilder("job").repository(jobRepository)
|
||||
.start(new StepBuilder("step1").repository(jobRepository).tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).transactionManager(new ResourcelessTransactionManager()).build()).build();
|
||||
|
||||
@@ -31,10 +31,11 @@ import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
|
||||
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.job.SimpleJob;
|
||||
import org.springframework.batch.core.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
@@ -282,18 +283,15 @@ public class TaskBatchExecutionListenerTests {
|
||||
public static class JobConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Bean
|
||||
public Job job() {
|
||||
return this.jobBuilderFactory.get("job")
|
||||
.start(this.stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> {
|
||||
return new JobBuilder("job").repository(this.jobRepository).start(
|
||||
new StepBuilder("step1").repository(this.jobRepository).tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).transactionManager(this.transactionManager).build()).build();
|
||||
@@ -307,21 +305,18 @@ public class TaskBatchExecutionListenerTests {
|
||||
public static class TaskNotEnabledConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@Autowired
|
||||
PlatformTransactionManager transactionManager;
|
||||
|
||||
@Bean
|
||||
public Job job() {
|
||||
return this.jobBuilderFactory.get("job")
|
||||
.start(this.stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> {
|
||||
return new JobBuilder("job").start(
|
||||
new StepBuilder("step1").repository(this.jobRepository).tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).transactionManager(transactionManager).build()).build();
|
||||
}).transactionManager(transactionManager).build()).repository(this.jobRepository).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -333,10 +328,7 @@ public class TaskBatchExecutionListenerTests {
|
||||
public static class JobFactoryBeanConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
@@ -346,13 +338,12 @@ public class TaskBatchExecutionListenerTests {
|
||||
return new FactoryBean<Job>() {
|
||||
@Override
|
||||
public Job getObject() {
|
||||
return JobFactoryBeanConfiguration.this.jobBuilderFactory.get("job")
|
||||
.start(JobFactoryBeanConfiguration.this.stepBuilderFactory.get("step1")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).transactionManager(transactionManager).build())
|
||||
.build();
|
||||
return new JobBuilder("job")
|
||||
.start(new StepBuilder("step1").tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).transactionManager(transactionManager).repository(jobRepository).build())
|
||||
.repository(jobRepository).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -376,14 +367,17 @@ public class TaskBatchExecutionListenerTests {
|
||||
public static class JobConfigurationMultipleDataSources {
|
||||
|
||||
@Bean
|
||||
public Job job(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
|
||||
return jobBuilderFactory.get("job").start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
}).transactionManager(new ResourcelessTransactionManager()).build()).build();
|
||||
public Job job(JobRepository jobRepository) {
|
||||
return new JobBuilder("job").repository(jobRepository)
|
||||
.start(new StepBuilder("step1").tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
|
||||
throws Exception {
|
||||
System.out.println("Executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
}).transactionManager(new ResourcelessTransactionManager()).repository(jobRepository).build())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -420,30 +414,27 @@ public class TaskBatchExecutionListenerTests {
|
||||
public static class MultipleJobConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Bean
|
||||
public Job job1() {
|
||||
return this.jobBuilderFactory.get("job1")
|
||||
.start(this.stepBuilderFactory.get("job1step1").tasklet((contribution, chunkContext) -> {
|
||||
return new JobBuilder("job1").repository(this.jobRepository)
|
||||
.start(new StepBuilder("job1step1").tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed job1");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).transactionManager(transactionManager).build()).build();
|
||||
}).transactionManager(transactionManager).repository(this.jobRepository).build()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job job2() {
|
||||
return this.jobBuilderFactory.get("job2")
|
||||
.start(this.stepBuilderFactory.get("job2step1").tasklet((contribution, chunkContext) -> {
|
||||
return new JobBuilder("job2").repository(this.jobRepository)
|
||||
.start(new StepBuilder("job2step1").tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("Executed job2");
|
||||
return RepeatStatus.FINISHED;
|
||||
}).transactionManager(transactionManager).build()).build();
|
||||
}).transactionManager(transactionManager).repository(this.jobRepository).build()).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,9 +22,10 @@ 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.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.item.Chunk;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
@@ -48,22 +49,19 @@ public class JobConfiguration {
|
||||
private static final int DEFAULT_CHUNK_COUNT = 3;
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Bean
|
||||
public Job job() {
|
||||
return this.jobBuilderFactory.get("job").start(step1()).next(step2()).build();
|
||||
return new JobBuilder("job").repository(this.jobRepository).start(step1()).next(step2()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step1() {
|
||||
return this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
|
||||
return new StepBuilder("step1").repository(this.jobRepository).tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
|
||||
System.out.println("Executed");
|
||||
@@ -74,7 +72,7 @@ public class JobConfiguration {
|
||||
|
||||
@Bean
|
||||
public Step step2() {
|
||||
return this.stepBuilderFactory.get("step2").<String, String>chunk(DEFAULT_CHUNK_COUNT)
|
||||
return new StepBuilder("step2").repository(this.jobRepository).<String, String>chunk(DEFAULT_CHUNK_COUNT)
|
||||
.reader(new ListItemReader<>(Arrays.asList("1", "2", "3", "4", "5", "6")))
|
||||
.processor(new ItemProcessor<String, String>() {
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2019 the original author or authors.
|
||||
* Copyright 2016-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,9 +20,10 @@ 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.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
@@ -41,22 +42,19 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||
public class JobSkipConfiguration {
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Bean
|
||||
public Job job() {
|
||||
return this.jobBuilderFactory.get("job").start(step1()).next(step2()).build();
|
||||
return new JobBuilder("job").repository(this.jobRepository).start(step1()).next(step2()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step1() {
|
||||
return this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
|
||||
return new StepBuilder("step1").repository(this.jobRepository).tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
|
||||
System.out.println("Executed");
|
||||
@@ -67,8 +65,9 @@ public class JobSkipConfiguration {
|
||||
|
||||
@Bean
|
||||
public Step step2() {
|
||||
return this.stepBuilderFactory.get("step2").chunk(3).faultTolerant().skip(IllegalStateException.class)
|
||||
.skipLimit(100).reader(new SkipItemReader()).processor(new ItemProcessor<Object, Object>() {
|
||||
return new StepBuilder("step2").repository(this.jobRepository).chunk(3).faultTolerant()
|
||||
.skip(IllegalStateException.class).skipLimit(100).reader(new SkipItemReader())
|
||||
.processor(new ItemProcessor<Object, Object>() {
|
||||
@Override
|
||||
public String process(Object item) throws Exception {
|
||||
return String.valueOf(Integer.parseInt((String) item) * -1);
|
||||
|
||||
@@ -17,15 +17,15 @@
|
||||
package io.spring.cloud;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
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.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.item.Chunk;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
@@ -55,17 +55,14 @@ public class BatchEventsApplication {
|
||||
private static final int DEFAULT_CHUNK_COUNT = 3;
|
||||
|
||||
@Autowired
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Bean
|
||||
public Step step1() {
|
||||
return this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
|
||||
return new StepBuilder("step1").repository(this.jobRepository).tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
|
||||
System.out.println("Tasklet has run");
|
||||
@@ -76,7 +73,7 @@ public class BatchEventsApplication {
|
||||
|
||||
@Bean
|
||||
public Step step2() {
|
||||
return this.stepBuilderFactory.get("step2").<String, String>chunk(DEFAULT_CHUNK_COUNT)
|
||||
return new StepBuilder("step2").repository(this.jobRepository).<String, String>chunk(DEFAULT_CHUNK_COUNT)
|
||||
.reader(new ListItemReader<>(Arrays.asList("1", "2", "3", "4", "5", "6")))
|
||||
.processor(new ItemProcessor<String, String>() {
|
||||
@Override
|
||||
@@ -95,7 +92,7 @@ public class BatchEventsApplication {
|
||||
|
||||
@Bean
|
||||
public Job job() {
|
||||
return this.jobBuilderFactory.get("job").start(step1()).next(step2()).build();
|
||||
return new JobBuilder("job").repository(this.jobRepository).start(step1()).next(step2()).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,9 +21,10 @@ import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.core.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -40,23 +41,22 @@ public class JobConfiguration {
|
||||
private static final Log logger = LogFactory.getLog(JobConfiguration.class);
|
||||
|
||||
@Autowired
|
||||
public JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
public StepBuilderFactory stepBuilderFactory;
|
||||
public JobRepository jobRepository;
|
||||
|
||||
@Autowired
|
||||
public PlatformTransactionManager transactionManager;
|
||||
|
||||
@Bean
|
||||
public Job job1() {
|
||||
return this.jobBuilderFactory.get("job1").start(this.stepBuilderFactory.get("job1step1").tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
|
||||
logger.info("Job1 was run");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
}).transactionManager(transactionManager).build()).build();
|
||||
return new JobBuilder("job1").repository(this.jobRepository)
|
||||
.start(new StepBuilder("job1step1").repository(this.jobRepository).tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
|
||||
throws Exception {
|
||||
logger.info("Job1 was run");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
}).transactionManager(transactionManager).build()).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,14 +27,14 @@ import javax.sql.DataSource;
|
||||
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.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepScope;
|
||||
import org.springframework.batch.core.explore.JobExplorer;
|
||||
import org.springframework.batch.core.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.partition.PartitionHandler;
|
||||
import org.springframework.batch.core.partition.support.Partitioner;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
@@ -55,6 +55,7 @@ import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
/**
|
||||
* @author Michael Minella
|
||||
@@ -66,17 +67,11 @@ public class JobConfiguration {
|
||||
|
||||
// @checkstyle:off
|
||||
@Autowired
|
||||
public JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
public StepBuilderFactory stepBuilderFactory;
|
||||
public JobRepository jobRepository;
|
||||
|
||||
@Autowired
|
||||
public DataSource dataSource;
|
||||
|
||||
@Autowired
|
||||
public JobRepository jobRepository;
|
||||
|
||||
// @checkstyle:on
|
||||
@Autowired
|
||||
private ConfigurableApplicationContext context;
|
||||
@@ -87,6 +82,9 @@ public class JobConfiguration {
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Bean
|
||||
public PartitionHandler partitionHandler(TaskLauncher taskLauncher, JobExplorer jobExplorer,
|
||||
TaskRepository taskRepository, @Autowired(required = false) ThreadPoolTaskExecutor executor)
|
||||
@@ -162,20 +160,23 @@ public class JobConfiguration {
|
||||
|
||||
@Bean
|
||||
public Step step1(PartitionHandler partitionHandler) throws Exception {
|
||||
return this.stepBuilderFactory.get("step1").partitioner(workerStep().getName(), partitioner())
|
||||
.step(workerStep()).partitionHandler(partitionHandler).build();
|
||||
return new StepBuilder("step1").repository(this.jobRepository)
|
||||
.partitioner(workerStep().getName(), partitioner()).step(workerStep())
|
||||
.partitionHandler(partitionHandler).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step workerStep() {
|
||||
return this.stepBuilderFactory.get("workerStep").tasklet(workerTasklet(null)).build();
|
||||
return new StepBuilder("workerStep").repository(this.jobRepository).tasklet(workerTasklet(null))
|
||||
.transactionManager(this.transactionManager).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Profile("!worker")
|
||||
public Job partitionedJob(PartitionHandler partitionHandler) throws Exception {
|
||||
Random random = new Random();
|
||||
return this.jobBuilderFactory.get("partitionedJob" + random.nextInt()).start(step1(partitionHandler)).build();
|
||||
return new JobBuilder("partitionedJob" + random.nextInt()).repository(this.jobRepository)
|
||||
.start(step1(partitionHandler)).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user