I am using Apache PoolingHttpClientConnectionManager to create a pool of conenctions. However I am seeing in the logs that the existing Idle connection is not used and rather it creates a new connection. Below is the code
PoolingHttpClientConnectionManager connManager = connManager = new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(4);
connManager.setDefaultMaxPerRoute(4);
//Creating CloseableHttpClient with a default keep alive of 2 minutes
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager)
.setKeepAliveStrategy(new KeepAliveStrategy(keepAlive))
.build();
//Sending 1st request
String xml="<xml data>";
HttpPost post = new HttpPost("<URL>");
HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
post.setEntity(entity);
HttpResponse response =client.execute(post);
String result = EntityUtils.toString(response.getEntity());
EntityUtils.consume(response.getEntity());
After the response is received the PoolStats says that Total Number of available Connection is 1 .
Now I again Fire the same request after 5 seconds and upon getting the response the PoolStats states that the Total Number of available Connection is 2
The below code is for PoolStats
PoolStats stats = connManager.getTotalStats();
System.out.println("Total Connections Available : "+stats.getAvailable());
My Question here is Since after the First Request-response there was already a Connection in the pool so why did it create one more connection. Why it did not use the existing conenction?