1

retryWhen複数を追加して再試行を実行し、さまざまな WebClient の失敗応答を処理することはできますか?

私が達成したいこと:

WebClient を使用して REST API 呼び出しを行っています。エラーのシナリオはほとんどありません。再試行が必要な場合は遅延が異なります。

たとえば、1.発生した場合401 Unauthorize、トークンを更新した直後に再試行できます。2.502/503 Serverエラーが発生した場合、5 秒後に再試行を遅らせる必要があります。3.429 Too Many Request発生した場合は、再試行を少し遅らせる必要があります。たとえば、20 秒後です。

以下のような Retry 仕様を作成したいと思います。

    protected static final Predicate<Throwable> is401 =
                (throwable) -> throwable instanceof WebClientResponseException.Unauthorized;

        protected static final Predicate<Throwable> is5xx =
                (throwable) -> throwable instanceof WebClientResponseException.ServiceUnavailable;

        protected static final Predicate<Throwable> is429 =
                (throwable) -> throwable instanceof WebClientResponseException.TooManyRequests;

        Retry retry401 = Retry.fixedDelay(5, Duration.ofSeconds(1))
                    .filter(is401)
                    .onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> retrySignal.failure());

        Retry retry5xx = Retry.fixedDelay(5, Duration.ofSeconds(10))
                    .filter(is5xx)
                    .onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> retrySignal.failure());

        Retry retry429 = Retry.fixedDelay(5, Duration.ofSeconds(20))
                    .filter(is429)
                    .onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> retrySignal.failure());

// trying to apply the to WebClient like below:
    WebClient.Builder()
        .get()
        .uri("endpointuri")
        .retrieve()
        .bodyToFlux(String.class)
        .retryWhen(retry401)
        .retryWhen(retry5xx)
        .retryWhen(retry429);

`.retryWhen(retry429)' は他の再試行を上書きするようです。

4

1 に答える 1