是否可以根据 HttpStatus Code 自定义重试模板使用的退避策略

Is it possible to customize the backoff policy used by a retry template based on HttpStatus Code

我正在评估 spring-retry 的用例,我们需要根据状态代码自动重试某些 API 调用。我可以像这样自定义重试策略

@Component("httpStatusCodeRetryPolicy")
public class HttpStatusRetryPolicy extends ExceptionClassifierRetryPolicy
{
    @PostConstruct
    public void init()
    {
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        this.setExceptionClassifier( new Classifier<Throwable, RetryPolicy>()
        {
            @Override
            public RetryPolicy classify(Throwable classifiable )
            {
                if ( classifiable instanceof HttpStatusCodeException)
                {
                    var exception = (HttpStatusCodeException)classifiable;
                    if(exception.getStatusCode() == HttpStatus.REQUEST_TIMEOUT){
                       retryPolicy.setMaxAttempts(3);
                    }
                    else if (exception.getStatusCode() == HttpStatus.valueOf(429) ||
                    exception.getStatusCode() == HttpStatus.valueOf(502) ||
                    exception.getStatusCode() == HttpStatus.valueOf(503) ||
                    exception.getStatusCode() == HttpStatus.valueOf(504)) {
                        retryPolicy.setMaxAttempts(4);
                    }
                    return retryPolicy;
                }
                return new NeverRetryPolicy();
            }
        });
    }
}

但是,我还想根据这些状态代码自定义退避策略。我想对某些响应状态代码使用 FixedBackOff 策略,对其余代码使用 ExponentialBackOffPolicy。我环顾四周,没有找到任何指示。

根据 retryContext 中的 lastThrowable,使用委托给所需 BackOffPolicy 的自定义 BackOffPolicy