Master Spring Retry: How @Retryable Simplifies Robust Error Handling
This article explains how Spring Retry's @Retryable and @Recover annotations provide a clean, annotation‑driven way to handle common retry scenarios such as message failures, remote service errors, and lock acquisition issues, while outlining configuration steps, parameter meanings, and important usage cautions.
In real work, retry is common for scenarios such as message sending failure, remote service call failure, or lock acquisition failure, often caused by network fluctuations.
Instead of using manual try/catch or loops, Spring Retry provides annotation‑driven retry with @Retryable, which can be added without invasive changes to business code.
What is @Retryable?
Spring‑Retry is a utility module that standardizes retry handling. All configuration is based on simple annotations.
How to use
1. Add Maven dependency
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>2. Enable retry
@EnableRetry
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}3. Annotate the method
@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 succeeded!");
return 200;
}
}4. Parameters of @Retryable
value: exception types that trigger retry. include: same as value, default empty (all exceptions if exclude also empty). exclude: exceptions that should not be retried. maxAttempts: maximum retry attempts, default 3. backoff: retry delay strategy, using @Backoff with delay and multiplier.
What happens after retries are exhausted?
Spring‑Retry passes control to a RecoveryCallback. You can define a method annotated with @Recover to handle the failure.
5. @Recover example
@Recover
public int recover(Exception e, int code) {
System.out.println("Recovery method executed!");
// log or other handling
return 400;
}Important notes
Based on AOP, it does not support self‑invocation.
If you need post‑retry handling, the retry method must return void when using @Recover.
Do not use try/catch inside the retry method; let exceptions propagate.
The @Recover method must have the same return type as the @Retryable method and its first parameter must be the exception type defined in @Retryable.
This article introduces the usage of Spring Boot’s Retryable annotation, its typical scenarios, and precautions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
