国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Java基于Guava Retrying實(shí)現(xiàn)重試功能

瀏覽:3日期:2022-08-29 16:49:36

在接口調(diào)用中由于各種原因,可能會重置失敗的任務(wù),使用Guava-Retrying可以方便的實(shí)現(xiàn)重試功能。

首先,需要引用Guava-Retrying的包

<dependency> <groupId>com.github.rholder</groupId> <artifactId>guava-retrying</artifactId> <version>2.0.0</version></dependency>

代碼示例:

import com.github.rholder.retry.Retryer;import com.github.rholder.retry.RetryerBuilder;import com.github.rholder.retry.StopStrategies;import com.google.common.base.Predicates;import java.util.concurrent.TimeUnit;import static com.github.rholder.retry.WaitStrategies.incrementingWait;/** * @author wangxuexing * @descrption * @date */public class RetryDemo { public static void main(String[] args) { Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder(). //如果異常會重試 retryIfException(). //如果結(jié)果為false會重試 retryIfResult(Predicates.equalTo(false)). //重調(diào)策略 withWaitStrategy(incrementingWait(30, TimeUnit.SECONDS, 30, TimeUnit.SECONDS)). //嘗試次數(shù) withStopStrategy(StopStrategies.stopAfterAttempt(3)). //注冊監(jiān)聽 withRetryListener(new MyRetryListener()).build(); try { retryer.call(new TaskCallable()); } catch (Exception e) { e.printStackTrace(); } }}

其中TaskCallable是任務(wù)的具體實(shí)現(xiàn)類,它實(shí)現(xiàn)了Callable接口

import java.util.concurrent.Callable;/** * @author wangxuexing * @descrption * @date */public class TaskCallable implements Callable<Boolean> { public Boolean call() throws Exception { return false; }}

另外,MyRetryListener監(jiān)聽實(shí)現(xiàn)了RetryListener接口,每次重試都會回調(diào)注冊的監(jiān)聽

import com.github.rholder.retry.Attempt;import com.github.rholder.retry.RetryListener;/** * @author wangxuexing * @descrption * @date */public class MyRetryListener implements RetryListener { public <V> void onRetry(Attempt<V> attempt) { System.out.print('[retry]time=' + attempt.getAttemptNumber()); // 距離第一次重試的延遲 System.out.print(',delay=' + attempt.getDelaySinceFirstAttempt()); // 重試結(jié)果: 是異常終止, 還是正常返回 System.out.print(',hasException=' + attempt.hasException()); System.out.print(',hasResult=' + attempt.hasResult()); // 是什么原因?qū)е庐惓? if (attempt.hasException()) { System.out.print(',causeBy=' + attempt.getExceptionCause().toString()); } else {// 正常返回時的結(jié)果 System.out.print(',result=' + attempt.getResult()); } System.out.println(); }}

執(zhí)行一下main方法,可以看到執(zhí)行的結(jié)果:

[retry]time=1,delay=0,hasException=false,hasResult=true,result=false[retry]time=2,delay=30000,hasException=false,hasResult=true,result=false[retry]time=3,delay=90000,hasException=false,hasResult=true,result=falsecom.github.rholder.retry.RetryException: Retrying failed to complete successfully after 3 attempts.at com.github.rholder.retry.Retryer.call(Retryer.java:174)at test.retryer.RetryDemo.main(RetryDemo.java:32)

下面詳細(xì)分析一下:

RetryerBuilder是一個factory創(chuàng)建者,可以定制設(shè)置重試源且可以支持多個重試源,可以配置重試次數(shù)或重試超時時間,以及可以配置等待時間間隔,創(chuàng)建重試者Retryer實(shí)例。

RetryerBuilder的重試源支持Exception異常對象 和自定義斷言對象,通過retryIfException 和retryIfResult設(shè)置,同時支持多個且能兼容。 retryIfException,拋出runtime異常、checked異常時都會重試,但是拋出error不會重試。 retryIfRuntimeException只會在拋runtime異常的時候才重試,checked異常和error都不重試。 retryIfExceptionOfType允許我們只在發(fā)生特定異常的時候才重試,比如NullPointerException和IllegalStateException都屬于runtime異常,也包括自定義的error retryIfResult可以指定你的Callable方法在返回值的時候進(jìn)行重試

StopStrategy:停止重試策略,提供三種:

StopAfterDelayStrategy 設(shè)定一個最長允許的執(zhí)行時間;比如設(shè)定最長執(zhí)行10s,無論任務(wù)執(zhí)行次數(shù),只要重試的時候超出了最長時間,則任務(wù)終止,并返回重試異常RetryException。

NeverStopStrategy 不停止,用于需要一直輪訓(xùn)知道返回期望結(jié)果的情況。

StopAfterAttemptStrategy 設(shè)定最大重試次數(shù),如果超出最大重試次數(shù)則停止重試,并返回重試異常。

WaitStrategy:等待時長策略(控制時間間隔),返回結(jié)果為下次執(zhí)行時長: FixedWaitStrategy 固定等待時長策略。 RandomWaitStrategy 隨機(jī)等待時長策略(可以提供一個最小和最大時長,等待時長為其區(qū)間隨機(jī)值)。 IncrementingWaitStrategy 遞增等待時長策略(提供一個初始值和步長,等待時間隨重試次數(shù)增加而增加)。 ExponentialWaitStrategy 指數(shù)等待時長策略。 FibonacciWaitStrategy Fibonacci 等待時長策略。 ExceptionWaitStrategy 異常時長等待策略。 CompositeWaitStrategy 復(fù)合時長等待策略。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 亚洲精品区在线播放一区二区 | 92看片淫黄大片看国产片 | 永久精品免费影院在线观看网站 | 一区二区三区四区在线视频 | 国产精品亚洲专区在线播放 | 自拍偷拍欧美视频 | 欧美一级毛片免费看视频 | 国产午夜爽爽窝窝在线观看 | 高清视频 一区二区三区四区 | 亚洲视屏在线观看 | 日韩三级中文字幕 | 亚洲国产精品一区二区不卡 | 成人一级黄色片 | 亚洲精品久久精品h成人 | 亚洲国产福利精品一区二区 | 日本三级在线观看中文字 | 香蕉视频在线观看黄 | 久国产 | 国产精品中文字幕在线观看 | 爱爱毛片 | 日韩美女免费线视频 | 免费一级毛片在播放视频 | 亚洲精品欧美精品中文字幕 | 久久精品综合 | 91精品国产91热久久p | 韩国三级日本三级香港三级黄 | 国产一级片在线 | 欧美亚洲国产人成aaa | 欧美一级毛片免费高清的 | 国产一国产a一级毛片 | 亚洲免费观看网站 | 性欧美欧美巨大69 | 亚洲国产精久久久久久久 | 国产精品免费看久久久久 | 91久久亚洲精品一区二区 | 久久久久久久久久免费视频 | 国产男女乱淫真视频全程播放 | 最近韩国日本免费免费版 | 亚洲国产精品成人午夜在线观看 | 亚洲精品一区二区三区在线观看 | 97干干干 |