Skip to content

Expose RetryState on the success path #37460

Closed as duplicate
Closed as duplicate

Description

@guanchengang

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 the RetryState.
  • beforeRetry(policy, retryable, retryState) is invoked before every retry attempt and receives the RetryState.
  • The failure callbacks (onRetryPolicyExhaustion, onRetryPolicyInterruption, onRetryPolicyTimeout) receive a RetryException, which implements RetryState.

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 onRetryableExecution describes the passed RetryState as 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

  1. added
    status: waiting-for-triage
    on Oct 12, 2026
  2. jhoeller commented on Oct 12, 2026

    @jhoeller
    Contributor

    This use case sounds plausible indeed and is worth a dedicated overload of onRetrySuccess with a RetryState argument indeed. For parity, let's also introduce an analogous overload with a RetryState argument to onRetryFailure as well (which otherwise only receives the original exception rather than a RetryState-implementing RetryException).

    As for the stability of the RetryState instance, 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 given RetryState instance is effectively stable since it reached the end of its updates at that point - even for onRetryableExecution (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.

  3. self-assigned this
    on Oct 12, 2026
  4. added
    in: core
    type: enhancement
    and removed
    status: waiting-for-triage
    on Oct 12, 2026
  5. added this to the 7.1.0-RC1 milestone on Oct 12, 2026
  6. guanchengang commented on Oct 12, 2026

    @guanchengang
    ContributorAuthor

    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.

  7. added a commit that references this issue on Oct 13, 2026
    1a1bc75
  8. jhoeller commented on Oct 13, 2026

    @jhoeller
    Contributor

    Superseded by PR #37465. Thanks for the timely contribution!

  9. removed this from the 7.1.0-RC1 milestone on Oct 13, 2026
  10. added a commit that references this issue on Oct 13, 2026
    85a532d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions