Monitor connection pool of PoolingHttpClientConnectionManager

余生长醉 提交于 2021-01-21 10:32:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!