Removed Job and Step Builder factories.

Now using builders directly
This commit is contained in:
Glenn Renfro
2022-09-13 18:39:24 -04:00
parent f30ac7e5b3
commit 30d6c63a23
12 changed files with 160 additions and 179 deletions

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}