问题
I'd like to monitor the connection pool of a Apache httpComponents PoolingHttpClientConnectionManager
but cannot find a way to access it.
We create our HttpClient
using the following builder:
HttpClient httpClient = HttpClientBuilder.create()
.setMaxConnTotal(maxConnections)
.setMaxConnPerRoute(maxConnectionsPerRoute)
.build();
This creates an instance of an InternalHttpClient
, which holds an instance of a PoolingHttpClientConnectionManager
, which holds an instance of a CPool
.
CPool
would give us access to T getRoutes()
and PoolStats getStats(T)
— which looks promising to me. But I can't really find out how to get access to this CPool
.
HttpClient.getConnectionManager()
is deprecated. InternalHttpClient.getConnectionManager()
is not deprecated, but returns a custom connection manager, which only exposes some of the methods of the real connection manager instance behind it.
So, how to get access to these stats? These would be very helpful for us.
回答1:
Use ConnPoolControl
implemented by PoolingHttpClientConnectionManager
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
ConnPoolControl<HttpRoute> connPoolControl = cm;
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(cm)
.build();
来源:https://stackoverflow.com/questions/53873158/monitor-connection-pool-of-poolinghttpclientconnectionmanager