illcar
illcar

Reputation: 469

How to fix ConnectionPoolTimeoutException in Apache Http Client

In our code we use version 4.5.3 of apache http client. PoolingHttpClientConnectionManager is used as follows.

final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
                600_000, TimeUnit.MILLISECONDS);

I understand that in order to fix ConnectionPoolTimeoutException, we either need to call EntityUtils.consume(httpEntity), or use ResponseHandler wherever HttpClient is used. However in our code base, the same HttpClient is being used from many places. After fixing much code to use either of the above two, the frequency is reduced, but we still get ConnectionPoolTimeoutException at times.

Can this exception be fixed by using evictExpiredConnections() or evictIdleConnections(maxIdleTime) methods while creating HttpClientBuilder? What is the ideal value of maxIdleTime that we can use - more than connectionTimeToLive?

One of the code example is as follows.

final HttpResponse resp ;
        try
        {
            resp = client.execute(request);
        }
        catch (IOException e)
        {
            logException(e);
            return;
        }

        processResponse(resp);
        return;

We noticed a correlation between logException invocations & ConnectionPoolTimeoutException in the logs. In other words ConnectionPoolTimeoutException starts happening on a node, shortly after there are number of logException calls on that node. Is there something wrong with this code snippet?

Upvotes: 0

Views: 1498

Answers (1)

illcar
illcar

Reputation: 469

Finally the solution was to use ResponseHandler wherever we had missed it. That was the only approach that worked, and solved ConnectionPoolTimeoutExceptions.

In an enterprise application, you cannot just close the HttpClient after each use. Using ResponseHandler is the best coding practice.

Upvotes: 2

Related Questions