Master Spring Retry: Using @Retryable for Elegant Failure Handling
This guide explains how Spring Boot’s @Retryable annotation enables elegant, annotation‑driven retry logic, covering dependency setup, enabling retry, method annotation with configurable parameters, recovery handling via @Recover, and important AOP‑related considerations to avoid common pitfalls.
In real‑world applications, retrying failed operations such as message sending, remote service calls, or lock acquisition is common, and Spring Retry provides a non‑intrusive, annotation‑based solution.
@Retryable Overview
Spring Retry is a utility module that standardizes retry handling via simple annotations, allowing developers to add retry logic without modifying existing business code.
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 succeeded!");
return 200;
}
}Key @Retryable parameters:
value : specifies which exception types trigger a retry.
include : similar to value; empty means all exceptions when exclude is also empty.
exclude : exceptions that should not be retried.
maxAttempts : maximum number of retry attempts (default 3).
backoff : retry delay strategy; delay sets the initial pause, multiplier defines exponential growth.
4. Define a Recovery Method 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 have the same return type as the @Retryable method, the first parameter must be the exception type, and it must reside in the same class.
5. Important Considerations
Retry is implemented via AOP; self‑invocation of methods in the same class will not trigger retries.
If you need post‑retry processing with @Recover, the retry method should return void when you only care about the callback.
Avoid using try‑catch inside the retry method; let exceptions propagate to the retry mechanism.
The @Recover method’s exception parameter must match the exception configured in @Retryable.
Conclusion
The article demonstrates how to use Spring Boot’s @Retryable annotation for reliable retry handling, outlines the necessary configuration steps, explains each parameter, shows how to implement a recovery callback, and highlights key pitfalls to ensure correct AOP‑based behavior.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
