遵循https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
的文件2.3.4. Connection manager shutdown
When an HttpClient instance is no longer needed and is about to go out of scope it is important to shut down its connection manager to ensure that all connections kept alive by the manager get closed and system resources allocated by those connections are released.
CloseableHttpClient httpClient = <...>
httpClient.close();
我的困惑在于将实例超出范围并需要关闭连接管理器。
在我的用例中,我正在使用PoolingConnection,所以我想保持连接打开,但当然要将它们返回池中。
在我的客户端代码中
ResponseHandler<Integer> rh = new ResponseHandler<Integer>()
.... elided ....
CloseableHttpClient httpclient = this.httpClientBuilder.build();
Integer statusCode = httpclient.execute(httpPost, rh);
我对文档的理解是,ResponseHandler的使用负责返回租约
When using a ResponseHandler, HttpClient will automatically take care of ensuring release of the connection back to the connection manager
答案 0 :(得分:3)
你的理解是正确的。只有在不再需要连接管理器和底层连接池时,才需要关闭连接管理器和底层连接池,以确保在池中保持活动状态的持久连接的立即关闭和处理。
ResponseHandler
确保从池中租用的连接被释放回管理器,无论请求执行结果如何,但管理员要么关闭连接还是保持活动状态以便随后重新使用请求。