spring boot retry

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.retry</groupId>
			<artifactId>spring-retry</artifactId>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>



import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.annotation.Retryable;

@Configuration
@EnableRetry
public class Application {



}


import java.time.LocalTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
public class RemoteService {
    
    private final static Logger logger = LoggerFactory.getLogger(RemoteService.class);
    
    @Retryable(value = { RemoteAccessException.class }, maxAttempts = 3, backoff = @Backoff(delay = 5000l, multiplier = 1))
    public int call() throws Exception {
        logger.info(LocalTime.now()+" do something...");
// throw new RemoteAccessException("RPC call exception");
        throw new RuntimeException("run errer");
    }

    @Recover
    public int recover(RemoteAccessException e) {
        logger.info("================10=========================="+e.getMessage());

        return 10;
    }

    @Recover
    public int recover(RuntimeException e) {
        logger.info("============12=============================="+e.getMessage());
        return 12;
    }

    @Recover
    public int recover(Exception e) {
        logger.info("============11=============================="+e.getMessage());
        return 11;
    }
}




@Retryable
annotation The annotated method will be retried when an exception occurs
value: specify the exception to be retried
include: same as value, the default is empty, when the exclude is also empty, all exceptions will be retried
exclude: the specified exception will not be retried , the default is empty, when include is also empty, all exceptions are retried
maxAttemps: number of retries, default 3
backoff: retry compensation mechanism, no

@Backoff annotation by default
delay: retry after a specified delay
multiplier: specify a multiple of the delay, For example, when delay=5000l, multiplier=2, the first retry is 5 seconds, the second is 10 seconds, and the third is 20 seconds

@Recover
When the retry reaches the specified number of times, the annotated method will be called back , you can perform log processing in this method. It should be noted that the callback will only be called when the exception that occurs is of the same type as the input parameter.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326214684&siteId=291194637