Is it possible to set RetryPolicy in spring-retry based on HttpStatus status code? Is it possible to set RetryPolicy in spring-retry based on HttpStatus status code? spring spring

Is it possible to set RetryPolicy in spring-retry based on HttpStatus status code?


The RestTemplate has setErrorHandler option and DefaultResponseErrorHandler is the default one.

Its code looks like:

public void handleError(ClientHttpResponse response) throws IOException {    HttpStatus statusCode = getHttpStatusCode(response);    switch (statusCode.series()) {        case CLIENT_ERROR:            throw new HttpClientErrorException(statusCode, response.getStatusText(),                    response.getHeaders(), getResponseBody(response), getCharset(response));        case SERVER_ERROR:            throw new HttpServerErrorException(statusCode, response.getStatusText(),                    response.getHeaders(), getResponseBody(response), getCharset(response));        default:            throw new RestClientException("Unknown status code [" + statusCode + "]");    }}

So, you can provide your own implementation for that method to simplify your RetryPolicy around desired status codes.


For others who are facing same problem, I'm posting this answer.Implement custom retry policy as follows:

class InternalServerExceptionClassifierRetryPolicy extends ExceptionClassifierRetryPolicy {public InternalServerExceptionClassifierRetryPolicy() {    final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();    simpleRetryPolicy.setMaxAttempts(3);    this.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {        @Override        public RetryPolicy classify(Throwable classifiable) {            if (classifiable instanceof HttpServerErrorException) {                // For specifically 500 and 504                if (((HttpServerErrorException) classifiable).getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR                        || ((HttpServerErrorException) classifiable)                                .getStatusCode() == HttpStatus.GATEWAY_TIMEOUT) {                    return simpleRetryPolicy;                }                return new NeverRetryPolicy();            }            return new NeverRetryPolicy();        }    });}}

Ans the simply call it as below:

RetryTemplate template = new RetryTemplate();template.setRetryPolicy(new InternalServerExceptionClassifierRetryPolicy())