У меня есть настраиваемая политика повторов, которая предназначена для выполнения попыток всякий раз, когда приложение получает коды состояния http, отличные от 404. Я также создал шаблон повторов. Я застрял на том, как мне выполнить пользовательскую политику повтора с шаблоном повтора. Любая помощь в этом и как проверить мои результаты были бы очень хорошими. Я включил код для моего шаблона повторения и пользовательской политики повторов.
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(maxBackOffPeriod);
retryTemplate.setBackOffPolicy(backOffPolicy);
NeverRetryPolicy doNotRetry = new NeverRetryPolicy();
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(maxAttempts);
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}
public class HttpFailedConnectionRetryPolicy extends ExceptionClassifierRetryPolicy {
@Value("${maxAttempts:-1}")
private String maxAttempts;
public void HttpFailedConnectionRetryPolicy() {
this.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {
@Override
public RetryPolicy classify(Throwable classifiable) {
Throwable exceptionCause = classifiable.getCause();
if (exceptionCause instanceof HttpStatusCodeException) {
int statusCode = ((HttpStatusCodeException) classifiable.getCause()).getStatusCode().value();
return handleHttpErrorCode(statusCode);
}
return simpleRetryPolicy();
}
});
}
public void setMaxAttempts(String maxAttempts) {
this.maxAttempts = maxAttempts;
}
private RetryPolicy handleHttpErrorCode(int statusCode) {
RetryPolicy retryPolicy = null;
switch (statusCode) {
case 404:
retryPolicy = doNotRetry();
break;
default:
retryPolicy = simpleRetryPolicy();
break;
}
return retryPolicy;
}
private RetryPolicy doNotRetry() {
return new NeverRetryPolicy();
}
private RetryPolicy simpleRetryPolicy() {
final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
simpleRetryPolicy.setMaxAttempts(Integer.valueOf(maxAttempts));
return simpleRetryPolicy;
}
}