I am trying to set up a RetryTemplate in Spring Batch, I just cannot find an example on how to add the RetryTemplate into the StepBuilderFactory. I've found this example to set it up in a SpringBoot Application, https://dzone.com/articles/how-to-use-spring-retry-template but no luck on Spring Batch.
The reason I am trying to use RetryTemplate is to set-up a Exponential BackOffPolicy (Spring Batch how to configure retry period for failed jobs).
I imagine to wire the RetryTemplate would be as simple as setting the RetryTemplate Bean, something like it (modified code from @Mahmoud Ben Hassine answer Spring Batch how to configure retry period for failed jobs):
@Bean
public RetryTemplate testExponentialBackoff() throws Exception {
// configure backoff policy
ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
exponentialBackOffPolicy.setInitialInterval(1000);
exponentialBackOffPolicy.setMultiplier(2.0);
exponentialBackOffPolicy.setMaxInterval(10000);
// configure retry policy
SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
simpleRetryPolicy.setMaxAttempts(5);
// configure retry template
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setBackOffPolicy(exponentialBackOffPolicy);
retryTemplate.setRetryPolicy(simpleRetryPolicy);
return retryTemplate;
}
I am not finding a way to wire it to the StepBuilderFactory . I would imagine it would be something similar to the "standard" retry:
.faultTolerant()
.retryLimit(3)
.retry(ConnectTimeoutException.class)
Does anybody have an example/template on how to set it up using Java Configuration?
Any help/example is appreciated. Thx, Markus.
If your goal is to set a custom backoff policy, there is no need to provide an entire RetryTemplate
for that, you can do it with the FaultTolerantStepBuilder#backOffPolicy
method, something like:
// configure backoff policy
ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
// customize exponentialBackOffPolicy as needed
Step step = stepBuilderFactory.get("step")
.chunk(5)
// configure reader/writer etc
.faultTolerant()
.backOffPolicy(exponentialBackOffPolicy)
// set other properties
.build();
Now if you really want to provide a custom RetryOperations
object, you need to extend the FaultTolerantStepBuilder
and override the createRetryOperations
method.