Programmatically configuring fault tolerance for a chunk works kind of as follows:
stepBuilders.get("step")
.<Partner,Partner>chunk(1)
.reader(reader())
.processor(processor())
.writer(writer())
.listener(logProcessListener())
.faultTolerant()
.skipLimit(10)
.skip(UnknownGenderException.class)
.listener(logSkipListener())
.build();
The trick is, that with adding "chunk", the chain switches to a SimpleStepBuilder which offers the "faultTolerant" method.
My question is how to do that if you just have a tasklet (no reader, processor, writer)?
Defining a tasklet works as follows:
stepBuilders.get("step")
.tasklet(tasklet())
.build();
The usage of "tasklet" switches to a TaskletStepBuilder which does not offer a "faultTolerant" method. Therefore I see no way how to define properties like skipLimit and that like.
Any ideas?
A Tasklet
has no notion of "items" to skip, so fault tolerance only makes sense for a chunk-oriented step. I suggest you use Spring Retry in the raw (1.1.0.RELEASE is out now, just, and you have some fluent builder options and a @Retryable
annotation there).
Based on the guidance given by @DaveSyer and using org.springframework.retry:spring-retry:1.1.0.RELEASE this is what I have ended up with:
Tasklet tasklet = // whatever
ProxyFactory proxyFactory = new ProxyFactory(Tasklet, new SingletonTargetSource(tasklet));
proxyFactory.addAdvice(RetryInterceptorBuilder.stateless()
.maxAttempts(3)
.build());
Step step = stepBuilderFactory.get("taskName")
.tasklet((Tasklet)proxyFactory.proxy)
.build();
The only thing I'm struggling with still is if I want to swallow the exception that causes the retry after maxAttempts
is exceeded. If I add an ExceptionHandler
to the step then the step is never retried. I guess that means the exception handler is inside the pointcut which I find a little surprising.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With