10

I recognized some unwanted and unintuitive behavior with spring-batch and chunk based reader/processor/writer setup where the item writer is called more often than I want.

I would like to have a setup in which on any exception the writer will not be called again.

Please have a look at this very stripped down initialization of a demo step:

stepBuilder
.chunk(1)
.reader(new ListItemReader<>(List.of("1", "2", "3")))
.writer(items -> {
    LOGGER.info("About to write " + items);
    throw new RuntimeException();
})
.faultTolerant()
.skipPolicy((throwable, skipCount) -> true)
.retryPolicy(new MaxAttemptsRetryPolicy(1))
// or
//.retryPolicy(new NeverRetryPolicy())

No matter what combination of skipPolicy and retryPolicy I use, the result is always

About to write [1]
About to write [1]
About to write [2]
About to write [2]
About to write [3]
About to write [3]

My expectation - or what I would like to achieve - is that after each exception the next item is processed/written.

A fully executable (but still minimalistic) demo can be found here https://github.com/LorenzSchumann/spring-batch-demo-simple

A similar version with JPA can be found here. https://github.com/LorenzSchumann/spring-batch-demo-jpa This demo also triggers two different types of exceptions. The one with the optimistic locking scenario is the one I really care about.

realsim
  • 1,386
  • 3
  • 13
  • 25
  • 1
    What you are observing is the chunk scanning feature: https://github.com/spring-projects/spring-batch/tree/main/spring-batch-samples#chunk-scanning-sample . You might find this thread helpful: https://stackoverflow.com/questions/61756484/springbatch-chunkstep-retried-on-exception-in-writer-even-with-neverretrypolicy/61770872#61770872 – Mahmoud Ben Hassine Nov 18 '22 at 08:25
  • Thanks! You cannot imagine how helpful your answer is! If you post it as an official answer, I will accept it as correct answer. – realsim Nov 18 '22 at 09:11

2 Answers2

1

Answer provided as comment by Mahmoud Ben Hassine:

What you are observing is the chunk scanning feature: https://github.com/spring-projects/spring-batch/tree/main/spring-batch-samples#chunk-scanning-sample . You might find this thread helpful: SpringBatch: ChunkStep retried on exception in writer even with NeverRetryPolicy

realsim
  • 1,386
  • 3
  • 13
  • 25
0

Try removing the retryPolicy completely...

stepBuilder
.chunk(1)
.reader(new ListItemReader<>(List.of("1", "2", "3")))
.writer(items -> {
    LOGGER.info("About to write " + items);
    throw new RuntimeException();
})
.faultTolerant()
.skipLimit(Integer.MAX_VALUE)
.skip(Exception.class)
.build()
httPants
  • 1,832
  • 1
  • 11
  • 13