Skip to content

Commit 3583c63

Browse files
authored
Merge pull request #696 from jeffgbutler/spring-batch-updates
Updates to Spring Batch Configuration
2 parents f7f212d + f58f84f commit 3583c63

File tree

6 files changed

+33
-21
lines changed

6 files changed

+33
-21
lines changed

src/test/java/examples/springbatch/bulkinsert/BulkInsertConfiguration.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
import org.springframework.batch.core.Job;
3131
import org.springframework.batch.core.Step;
3232
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
33-
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
34-
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
33+
import org.springframework.batch.core.job.builder.JobBuilder;
3534
import org.springframework.batch.core.launch.support.RunIdIncrementer;
35+
import org.springframework.batch.core.repository.JobRepository;
36+
import org.springframework.batch.core.step.builder.StepBuilder;
3637
import org.springframework.batch.item.ItemProcessor;
3738
import org.springframework.batch.item.ItemWriter;
3839
import org.springframework.beans.factory.annotation.Autowired;
@@ -56,10 +57,10 @@
5657
public class BulkInsertConfiguration {
5758

5859
@Autowired
59-
private JobBuilderFactory jobBuilderFactory;
60+
private JobRepository jobRepository;
6061

6162
@Autowired
62-
private StepBuilderFactory stepBuilderFactory;
63+
private PlatformTransactionManager transactionManager;
6364

6465
@Bean
6566
public DataSource dataSource() {
@@ -102,7 +103,9 @@ public MyBatisBatchItemWriter<PersonRecord> writer(SqlSessionFactory sqlSessionF
102103

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

113116
@Bean
114117
public Job insertRecords(Step step1) {
115-
return jobBuilderFactory.get("insertRecords")
118+
return new JobBuilder("insertRecords")
119+
.repository(jobRepository) // In Spring Batch 5, move this to the job builder constructor
116120
.incrementer(new RunIdIncrementer())
117121
.flow(step1)
118122
.end()

src/test/java/examples/springbatch/bulkinsert/SpringBatchBulkInsertTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private int getRowCount(ExecutionContext executionContext) {
6969
return executionContext.getInt("row_count", 0);
7070
}
7171

72-
private long rowCount() throws Exception {
72+
private long rowCount() {
7373
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
7474
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
7575

src/test/java/examples/springbatch/cursor/CursorReaderBatchConfiguration.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
import org.springframework.batch.core.Job;
3333
import org.springframework.batch.core.Step;
3434
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
35-
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
36-
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
35+
import org.springframework.batch.core.job.builder.JobBuilder;
3736
import org.springframework.batch.core.launch.support.RunIdIncrementer;
37+
import org.springframework.batch.core.repository.JobRepository;
38+
import org.springframework.batch.core.step.builder.StepBuilder;
3839
import org.springframework.batch.item.ItemProcessor;
3940
import org.springframework.batch.item.ItemReader;
4041
import org.springframework.batch.item.ItemWriter;
@@ -58,10 +59,10 @@
5859
public class CursorReaderBatchConfiguration {
5960

6061
@Autowired
61-
private JobBuilderFactory jobBuilderFactory;
62+
private JobRepository jobRepository;
6263

6364
@Autowired
64-
private StepBuilderFactory stepBuilderFactory;
65+
private PlatformTransactionManager transactionManager;
6566

6667
@Bean
6768
public DataSource dataSource() {
@@ -113,7 +114,9 @@ public MyBatisBatchItemWriter<PersonRecord> writer(SqlSessionFactory sqlSessionF
113114

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

124127
@Bean
125128
public Job upperCaseLastName(Step step1) {
126-
return jobBuilderFactory.get("upperCaseLastName")
129+
return new JobBuilder("upperCaseLastName") // In Spring Batch 5, move this to the job builder constructor
130+
.repository(jobRepository)
127131
.incrementer(new RunIdIncrementer())
128132
.flow(step1)
129133
.end()

src/test/java/examples/springbatch/cursor/SpringBatchCursorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private int getRowCount(ExecutionContext executionContext) {
7171
return executionContext.getInt("row_count", 0);
7272
}
7373

74-
private long upperCaseRowCount() throws Exception {
74+
private long upperCaseRowCount() {
7575
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
7676
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
7777

src/test/java/examples/springbatch/paging/PagingReaderBatchConfiguration.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
import org.springframework.batch.core.Job;
3232
import org.springframework.batch.core.Step;
3333
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
34-
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
35-
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
34+
import org.springframework.batch.core.job.builder.JobBuilder;
3635
import org.springframework.batch.core.launch.support.RunIdIncrementer;
36+
import org.springframework.batch.core.repository.JobRepository;
37+
import org.springframework.batch.core.step.builder.StepBuilder;
3738
import org.springframework.batch.item.ItemProcessor;
3839
import org.springframework.batch.item.ItemReader;
3940
import org.springframework.batch.item.ItemWriter;
@@ -57,10 +58,10 @@
5758
public class PagingReaderBatchConfiguration {
5859

5960
@Autowired
60-
private JobBuilderFactory jobBuilderFactory;
61+
private JobRepository jobRepository;
6162

6263
@Autowired
63-
private StepBuilderFactory stepBuilderFactory;
64+
private PlatformTransactionManager transactionManager;
6465

6566
@Bean
6667
public DataSource dataSource() {
@@ -114,7 +115,9 @@ public MyBatisBatchItemWriter<PersonRecord> writer(SqlSessionFactory sqlSessionF
114115

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

125128
@Bean
126129
public Job upperCaseLastName(Step step1) {
127-
return jobBuilderFactory.get("upperCaseLastName")
130+
return new JobBuilder("upperCaseLastName")
131+
.repository(jobRepository) // In Spring Batch 5, move this to the job builder constructor
128132
.incrementer(new RunIdIncrementer())
129133
.flow(step1)
130134
.end()

src/test/java/examples/springbatch/paging/SpringBatchPagingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private int getChunkCount(ExecutionContext executionContext) {
8383
return executionContext.getInt("chunk_count", 0);
8484
}
8585

86-
private long upperCaseRowCount() throws Exception {
86+
private long upperCaseRowCount() {
8787
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
8888
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
8989

0 commit comments

Comments
 (0)