Reputation: 3533
before HttpClient we had a thread going on, which checked whether there are MAX_TOTAL_CONNECTIONS reached. It looked like this:
@Override
public void run() {
try {
while (!shutdown) {
synchronized (this) {
wait(60000);
// this is an emergency check. if all connections have been exhausted, then shutdown the old connection manager and instantiate a new http client
PoolingHttpClientConnectionManager tsc = (PoolingHttpClientConnectionManager) connMgr;
// previously here was : tsc.getConnectionsInPool() == MAX_TOTAL_CONNECTIONS
// since the PoolingClientConnectionManager has no clear replacement multiple candidates:
// 1. tsc.getTotalStats().getLeased() (number of persistent connections tracked by the connection manager currently being used to execute requests.) (Available + Leased)
// 2. tsc.getTotalStats().getPending() (number of connection requests being blocked awaiting a free connection.)
// 3. tsc.getTotalStats().getAvailable() (number idle persistent connections.)
// 4. tsc.getTotalStats().getMax() (maximum number of allowed persistent connections.)
// because of the statement: The total number of connections in the pool is equal to available plus leased - will go to use the sum of them both.
PoolStats poolStats = tsc.getTotalStats();
if (poolStats.getAvailable() + poolStats.getLeased() == MAX_TOTAL_CONNECTIONS) {
System.out.println("Error in HttpClient: It seems as if all connections are exhausted: will create new HttpClient now!");
tsc.shutdown();
httpAgent = initializeDefaultHttpClient();
}
}
}
}
catch (InterruptedException ex) {
// terminate
}
catch (IOException e) {
// error
}
}
public void shutdown() {
shutdown = true;
synchronized (this) {
notifyAll();
}
}
}
Now i know that we should use the CloseableHttpClient which has a Method ".close()". But i don't seam no to have any possibility to check whether i want to close or not.
Does someone know how to get best around that problem?
Thx
Mac
Upvotes: 0
Views: 3643
Reputation: 27538
There is absolutely nothing that stops you from keeping a reference to the underlying connection pool if required
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(cm)
.build();
PoolStats totalStats = cm.getTotalStats();
Upvotes: 1