|
15 | 15 | */ |
16 | 16 | package org.springframework.batch.core.step.item; |
17 | 17 |
|
| 18 | +import java.util.List; |
18 | 19 | import java.util.Set; |
19 | 20 |
|
| 21 | +import org.jspecify.annotations.Nullable; |
20 | 22 | import org.junit.jupiter.api.Assertions; |
21 | 23 | import org.junit.jupiter.api.Test; |
22 | 24 |
|
|
31 | 33 | import org.springframework.batch.core.step.Step; |
32 | 34 | import org.springframework.batch.core.step.StepExecution; |
33 | 35 | import org.springframework.batch.core.step.builder.ChunkOrientedStepBuilder; |
| 36 | +import org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy; |
34 | 37 | import org.springframework.batch.core.step.skip.LimitCheckingExceptionHierarchySkipPolicy; |
35 | 38 | import org.springframework.batch.core.step.skip.SkipLimitExceededException; |
36 | 39 | import org.springframework.batch.infrastructure.item.ItemProcessor; |
37 | 40 | import org.springframework.batch.infrastructure.item.ItemReader; |
38 | 41 | import org.springframework.batch.infrastructure.item.ItemWriter; |
39 | 42 | import org.springframework.batch.infrastructure.item.file.FlatFileParseException; |
| 43 | +import org.springframework.batch.infrastructure.item.support.ListItemReader; |
40 | 44 | import org.springframework.context.ApplicationContext; |
41 | 45 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
42 | 46 | import org.springframework.context.annotation.Bean; |
@@ -188,6 +192,105 @@ void testConcurrentFaultTolerantChunkOrientedStepFailure() throws Exception { |
188 | 192 | System.clearProperty("skipLimit"); |
189 | 193 | } |
190 | 194 |
|
| 195 | + // Issue https://github.com/spring-projects/spring-batch/issues/5084 |
| 196 | + @Test |
| 197 | + void testSkipInReadInSequentialMode() throws Exception { |
| 198 | + // given |
| 199 | + ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class, |
| 200 | + StepConfiguration.class); |
| 201 | + JobOperator jobOperator = context.getBean(JobOperator.class); |
| 202 | + Job job = context.getBean(Job.class); |
| 203 | + |
| 204 | + // when |
| 205 | + JobParameters jobParameters = new JobParametersBuilder().toJobParameters(); |
| 206 | + JobExecution jobExecution = jobOperator.start(job, jobParameters); |
| 207 | + |
| 208 | + // then |
| 209 | + Assertions.assertEquals(ExitStatus.COMPLETED.getExitCode(), jobExecution.getExitStatus().getExitCode()); |
| 210 | + StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); |
| 211 | + Assertions.assertEquals(8, stepExecution.getReadCount()); |
| 212 | + Assertions.assertEquals(8, stepExecution.getWriteCount()); |
| 213 | + Assertions.assertEquals(2, stepExecution.getCommitCount()); |
| 214 | + Assertions.assertEquals(0, stepExecution.getRollbackCount()); |
| 215 | + Assertions.assertEquals(2, stepExecution.getReadSkipCount()); |
| 216 | + Assertions.assertEquals(0, stepExecution.getWriteSkipCount()); |
| 217 | + } |
| 218 | + |
| 219 | + // Issue https://github.com/spring-projects/spring-batch/issues/5084 |
| 220 | + @Test |
| 221 | + void testSkipInReadInConcurrentMode() throws Exception { |
| 222 | + // given |
| 223 | + ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class, |
| 224 | + ConcurrentStepConfiguration.class); |
| 225 | + JobOperator jobOperator = context.getBean(JobOperator.class); |
| 226 | + Job job = context.getBean(Job.class); |
| 227 | + |
| 228 | + // when |
| 229 | + JobParameters jobParameters = new JobParametersBuilder().toJobParameters(); |
| 230 | + JobExecution jobExecution = jobOperator.start(job, jobParameters); |
| 231 | + |
| 232 | + // then |
| 233 | + Assertions.assertEquals(ExitStatus.COMPLETED.getExitCode(), jobExecution.getExitStatus().getExitCode()); |
| 234 | + StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); |
| 235 | + Assertions.assertEquals(8, stepExecution.getReadCount()); |
| 236 | + Assertions.assertEquals(8, stepExecution.getWriteCount()); |
| 237 | + Assertions.assertEquals(2, stepExecution.getCommitCount()); |
| 238 | + Assertions.assertEquals(0, stepExecution.getRollbackCount()); |
| 239 | + Assertions.assertEquals(2, stepExecution.getReadSkipCount()); |
| 240 | + Assertions.assertEquals(0, stepExecution.getWriteSkipCount()); |
| 241 | + } |
| 242 | + |
| 243 | + @Configuration |
| 244 | + static class StepConfiguration { |
| 245 | + |
| 246 | + @Bean |
| 247 | + public Step step(JobRepository jobRepository, JdbcTransactionManager transactionManager) { |
| 248 | + List<String> items = List.of("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"); |
| 249 | + return new ChunkOrientedStepBuilder<String, String>(jobRepository, 5).reader(new ListItemReader<>(items) { |
| 250 | + @Override |
| 251 | + public @Nullable String read() { |
| 252 | + String item = super.read(); |
| 253 | + if ("three".equals(item) || "seven".equals(item)) { |
| 254 | + throw new RuntimeException("Simulated read error on item: " + item); |
| 255 | + } |
| 256 | + return item; |
| 257 | + } |
| 258 | + }).writer(chunk -> { |
| 259 | + }) |
| 260 | + .transactionManager(transactionManager) |
| 261 | + .faultTolerant() |
| 262 | + .skipPolicy(new AlwaysSkipItemSkipPolicy()) |
| 263 | + .build(); |
| 264 | + } |
| 265 | + |
| 266 | + } |
| 267 | + |
| 268 | + @Configuration |
| 269 | + static class ConcurrentStepConfiguration { |
| 270 | + |
| 271 | + @Bean |
| 272 | + public Step step(JobRepository jobRepository, JdbcTransactionManager transactionManager) { |
| 273 | + List<String> items = List.of("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"); |
| 274 | + return new ChunkOrientedStepBuilder<String, String>(jobRepository, 5).reader(new ListItemReader<>(items) { |
| 275 | + @Override |
| 276 | + public @Nullable String read() { |
| 277 | + String item = super.read(); |
| 278 | + if ("three".equals(item) || "seven".equals(item)) { |
| 279 | + throw new RuntimeException("Simulated read error on item: " + item); |
| 280 | + } |
| 281 | + return item; |
| 282 | + } |
| 283 | + }).writer(chunk -> { |
| 284 | + }) |
| 285 | + .transactionManager(transactionManager) |
| 286 | + .taskExecutor(new SimpleAsyncTaskExecutor()) |
| 287 | + .faultTolerant() |
| 288 | + .skipPolicy(new AlwaysSkipItemSkipPolicy()) |
| 289 | + .build(); |
| 290 | + } |
| 291 | + |
| 292 | + } |
| 293 | + |
191 | 294 | @Configuration |
192 | 295 | static class FaultTolerantChunkOrientedStepConfiguration { |
193 | 296 |
|
|
0 commit comments