Spring / Spring Retry Interview Questions
What is a RetryContext and what information does it carry?
RetryContext is an object created by Spring Retry at the start of each retry sequence and passed through every retry attempt. It acts as a stateful record of the current retry operation, storing information that policies, listeners, and recovery callbacks can inspect or modify.
Key information available in RetryContext:
- Retry count:
context.getRetryCount()returns the number of attempts completed so far (0 on the first try). - Last exception:
context.getLastThrowable()returns the exception that caused the most recent failure. - Exhausted flag:
context.isExhaustedOnly()indicates that retries were exhausted by external signal rather than policy. - Attribute store:
context.setAttribute(key, value)/context.getAttribute(key)let you attach custom data for use across attempts or in listeners.
Accessing RetryContext in a recovery callback:
retryTemplate.execute(context -> {
log.info("Attempt: " + context.getRetryCount());
return callService();
}, context -> {
Throwable ex = context.getLastThrowable();
log.error("Giving up after " + context.getRetryCount() + " attempts", ex);
return "fallback";
});Custom retry listeners also receive the RetryContext on open, close, and onError callbacks, making it the central coordination object for cross-cutting retry concerns.
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...
