Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can detect last retry? #361

Closed
donghoon-khan opened this issue Jun 7, 2023 · 3 comments
Closed

How can detect last retry? #361

donghoon-khan opened this issue Jun 7, 2023 · 3 comments

Comments

@donghoon-khan
Copy link

donghoon-khan commented Jun 7, 2023

I am using Spring-retry-1.3.4, i want to detect the event before the last retry and insert logic. Is there a way? I think I can use RetryListener, but I can't.

TestService.java

    @Retryable(
            value = {Exception.class},
            maxAttempts = 2
    )
    public void test1() {
    }

    @Retryable(
            value = {Exception.class},
            maxAttempts = 3
    )
    public void test2() {
    }

    @Retryable(
            value = {Exception.class},
            maxAttempts = 4
    )
    public void test3() {
    }

MyRetryListener.java

public class MyListener implements RetryListener {
...
    public <T, E extends Throwable> void onError(
            final RetryContext context,
            final RetryCallback<T, E> callback,
            final Throwable throwable
    ) {
        if (getMaxAttemps() == context.getRetryCount()) {
            logic();
        }
    }
    
    private int getMaxAttemps() {
        ???
    }
}
@garyrussell
Copy link
Contributor

Don't use issues to ask questions - ask on Stack Overflow or use the Discussions tab for Q&A.

The context only has access to the count; it has no knowledge of the retry policy.

@garyrussell garyrussell closed this as not planned Won't fix, can't repro, duplicate, stale Jun 7, 2023
@artembilan
Copy link
Member

Not all RetryPolicy impls are based on the count, but you indeed can inject the same MaxAttemptsRetryPolicy into your listener to make an analysis you are looking for based on its getMaxAttempts().

@artembilan
Copy link
Member

Also, there is a special ExhaustedRetryException thrown in case a RetryPolicy cannot retry any more:

			rethrow(context, "Retry exhausted after last attempt with no recovery path",
					this.throwLastExceptionOnExhausted || !doRecover);
...
protected <E extends Throwable> void rethrow(RetryContext context, String message, boolean wrap) throws E {
		if (wrap) {
			@SuppressWarnings("unchecked")
			E rethrow = (E) context.getLastThrowable();
			throw rethrow;
		}
		else {
			throw new ExhaustedRetryException(message, context.getLastThrowable());
		}
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants