日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring-retry----线程内重试

發(fā)布時間:2025/4/5 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring-retry----线程内重试 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

第一步、引入maven依賴

<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>
  • ?

第二步、添加@Retryable和@Recover注解

package hello;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 { @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調(diào)用異常"); } @Recover public void recover(RemoteAccessException e) {System.out.println(e.getMessage()); } }
  • ?

@Retryable注解?
被注解的方法發(fā)生異常時會重試?
value:指定發(fā)生的異常進行重試?
include:和value一樣,默認空,當exclude也為空時,所有異常都重試?
exclude:指定異常不重試,默認空,當include也為空時,所有異常都重試?
maxAttemps:重試次數(shù),默認3?
backoff:重試補償機制,默認沒有

@Backoff注解?
delay:指定延遲后重試?
multiplier:指定延遲的倍數(shù),比如delay=5000l,multiplier=2時,第一次重試為5秒后,第二次為10秒,第三次為20秒

@Recover?
當重試到達指定次數(shù)時,被注解的方法將被回調(diào),可以在該方法中進行日志處理。需要注意的是發(fā)生的異常和入?yún)㈩愋鸵恢聲r才會回調(diào)

第三步、SpringBoot方式啟動容器、測試

添加@EnableRetry注解,啟用重試功能

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 @EnableRetry public class Application {public static void main(String[] args) throws Exception {ApplicationContext annotationContext = new AnnotationConfigApplicationContext("hello");RemoteService remoteService = annotationContext.getBean("remoteService", RemoteService.class);remoteService.call();} }
  • 運行結果:?
  • 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調(diào)用異常

參考?
https://github.com/spring-projects/spring-retry

?

這邊有個坑:僅僅對類的直接調(diào)用方法有效,方法內(nèi)部調(diào)用自己內(nèi)部方法不會被代理aop和重試

轉載于:https://my.oschina.net/91jason/blog/1512059

總結

以上是生活随笔為你收集整理的spring-retry----线程内重试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。