httpclientapache-httpclient-4.xapache-commons-httpclient

Timeout between request retries Apache HttpClient


Could somebody share how to configure modern HttpClient 4.5.3 to retry failed requests and wait for some time before each retry?

So far it looks like I got it correctly that .setRetryHandler(new DefaultHttpRequestRetryHandler(X, false)) will allow to retry requests X times.

But I cannot understand how to configure backoff: .setConnectionBackoffStrategy() / .setBackoffManager() according to JavaDocs regulate something else, not timeout between retries.


Solution

  • About the dynamic delay, I want to suggest this:

    CloseableHttpClient client = HttpClientBuilder.create()
        .setRetryHandler(new HttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
                return executionCount <= maxRetries ;
            }
        })
        .setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() {
            int waitPeriod = 100;
            @Override
            public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
                waitPeriod *= 2;
                return executionCount <= maxRetries &&
                   response.getStatusLine().getStatusCode() >= 500; //important!
            }
    
            @Override
            public long getRetryInterval() {
                return waitPeriod;
            }
        })
        .build();
    

    Appendix: Please note, that ServiceUnavailableRetryStrategy.retryRequest will NOT be called, if there was an IO error like timeout, port not open or connection closed. In such cases, only HttpRequestRetryHandler.retryRequest will be called, and the retry will happen either immediately or after a fixed delay (I could not finally clarify this). So oleg's answer is actually the right one. There is no way to do it with support of HttpClient 4.5.

    (I would actually like to call this a design bug, as delayed retries after an IO error are vitally important in a modern microservice environment.)