SpringBootの起動クラスに@EnableRetryをつけておくでOK。
失敗する可能性がある処理に以下のアノテーションを付与(AOPなのでfinalなクラスには使えない)。
@Retryable(value = {BarException.class, BazException.class}, maxAttempts = 10, backoff = @Backoff(delay = 500))
@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(); }