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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

春天猫rtsy_春天重试,因为冬天来了

發布時間:2023/12/3 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 春天猫rtsy_春天重试,因为冬天来了 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

春天貓rtsy

好的,這實際上與冬天無關,眾所周知,冬天已經到了 。 它與Spring Retry有關,Spring Retry是一個小的Spring框架庫,它使我們可以向應重試的任何任務添加重試功能。

這里有一個很好的教程 ,解釋了如何設置簡單的重試和恢復。 它很好地解釋了如何添加spring-retry依賴項 ,使用@Retryable和@Recover批注以及將RetryTemplate與簡單策略一起使用。 當我們實際上想根據異常的類型應用不同的重試行為時,我想講的是一個稍微復雜的情況。 這是有道理的,因為我們可能知道某些異常是可恢復的,而某些異常是不可恢復的,因此嘗試從異常中恢復并沒有太大的意義。 為此,有一個特定的重試策略實現,稱為ExceptionClassifierRetryPolicy ,與Spring RetryTemplate一起使用 。

假設我們只能從IO異常中恢復并跳過所有其他異常 。 我們將創建三個類來擴展RetryCallback,并創建一個類來擴展RecoveryCallback以更好地顯示內部發生的情況:

private class SuccessCallback implements RetryCallback<Boolean, RuntimeException> {@Overridepublic Boolean doWithRetry(RetryContext context) throws RuntimeException {System.out.println("Success callback: attempt " + context.getRetryCount());return true;}}private class ExceptionCallback implements RetryCallback<Boolean, Exception> {@Overridepublic Boolean doWithRetry(RetryContext context) throws Exception {System.out.println("Exception callback: attempt " + context.getRetryCount());throw new Exception("Test Exception");}}private class SpecificExceptionCallback implements RetryCallback<Boolean, IOException> {@Overridepublic Boolean doWithRetry(RetryContext context) throws IOException {System.out.println("IO Exception callback: attempt " + context.getRetryCount());throw new IOException("Test IO Exception");}}private class LoggingRecoveryCallback implements RecoveryCallback<Boolean> {@Overridepublic Boolean recover(RetryContext context) throws Exception {System.out.println("Attempts exhausted. Total: " + context.getRetryCount());System.out.println("Last exception: " + Optional.ofNullable(context.getLastThrowable()).orElse(new Throwable("No exception thrown")).getMessage());System.out.println("\n");return false;}}

然后,我們設置RetryTemplate 。 我們將使用SimpeRetryPolicy和IOException的固定嘗試次數,以及一個NeverRetryPolicy ,它僅允許對其他所有事物進行初始嘗試。

*We want to retry on IOException only.Other Exceptions won't be retried.IOException will be retried three times, counting the initial attempt.*/final ExceptionClassifierRetryPolicy exRetryPolicy = new ExceptionClassifierRetryPolicy();exRetryPolicy.setPolicyMap(new HashMap<Class<? extends Throwable>, RetryPolicy>() {{put(IOException.class, new SimpleRetryPolicy(3));put(Exception.class, new NeverRetryPolicy());}});retryTemplate.setRetryPolicy(exRetryPolicy);

現在,我們需要使用這些回調來演示它們如何工作。 首先成功執行,這很簡單:

// we do not catch anything hereSystem.out.println("\n*** Executing successfull callback...");retryTemplate.execute(new SuccessCallback(), new LoggingRecoveryCallback());

其輸出如下:

*** Executing successfull callback... Success callback: attempt 0

然后是異常

// we catch Exception to allow the program to continueSystem.out.println("\n*** Executing Exception callback...");try {retryTemplate.execute(new ExceptionCallback(), new LoggingRecoveryCallback());} catch (Exception e) {System.out.println("Suppressed Exception");}*** Executing Exception callback... Exception callback: attempt 0 Attempts exhausted. Total: 1 Last exception: Test Exception

最后是我們的IOException

// we catch IOException to allow the program to continueSystem.out.println("\n*** Executing IO Exception callback...");try {retryTemplate.execute(new SpecificExceptionCallback(), new LoggingRecoveryCallback());} catch (IOException e) {System.out.println("Suppressed IO Exception");}*** Executing IO Exception callback... IO Exception callback: attempt 0 IO Exception callback: attempt 1 IO Exception callback: attempt 2 Attempts exhausted. Total: 3 Last exception: Test IO Exception

如我們所見,只有IOException發起了三個嘗試。 請注意,嘗試次數從0開始編號,因為執行回調時嘗試次數并未耗盡,因此上一次嘗試次數為#2,而不是#3。 但是在RecoveryCallback上所有嘗試均已用盡,因此上下文保留3次嘗試。

我們還可以看到,嘗試成功后未調用RecoveryCallback 。 也就是說,僅當執行以異常結束時才調用它。

RetryTemplate是同步的,因此所有執行都在我們的主線程中進行。 這就是為什么我在調用周圍添加了try / catch塊的原因,以使程序可以毫無問題地運行所有三個示例。 否則,重試策略將在上次失敗嘗試后將異常拋出,并停止執行。

還有一個非常有趣的CompositeRetryPolicy ,它允許添加幾個策略并委托來依次調用它們。 它還可以允許創建相當靈活的重試策略,但這本身就是另一個主題。

我認為spring-retry是一個非常有用的庫,它可以使常見的可重試任務更可預測,可測試且更易于實現。

翻譯自: https://www.javacodegeeks.com/2017/07/spring-retry-winter-coming.html

春天貓rtsy

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的春天猫rtsy_春天重试,因为冬天来了的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。