Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Spring-Retry

Shulou Source: shulou.com Published: 2022-06-02 15:54:52 12月01日 Update

How to use Spring-Retry, in view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Brief introduction of 1 Spring-Retry

In some daily scenarios, there are many operations that require retry. Spring-retry is a spring-based retry framework provided by spring, which is very simple and easy to use.

Application in 2 Spring

1 Import maven coordinates

Org.springframework.retry spring-retry 1.2.2.RELEASE 2 add called class

@ Slf4jpublic class RetryDemo {public static boolean retryMethod (Integer param) {int I = new Random () .nextInt (param); log.info ("randomly generated number: {}", I); if (1 = = I) {log.info ("is 1, returns true."); return true;} else if (I)

< 1) { log.info("小于1,抛出参数异常."); throw new IllegalArgumentException("参数异常"); } else if (i >

1 & & I < 10) {log.info ("> 1, < 10, throw parameter exception."); return false;} else {/ / for other log.info ("> 10, throw custom exception."); throw new RemoteAccessException ("> 10, throw custom exception");} 3 add test class

@ Slf4jpublic class SpringRetryTest {/ * retry interval ms, default 1000ms * / private long fixedPeriodTime = 1000L; / * maximum number of retries, default is 3 * / private int maxRetryTimes = 3; / * indicates which exceptions need to be retried * key must be a subclass of Throwable exception Class

Tags: Policies methods times times annotations results maximum parameters hibernation composition objects interfaces generation multipliers source code problems delays differences functions at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL Xiaomi Huawei Shulou Information