Apache HttpClient

Quick Tips

Pooling connection manager

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// Increase max total connection to 200
cm.setMaxTotal(200);

// global variable $httpClient, build Once and using by Every Thread
CloseableHttpClient httpClient = HttpClients.custom()
        .setConnectionManager(cm)
        // must be True
        .setConnectionManagerShared(true)
        .build();

try {
   //... preparing post content
   resp = httpClient.execute(post, respeHandler);
   //... process response
} finally {
   if (null != httpClient) httpClient.close();
}
  • httpClient is a global variable which is sharing by multiple threads.
  • setConnectionManagerShared(true) a shared ConnectionManager won't be shut down while the client is closed, otherwise the following exception will be arisen, i.e. java.lang.IllegalStateException: Connection pool shut down ...

results matching ""

    No results matching ""