0

I have a custom retry policy that is designed to perform retries whenever the application receives http status codes that are not 404. I also have created a retry template. I am stuck on how I am to execute the custom retry policy with the retry template. Any help on this and how to test my results would be very nice. I have included the code for my retry template and custom retry policy.

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;
}

}

Dave Michaels
  • 847
  • 1
  • 19
  • 51

1 Answers1

0

Just inject an instance of your retry policy into the template instead of the SimpleRetryPolicy.

To test, you can add a RetryListener to the template in your test case.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179