-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Description
I'm currently trying to migrate the two simple pieces of code which use spring-retry to the RetryTemplate provided by the framework. But it's harder than I imagined for the following reasons:
First reason: No timeout support.
The RetryPolicy builder doesn't offer any way to specify a timeout for the retries (i.e. if the first execution started at time x, and the policy is invoked at time y and y > x + timeout, then the policy should return STOP).
There is a way to implement this by creating a custom BackOff class delegating to ExponentialBackOff (and its execution delegating to the ExponentialBackOff execution), but it's cumbersome for a use-case that looks quite frequent to me. You might argue that we could turn the timeout into a number of retries by taking the delay and the multiplier into account, but that wouldn't take the time spent inside the retryable operation itself.
Also, ExponentialBackOff has a maxElapsedTime, but it's not actually a max elapsed time. It's just a sum of the intervals, so once again not taking into account the time spent in the retryable operation itself.
Second reason: no access to the attempt number in the retry listener.
Let's say I want to log every failed attempt of a Retryable. A listener looks like the appropriate tool. But its method signature is the following one:
onRetryFailure(RetryPolicy retryPolicy, Retryable<?> retryable, Throwable throwable)
There is no way to know which attempt failed.
Third reason: successes and failures of the initial attempts are not passed to the listener.
The success and failure listeners are not called when the initial attempt succeeds or fails.
So the handling of failures and successes of a retryable (incrementing counters, logging) can't really be done using a listener without doing it inside the retryable itself, catching exceptions and rethrowing them.
Question about the future of spring-retry
Given spring-retry is not in the managed dependencies of Spring Boot anymore, is spring-retry deprecated in favor of Spring Framework's native retry support? Or will it still be maintained in the future? I'm fine with using Spring's native support for simple cases (i.e. @Retryable-annotated methods), and spring-retry or some other solution for more complex cases.