This article introduces the method of Spring retrying to support Spring Retry. It is shared with you. The details are as follows:
The first step is to introduce maven dependencies
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version></parent><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry --><dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> <version>1.1.2.RELEASE</version></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.6</version></dependency>
Step 2: Add @Retryable and @Recover annotations
package hello;import org.springframework.remoting.RemoteAccessException;import org.springframework.retry.annotation.Backoff;import org.springframework.retry.annotation.Recover;import org.springframework.retry.annotation.Recover;import org.springframework.retry.annotation.Retryable;import org.springframework.stereotype.Service;@Servicepublic class RemoteService {@Retryable(value= {RemoteAccessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000l,multiplier = 1))public void call() throws Exception { System.out.println("do something..."); throw new RemoteAccessException("RPC call exception");}@Recoverpublic void recover(RemoteAccessException e) { System.out.println(e.getMessage());}} @Retryable annotation <br />If the annotated method has an exception, it will be tried again
value: Specify that the exception occurred and try again
include: Like value, the default is empty. When the exclude is also empty, all exceptions will be tried again
exclude: Specify that the exception does not retry, the default is empty. When include is also empty, all exceptions will be retryed.
maxAttemps: Number of retry times, default 3
backoff: Retry the compensation mechanism, no default
@Backoff annotation
delay: Specify delay and try again
multiplier: Specify multiples of the delay, such as delay=5000l, multiplier=2, after the first retry is 5 seconds, the second time is 10 seconds, and the third time is 20 seconds.
@Recover
When the retry reaches the specified number of times, the annotated method will be called back, and log processing can be performed in this method. It should be noted that the callback will only occur if the exception occurs and the parameter type is the same.
Step 3: Start the container and test in SpringBoot
Add @EnableRetry annotation to enable retry function
package hello;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.retry.annotation.EnableRetry;@SpringBootApplication@EnableRetrypublic class Application { public static void main(String[] args) throws Exception { ApplicationContext annotationContext = new AnnotationConfigApplicationContext("hello"); RemoteService remoteService = annotationContext.getBean("remoteService", RemoteService.class); remoteService.call(); }} Running results:
16:50:51.012 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=0
do something…
16:50:51.025 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:50:56.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=1
16:50:56.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=1
do something…
16:50:56.026 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:51:01.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=2
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=2
do something…
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=3
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry failed last attempt: count=3
RPC call exception
Reference: https://github.com/spring-projects/spring-retry
Replenish
For non-idempotent requests (such as new or updated operations), do not use retry, as it will have a great impact on data consistency.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.