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.