🤞 personal home page: @Qingcheng sequence member Stone 🤞 fan benefits: plus one to one fan group to answer questions, get free rich resume template, improve learning materials, do a good job in the new era of volume king!
For many businesses, in order to make processing more robust and reduce the likelihood of failure, it sometimes helps to automatically retry failed operations, using a mechanism that may enable our operations to succeed on subsequent attempts. In this article, we’ll learn how to use the Spring Retry feature in a Spring application.
I. Project configuration
To enable Spring Retry support, first add the following dependencies to the POM.xml file
<properties> <version.spring. Retry >1.3.1</version.spring. Retry > </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> <version>${version.spring.retry}</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> </dependencies>Copy the code
Here we use the latest artifact in Maven’s central repository, 1.3.1. In addition to its own dependencies, proper use of Spring Retry requires a dependency on AOP. For Spring Boot projects, add spring-boot-starter-AOP starter to pom.xml
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
Copy the code
Enable Spring Retry support
To enable Spring Retry functionality, add the @enableretry annotation to the configuration class.
@SpringBootApplication @EnableRetry public class Launcher { public static void main(String[] args) { SpringApplication.run(Launcher.class, args); }}Copy the code
There are two common ways to use the Retry function, including annotating the @retryable method and manually executing the RetryTemplate policy.
Iii. Form of annotations by @retryable
3.1 @ Retryable
The annotation approach is to use the @retryable annotation on methods that enable the retry feature.
We provide an interface, provide a method that needs to be retried, and annotate that method.
Public interface RetryService {/** * CustomRetryException Retry. The maximum number of retries is 4 (the default is 3). The retry compensation mechanism is 200 milliseconds. Default is empty * @ return result * @ throws CustomRetryException specify exceptions * / @ Retryable (value = {CustomRetryException. Class}, maxAttempts = 4,backoff = @Backoff(200)) String retry() throws CustomRetryException; }Copy the code
Annotation parameter assignments are explained in method comments.
- valueProperty tells Spring to retry in the method
CustomRetryException
Retry is triggered when an exception occurs. - MaxAttempts Sets the maximum number of retries. If no value is specified, the default value is 3.
- Backoff Specifies the delay for the next retry. The default value is 1 second.
CustomRetryException is a custom exception. The code is as follows
public class CustomRetryException extends Exception{ public CustomRetryException(String error){ super(error); }}Copy the code
3.2 @ Recover
The @Recover annotation used to define a separate recovery method when a method annotated by @retryable fails due to a specified exception.
@Service @Slf4j public class RetryServiceImpl implements RetryService { private static int count = 1; @Override public String retry() throws CustomRetryException { log.info("retry{},throw CustomRetryException in method retry",count); count ++; throw new CustomRetryException("throw custom exception"); } @Recover public String recover(Throwable throwable) { log.info("Default Retry service test"); return "Error Class :: " + throwable.getClass().getName(); }}Copy the code
Boy, haven’t you seen enough? Click on the details of the stone, casually have a look, maybe there is a surprise? Welcome to support the likes/attention/comments, your support is my biggest motivation, thank you!