So I've been experimenting with Spring's Retry Template. Everything is working as expected. One thing I would like however is the ability to extract or log the current backoff time that's being used. I had a look through the docs/searched everywhere but I can't find any easy way of getting this.
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
exponentialBackOffPolicy.setMaxInterval(10000); // 10 secs
exponentialBackOffPolicy.setMultiplier(2); // double the backoff
retryTemplate.setBackOffPolicy(exponentialBackOffPolicy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(2);
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}
And I'm using it like this...
retryTemplate.execute(arg0 -> {
myService.templateRetryService();
return null;
});
The RetryCallback only provides the RetryConext but this doesn't contain what I need.
Any ideas would be welcome. Thanks.
You can set a custom
Sleeper
on theExponentialBackOffPolicy
to log the time.The default sleeper is this: