Spring / Spring Retry Interview Questions
What is the @Retryable annotation and what are its key attributes?
The @Retryable annotation is placed on a Spring-managed bean method to declare that it should be retried automatically when it throws a specified exception. Spring Retry wraps the method in an AOP proxy and applies the configured retry policy transparently to the caller.
Key attributes of @Retryable:
| Attribute | Default | Description |
|---|---|---|
| value / retryFor | Exception.class | Exception types that trigger a retry |
| noRetryFor | – | Exception types that must NOT trigger a retry |
| maxAttempts | 3 | Total number of attempts including the first try |
| backoff | no delay | A nested @Backoff for delay between retries |
| listeners | – | Names of RetryListener beans to notify |
| recover | – | Name of a specific @Recover method to use |
Example:
@Retryable( retryFor = { HttpServerErrorException.class }, maxAttempts = 4, backoff = @Backoff(delay = 1000, multiplier = 2) ) public String callExternalService() { return restTemplate.getForObject(url, String.class); }
This retries up to 4 times on HttpServerErrorException with an exponential backoff starting at 1 second.
More Related questions...