Repository navigation
Description
Background
A common requirement is to report "succeeded after N retries" to Micrometer or to the logs, for example to monitor jitter in a downstream dependency, or to decide whether to degrade gracefully or trigger an additional health check based on the number of retries. This information is currently not available through the natural success callback.
RetryTemplate returns only the result when an operation eventually succeeds:
<R> R execute(Retryable<R> retryable) throws RetryException;RetryListener already exposes RetryState through several callbacks:
onRetryableExecution(policy, retryable, retryState)is invoked after every attempt, including the initial one and the successful one, and receives theRetryState.beforeRetry(policy, retryable, retryState)is invoked before every retry attempt and receives theRetryState.- The failure callbacks (
onRetryPolicyExhaustion,onRetryPolicyInterruption,onRetryPolicyTimeout) receive aRetryException, which implementsRetryState.
The one callback that reports success, onRetrySuccess(policy, retryable, result), does not receive the RetryState. A listener could instead use onRetryableExecution, which is also invoked on the successful attempt, but doing so has two drawbacks:
- The Javadoc of
onRetryableExecutiondescribes the passedRetryStateas a "live instance reflecting the current state; not intended to be stored". Reading it inside the callback is fine, but a listener that wants to report the retry count after the invocation needs a stable snapshot. - The callback fires on every attempt, so the listener has to distinguish the initial attempt from a retry (for example via
RetryState.getRetryCount() == 0) and, with concurrent invocations, map each callback back to the invocation it belongs to. This significantly complicates the listener for what is otherwise a simple reporting need.
Proposal
Add an overload of onRetrySuccess that receives the RetryState, following the same overload-plus-default-delegation pattern already used by beforeRetry:
default void onRetrySuccess(RetryPolicy retryPolicy, Retryable<?> retryable, RetryState retryState, @Nullable Object result) {
onRetrySuccess(retryPolicy, retryable, result);
}
default void onRetrySuccess(RetryPolicy retryPolicy, Retryable<?> retryable, @Nullable Object result) {
}Question
Would the team be open to adding an overload of onRetrySuccess that receives the RetryState, as shown above?
This would let listeners report "succeeded after N retries" directly from the success callback, without having to distinguish the initial attempt from a retry or map callbacks back to invocations when using onRetryableExecution.
I'm happy to provide a PR and update the related tests if this is something you'd consider.
Activity
jhoeller commentedon Oct 12, 2026
This use case sounds plausible indeed and is worth a dedicated overload of
onRetrySuccesswith aRetryStateargument indeed. For parity, let's also introduce an analogous overload with aRetryStateargument toonRetryFailureas well (which otherwise only receives the original exception rather than a RetryState-implementing RetryException).As for the stability of the
RetryStateinstance, that should technically be the live instance for those new overloaded methods as well (for efficiency reasons, in particular on listeners that do not listen to those particular callbacks). That said, for a success scenario, the givenRetryStateinstance is effectively stable since it reached the end of its updates at that point - even foronRetryableExecution(for such a final success callback). We could strengthen that stability characteristic for a success callback through mentioning it in the javadoc.Overall, I'd be happy to roll this into 7.1.0.RC1. Please note that we have an upcoming cut-off for the next round of releases, so we'd really need a PR by Wednesday this week. Otherwise I can pick this up directly so that we don't miss the release opportunity.
guanchengang commentedon Oct 12, 2026
Thanks for the heads-up. I’ll submit a PR within the next 24 hours so we can still make the 7.1.0.RC1 cutoff.
Add RetryState-aware overloads for onRetrySuccess and onRetryFailure
jhoeller commentedon Oct 13, 2026
Superseded by PR #37465. Thanks for the timely contribution!
Add RetryState-aware overloads for onRetrySuccess and onRetryFailure (#…