1919import org .junit .jupiter .api .Test ;
2020
2121import org .springframework .batch .core .ExitStatus ;
22+ import org .springframework .batch .core .configuration .annotation .EnableBatchProcessing ;
23+ import org .springframework .batch .core .configuration .annotation .EnableJdbcJobRepository ;
2224import org .springframework .batch .core .job .Job ;
2325import org .springframework .batch .core .job .JobExecution ;
26+ import org .springframework .batch .core .job .builder .JobBuilder ;
2427import org .springframework .batch .core .job .parameters .JobParameters ;
2528import org .springframework .batch .core .job .parameters .JobParametersBuilder ;
2629import org .springframework .batch .core .launch .JobOperator ;
2730import org .springframework .batch .core .repository .JobRepository ;
2831import org .springframework .batch .core .step .Step ;
2932import org .springframework .batch .core .step .StepExecution ;
3033import org .springframework .batch .core .step .builder .ChunkOrientedStepBuilder ;
34+ import org .springframework .batch .core .step .builder .StepBuilder ;
3135import org .springframework .batch .infrastructure .item .ItemProcessor ;
3236import org .springframework .batch .infrastructure .item .ItemReader ;
3337import org .springframework .batch .infrastructure .item .ItemWriter ;
38+ import org .springframework .batch .infrastructure .item .support .ListItemWriter ;
3439import org .springframework .context .ApplicationContext ;
3540import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
3641import org .springframework .context .annotation .Bean ;
3742import org .springframework .context .annotation .Configuration ;
43+ import org .springframework .context .annotation .Import ;
3844import org .springframework .core .task .SimpleAsyncTaskExecutor ;
3945import org .springframework .jdbc .core .JdbcTemplate ;
4046import org .springframework .jdbc .support .JdbcTransactionManager ;
@@ -160,6 +166,23 @@ void testConcurrentChunkOrientedStepFailure() throws Exception {
160166 System .clearProperty ("fail" );
161167 }
162168
169+ // Issue: https://github.com/spring-projects/spring-batch/issues/5126
170+ @ Test
171+ void testChunkOrientedStepReExecution () throws Exception {
172+ // given
173+ ApplicationContext context = new AnnotationConfigApplicationContext (StepConfiguration .class );
174+ JobOperator jobOperator = context .getBean (JobOperator .class );
175+ Job job = context .getBean (Job .class );
176+
177+ // when
178+ jobOperator .start (job , new JobParametersBuilder ().addLong ("run.id" , 1L ).toJobParameters ());
179+ jobOperator .start (job , new JobParametersBuilder ().addLong ("run.id" , 2L ).toJobParameters ());
180+
181+ // then
182+ ListItemWriter <String > itemWriter = context .getBean (ListItemWriter .class );
183+ Assertions .assertEquals (2 , itemWriter .getWrittenItems ().size ());
184+ }
185+
163186 @ Configuration
164187 static class ChunkOrientedStepConfiguration {
165188
@@ -193,4 +216,30 @@ public Step concurrentChunkOrientedStep(JobRepository jobRepository, JdbcTransac
193216
194217 }
195218
219+ @ Configuration
220+ @ EnableBatchProcessing
221+ @ EnableJdbcJobRepository
222+ @ Import (JdbcInfrastructureConfiguration .class )
223+ static class StepConfiguration {
224+
225+ // singleton-scoped item writer acting as a global collector
226+ // of written items across job executions
227+ @ Bean
228+ public ListItemWriter <String > itemWriter () {
229+ return new ListItemWriter <>();
230+ }
231+
232+ @ Bean
233+ Job job (JobRepository jobRepository , JdbcTransactionManager transactionManager ,
234+ ListItemWriter <String > itemWriter ) {
235+ ChunkOrientedStep <String , String > step = new StepBuilder ("step" , jobRepository ).<String , String >chunk (1 )
236+ .transactionManager (transactionManager )
237+ .reader (new SingleItemStreamReader <>("foo" ))
238+ .writer (itemWriter )
239+ .build ();
240+ return new JobBuilder (jobRepository ).start (step ).build ();
241+ }
242+
243+ }
244+
196245}
0 commit comments