設定

リトライ有効化

SpringBootの起動クラスに@EnableRetryをつけておくでOK。

リトライ対象メソッド

失敗する可能性がある処理に以下のアノテーションを付与(AOPなのでfinalなクラスには使えない)。

@Retryable(value = {BarException.class, BazException.class}, maxAttempts = 10, backoff = @Backoff(delay = 500))

リカバリー

RetryTemplateを使う場合

   @GetMapping("/retry")
   public TestResponse retry() {
       RetryTemplate retryTemplate = new RetryTemplate();
       // リトライ設定
       Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<>();
       retryableExceptions.put(RuntimeException.class, true);
       SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(3,retryableExceptions);
       retryTemplate.setRetryPolicy(retryPolicy);
       // リトライ付き処理
       retryTemplate.execute(context -> {
           System.out.println("リトライ前");
           throw new RuntimeException();
       });
       return new TestResponse();
   }

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2022-10-14 (金) 13:16:53