Lance Rodrigues
Lance Rodrigues

Reputation: 111

When does a ServiceUnavailableRetryStrategy get called for apache httpclient

I have extended the ServiceUnavailableRetryStrategy interface provided by Apache HttpClient 4.5.3.

I have extended the retryRequest method as follows

@Override
public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
    int statusCode = response.getStatusLine().getStatusCode();
    return false;
}

I was expecting the value of the statusCode to be any failure status code (4xx or 5xx). However I seem to be receiving a 200 response code.

Is there something I am missing? The documentation mentions that this for handling Service Unavailable(503) responses and therefore never expected a 200 response to get into this method. Any suggestions on what could be wrong would be most appreciated

Upvotes: 1

Views: 1721

Answers (1)

zaerymoghaddam
zaerymoghaddam

Reputation: 3127

Seems the ServiceUnavailableRetryStrategy is a more general interface that would be used by the framework to determine if it should retry for any sort of error code. There is a default implementation called DefaultServiceUnavailableRetryStrategy that just does the check only for 503 error code. This means that when you provide your own implementation, you should also check for the specific error code that you're interested in (in this case 503) and response appropriately. If you check the source code of the DefaultServiceUnavailableRetryStrategy you can see that it explicitly checks the status code as:

return executionCount <= maxRetries &&
    response.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE;

Upvotes: 1

Related Questions