Spring / Spring Retry Interview Questions
How does Spring Retry handle the case when @Recover returns void?
A @Recover method can return void if and only if the corresponding @Retryable method also returns void. Spring Retry's recovery method matching requires that the return types align. If there is a mismatch, Spring Retry will not find the recovery method and will rethrow the final exception.
Correct void recover example:
@Retryable(retryFor = JmsException.class, maxAttempts = 3)
public void sendMessage(String payload) {
jmsTemplate.convertAndSend("queue.orders", payload);
}
@Recover
public void recoverSendMessage(JmsException ex, String payload) {
log.error("Failed to send message after retries: {}", payload, ex);
deadLetterStore.save(payload); // persist for manual reprocessing
}In this case, since sendMessage returns void, the recover method also returns void. When recovery is invoked, Spring simply calls the method for its side effects (logging, storing) and then returns normally to the caller — the caller sees no exception.
If sendMessage returned a value (e.g., String) and the recover method was declared as void, the recovery would not be matched and the exception would propagate. Always align return types precisely, including generic type parameters.
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...
