get NoHttpResponseException for load testing

I’ve found the root cause of the problem.

For some reason the connection becomes invalid and pool is not aware of it.

In this case the NoHttpResponseException is thrown and the request simply fails. I thought that such issues should be resolved in HTTP client pool layer and be transparent to my code, but this is not as it acts.

To solve this problem the HttpRequestRetryHandler in HTTP client should be overridden:

ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
...
DefaultHttpClient httpClient = new DefaultHttpClient(cm, params);
httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
    @Override
    public boolean retryRequest(IOException exception, int executionCount, 
                                HttpContext context) {
        if (executionCount > 3) {
           LOGGER.warn("Maximum tries reached for client http pool ");
                return false;
        }
        if (exception instanceof org.apache.http.NoHttpResponseException) {
            LOGGER.warn("No response from server on " + executionCount + " call");
            return true;
        }
        return false;
      }
   }); 

This solution is for version HttpClient 4.5 and later. DefaultHttpClient is deprecated.

HttpClientBuilder clientBuilder = HttpClients.custom();
clientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, false));

Leave a Comment