Configure HttpClient correctly

References:

[1] http://dev.bizo.com/2013/04/sensible-defaults-for-apache-httpclient.html

 We have hit an issue recently that the httpClient is too slow to send messages to the hosts. Finally, we found that we just use 

CloseableHttpClient httpClient = HttpClients.custom().build();

to create a default httpClient and not even configure it. We shoud have set at least MaxTotal and MaxPerRoute.

MaxTotal is the maximum total number of connections in the pool. MaxPerRoute is the maximum number of connections to a particular host. If the client attempts to make a request and either of these maximums have been reached, then by default the client will block until a connection is free. Unfortunately the default for MaxTotal is 20 and the default MaxPerRoute is only 2.

Finally we solved th issue by setting

CloseableHttpClient httpClient = HttpClients.custom()
        .setMaxConnTotal(threadpoolSize) // threadpoolSize = 100
        .setMaxConnPerRoute(threadpoolSize)
        .build();

  

原文地址:https://www.cnblogs.com/codingforum/p/6099173.html