Using Spring Retry @Retryable for Automatic Retry in Spring Boot Applications
This article introduces Spring Retry's @Retryable annotation, explains its purpose, shows how to add the Maven dependency, enable retry support, annotate methods with retry settings, handle failures with @Recover, and outlines important considerations for reliable retry mechanisms in Java backend development.
In many real‑world scenarios such as message sending failures, remote service call failures, or lock contention, retrying the operation after a short wait can resolve transient issues caused by network fluctuations.
Instead of writing repetitive try/catch or while loops, Spring Retry provides a declarative way to implement retries without intruding on existing business logic.
@Retryable Overview
Spring Retry is a utility module of the Spring ecosystem that enables standardized retry handling for any operation. All configuration is based on simple annotations.
Usage Steps
1. Add Maven Dependency
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>2. Enable Retry Support
@EnableRetry
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}3. Annotate Business Method with @Retryable
import com.mail.elegant.service.TestRetryService;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import java.time.LocalTime;
@Service
public class TestRetryServiceImpl implements TestRetryService {
@Override
@Retryable(value = Exception.class, maxAttempts = 3,
backoff = @Backoff(delay = 2000, multiplier = 1.5))
public int test(int code) throws Exception {
System.out.println("test called, time: " + LocalTime.now());
if (code == 0) {
throw new Exception("Invalid condition!");
}
System.out.println("test called, condition OK!");
return 200;
}
}The annotation parameters mean:
value : the exception type that triggers a retry.
include : similar to value , defaults to all exceptions when empty.
exclude : exceptions that should not be retried.
maxAttempts : maximum number of retry attempts (default 3).
backoff : retry waiting strategy, using @Backoff with delay (base wait time) and multiplier (exponential factor).
4. Handle Exhausted Retries with @Recover
@Recover
public int recover(Exception e, int code) {
System.out.println("Recovery method executed!");
// log to DB or perform alternative actions
return 400;
}The recovery method must be in the same class as the @Retryable method, return the same type, and its first parameter must be the exception type defined in @Retryable.
5. Important Notes
Retry is implemented via AOP, so self‑invocation within the same class will not trigger retries.
If you need post‑retry processing via @Recover, the retrying method should return void when the recovery method does not return a value.
Do not use try/catch inside the retrying method; let the exception propagate so Spring Retry can intercept it.
The @Recover method’s first argument must match the exception type specified in @Retryable.
Conclusion
This article demonstrated the basic usage of Spring Boot's @Retryable annotation, its typical scenarios, and key considerations, showing that it is a handy tool when retry logic is required in backend services.
Additional Resources
The author offers PDF collections of Spring Cloud, Spring Boot, and MyBatis advanced topics via a public WeChat account. Readers are encouraged to like, share, and follow for more technical content.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.