SoFunction
Updated on 2025-04-14

Detailed explanation of SpringBoot's operation using spring retry retry mechanism

Retry needs to be designed according to the application scenario in functional design. The interface for reading data is more suitable for retry scenarios. The interface for writing data needs to pay attention to the idempotence of the interface. In addition, if the number of retry times is too large, it will double the number of requests, which will put greater pressure on the backend. Setting up a reasonable retry mechanism is the key;

Spring Retry Retry Framework

Spring boot uses spring retry retry mechanism

Quote

<dependency>
  <groupId></groupId>
  <artifactId>spring-retry</artifactId>
</dependency>

2. Open retry by application startup class

@EnableRetry
public class Application {
    .......
}

3. Mark @Retryable on the specified method to enable retry

@Retryable(value={},maxAttempts=5,backoff = @Backoff(delay = 2000,multiplier = 1.5))
public void retryTest() throws Exception {
    (().getName()+" do something...");
    throw new RuntimeException();
}

value:Specify that the exception occurs and try again

include:Like value, the default is empty. When exclude is also empty, all exceptions will be retryed.

exclude:Specify that the exception does not retry, the default is empty. When the include is also empty, all exceptions will be retryed.

maxAttemps:The number of retry times, default 3

backoff:Retry the compensation mechanism, no default

4. Mark @Recover on the specified method to enable the method called after retry failure (note that the reprocessing method needs to be in the same class)

@Recover
 public void recover(RuntimeException e) {
   // ... do something
 }

Detailed explanation of usage
spring-retry uses AOP to encapsulate the target method and execute it under the current thread, so the current thread will be blocked during retry. If the BackOff time is set for a long time, it is best to start the asynchronous thread and try again (you can also add @Async annotation).

@Retryable annotation

Retry again when an annotated method occurs

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.

spring-retry

1. Possible reasons why @Retryable does not take effect

@Retryable method must be public

In the following case, @Retryable does not take effect, that is, the retry method is in the same class as the non-retry method that calls it.

// Note that this method does not take effect!  !  !@EnableRetry(proxyTargetClass = true)
class test{
	public void methodA(){
		methodB();
	}
  @Retryable
  public void methodB(){
  
  }
}

Solution:A Service was written separately to the retry method.

3. There is only one retry method for an exception in each class. Two retry methods capture Exception, and retry fails.

2. Possible reasons why @Recover is not effective

① The return value must be consistent with the return value of the retry function;
② Except for the first one of the parameters, the subsequent parameters need to be consistent with the parameter list of the retry function;
③Of course, the return value part here can also be manually retryed again, but it has failed so many times, so it is not very meaningful to do it again in the guarantee function. Therefore, my consideration is to use it for logging here.

The above is a detailed explanation of the operation of SpringBoot using spring retry retry mechanism. For more information about SpringBoot using spring retry, please follow my other related articles!