成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術文章
文章詳情頁

Java基于Guava Retrying實現重試功能

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

在接口調用中由于各種原因,可能會重置失敗的任務,使用Guava-Retrying可以方便的實現重試功能。

首先,需要引用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(). //如果結果為false會重試 retryIfResult(Predicates.equalTo(false)). //重調策略 withWaitStrategy(incrementingWait(30, TimeUnit.SECONDS, 30, TimeUnit.SECONDS)). //嘗試次數 withStopStrategy(StopStrategies.stopAfterAttempt(3)). //注冊監聽 withRetryListener(new MyRetryListener()).build(); try { retryer.call(new TaskCallable()); } catch (Exception e) { e.printStackTrace(); } }}

其中TaskCallable是任務的具體實現類,它實現了Callable接口

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

另外,MyRetryListener監聽實現了RetryListener接口,每次重試都會回調注冊的監聽

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()); // 重試結果: 是異常終止, 還是正常返回 System.out.print(',hasException=' + attempt.hasException()); System.out.print(',hasResult=' + attempt.hasResult()); // 是什么原因導致異常 if (attempt.hasException()) { System.out.print(',causeBy=' + attempt.getExceptionCause().toString()); } else {// 正常返回時的結果 System.out.print(',result=' + attempt.getResult()); } System.out.println(); }}

執行一下main方法,可以看到執行的結果:

[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)

下面詳細分析一下:

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

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

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

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

NeverStopStrategy 不停止,用于需要一直輪訓知道返回期望結果的情況。

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 免费黄色网址在线播放 | 6一12呦女精品 | 怡红院视频在线 | 日韩一级欧美一级毛片在 | 国产一级黄毛片 | 亚洲性欧美 | 91久久香蕉国产线看观看软件 | 三级毛片网站 | 亚洲国产高清视频 | 久久一二 | 欧美一级片在线看 | 67194欧美成l人在线观看免费 | 久久这里只有精品免费播放 | 国产微拍精品福利视频 | 美国一级特a黄 | 九九99久久 | 日韩精品在线一区二区 | 九九热久久免费视频 | 京东一热本色道久久爱 | 国产在线黄 | 女人张开腿让男人 | 欧美成人a人片 | 日本男人的天堂 | 99精品视频免费观看 | 成人毛片1024你懂的 | 在线看精品 | 中文字幕 日韩在线 | 中文字幕在线欧美 | 亚洲高清二区 | 久久是精品 | 日本一级毛片免费 | 午夜精品视频 | 国产情侣久久精品 | 美女和男人免费网站视频 | 91伊人久久 | 日本人在线看片 | 成年女人毛片免费播放视频m | 鲁老汉精品视频在线观看 | 久久国产精品99久久小说 | 欧美成人免费tv在线播放 | 欧美精品色视频 |