Skip to content

Commit

Permalink
Merge pull request #696 from jeffgbutler/spring-batch-updates
Browse files Browse the repository at this point in the history
Updates to Spring Batch Configuration
  • Loading branch information
jeffgbutler authored Nov 13, 2023
2 parents f7f212d + f58f84f commit 3583c63
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
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.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -56,10 +57,10 @@
public class BulkInsertConfiguration {

@Autowired
private JobBuilderFactory jobBuilderFactory;
private JobRepository jobRepository;

@Autowired
private StepBuilderFactory stepBuilderFactory;
private PlatformTransactionManager transactionManager;

@Bean
public DataSource dataSource() {
Expand Down Expand Up @@ -102,7 +103,9 @@ public MyBatisBatchItemWriter<PersonRecord> writer(SqlSessionFactory sqlSessionF

@Bean
public Step step1(ItemProcessor<PersonRecord, PersonRecord> processor, ItemWriter<PersonRecord> writer) {
return stepBuilderFactory.get("step1")
return new StepBuilder("step1")
.repository(jobRepository) // In Spring Batch 5, move this to the step builder constructor
.transactionManager(transactionManager) // In Spring Batch 5, move this to the 'chunk' method
.<PersonRecord, PersonRecord>chunk(10)
.reader(new TestRecordGenerator())
.processor(processor)
Expand All @@ -112,7 +115,8 @@ public Step step1(ItemProcessor<PersonRecord, PersonRecord> processor, ItemWrite

@Bean
public Job insertRecords(Step step1) {
return jobBuilderFactory.get("insertRecords")
return new JobBuilder("insertRecords")
.repository(jobRepository) // In Spring Batch 5, move this to the job builder constructor
.incrementer(new RunIdIncrementer())
.flow(step1)
.end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private int getRowCount(ExecutionContext executionContext) {
return executionContext.getInt("row_count", 0);
}

private long rowCount() throws Exception {
private long rowCount() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
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.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
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;
Expand All @@ -58,10 +59,10 @@
public class CursorReaderBatchConfiguration {

@Autowired
private JobBuilderFactory jobBuilderFactory;
private JobRepository jobRepository;

@Autowired
private StepBuilderFactory stepBuilderFactory;
private PlatformTransactionManager transactionManager;

@Bean
public DataSource dataSource() {
Expand Down Expand Up @@ -113,7 +114,9 @@ public MyBatisBatchItemWriter<PersonRecord> writer(SqlSessionFactory sqlSessionF

@Bean
public Step step1(ItemReader<PersonRecord> reader, ItemProcessor<PersonRecord, PersonRecord> processor, ItemWriter<PersonRecord> writer) {
return stepBuilderFactory.get("step1")
return new StepBuilder("step1")
.repository(jobRepository) // In Spring Batch 5, move this to the step builder constructor
.transactionManager(transactionManager) // In Spring Batch 5, move this to the 'chunk' method
.<PersonRecord, PersonRecord>chunk(10)
.reader(reader)
.processor(processor)
Expand All @@ -123,7 +126,8 @@ public Step step1(ItemReader<PersonRecord> reader, ItemProcessor<PersonRecord, P

@Bean
public Job upperCaseLastName(Step step1) {
return jobBuilderFactory.get("upperCaseLastName")
return new JobBuilder("upperCaseLastName") // In Spring Batch 5, move this to the job builder constructor
.repository(jobRepository)
.incrementer(new RunIdIncrementer())
.flow(step1)
.end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private int getRowCount(ExecutionContext executionContext) {
return executionContext.getInt("row_count", 0);
}

private long upperCaseRowCount() throws Exception {
private long upperCaseRowCount() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
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.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
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;
Expand All @@ -57,10 +58,10 @@
public class PagingReaderBatchConfiguration {

@Autowired
private JobBuilderFactory jobBuilderFactory;
private JobRepository jobRepository;

@Autowired
private StepBuilderFactory stepBuilderFactory;
private PlatformTransactionManager transactionManager;

@Bean
public DataSource dataSource() {
Expand Down Expand Up @@ -114,7 +115,9 @@ public MyBatisBatchItemWriter<PersonRecord> writer(SqlSessionFactory sqlSessionF

@Bean
public Step step1(ItemReader<PersonRecord> reader, ItemProcessor<PersonRecord, PersonRecord> processor, ItemWriter<PersonRecord> writer) {
return stepBuilderFactory.get("step1")
return new StepBuilder("step1")
.repository(jobRepository) // In Spring Batch 5, move this to the step builder constructor
.transactionManager(transactionManager) // In Spring Batch 5, move this to the 'chunk' method
.<PersonRecord, PersonRecord>chunk(7)
.reader(reader)
.processor(processor)
Expand All @@ -124,7 +127,8 @@ public Step step1(ItemReader<PersonRecord> reader, ItemProcessor<PersonRecord, P

@Bean
public Job upperCaseLastName(Step step1) {
return jobBuilderFactory.get("upperCaseLastName")
return new JobBuilder("upperCaseLastName")
.repository(jobRepository) // In Spring Batch 5, move this to the job builder constructor
.incrementer(new RunIdIncrementer())
.flow(step1)
.end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private int getChunkCount(ExecutionContext executionContext) {
return executionContext.getInt("chunk_count", 0);
}

private long upperCaseRowCount() throws Exception {
private long upperCaseRowCount() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);

Expand Down

0 comments on commit 3583c63

Please sign in to comment.