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.
More Related questions...