春天猫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_春天重试,因为冬天来了的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓外星人a16(安卓外星人)
- 下一篇: react 事件处理_在React中处理