Spring / Spring Retry Interview Questions
How does the RetryListener interface work in Spring Retry?
RetryListener is a callback interface that lets you hook into retry lifecycle events without modifying the retried method or recovery logic. It is useful for metrics collection, structured logging, and alerting.
The RetryListener interface defines three methods:
public interface RetryListener {
<T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback);
<T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable);
<T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable);
}- open: Called before the first attempt. Returning
falsesuppresses all retries for this operation. - onError: Called after each failed attempt with the thrown exception and current RetryContext.
- close: Called after the retry sequence ends — whether by success, exhaustion, or exception.
Registering a listener on RetryTemplate:
retryTemplate.setListeners(new RetryListener[] { new MyMetricsListener() });For annotation-based retry, register the listener bean by name in the listeners attribute of @Retryable:
@Retryable(listeners = {"metricsRetryListener"})
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
