连接管理器如何丢弃未使用的连接



我正在尝试调试我的应用程序中的连接泄漏问题,因为连接管理器使用的连接比我想要的要多。我有一条线索如下:

根据Apache HttpClient快速启动,如果响应内容由于任何原因没有被完全消耗,池连接管理器将无法安全地重用底层连接,并将丢弃它。

有人能给我指一下在连接中检查未使用内容并丢弃连接的代码块吗?

// Please note that if response content is not fully consumed the underlying
// connection cannot be safely re-used and will be shut down and discarded
// by the connection manager.
try (CloseableHttpResponse response1 = httpclient.execute(httpGet)) {
System.out.println(response1.getCode() + " " + response1.getReasonPhrase());
HttpEntity entity1 = response1.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity1);
}

HttpClient版本4.x和5.x使用代理封装HTTP响应实体,该代理在到达消息流末尾时将底层连接释放回池。在所有其他情况下,HttpClient都假定消息尚未完全使用,并且底层连接无法重复使用。

https://github.com/apache/httpcomponents-client/blob/master/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ResponseEntityProxy.java

最新更新