何时在 CloseableHttpClient 实例上调用关闭
When to call close on CloseableHttpClient instnances
遵循 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,所以我想保持连接打开,但当然 return 它们会回到池中。
在我的客户端代码中有
ResponseHandler<Integer> rh = new ResponseHandler<Integer>()
.... elided ....
CloseableHttpClient httpclient = this.httpClientBuilder.build();
Integer statusCode = httpclient.execute(httpPost, rh);
我从文档中了解到,ResponseHandler 的使用负责return 租约
When using a ResponseHandler, HttpClient will automatically take care of ensuring release of the connection back to the connection manager
您的理解是正确的。只有在不再需要时才需要关闭连接管理器和底层连接池,以确保立即关闭和释放池中保持活动状态的持久连接。
ResponseHandler
确保从池中租用的连接被释放回管理器,无论请求执行的结果如何,但由管理器关闭连接或保持连接以供重新使用根据后续请求。
遵循 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,所以我想保持连接打开,但当然 return 它们会回到池中。
在我的客户端代码中有
ResponseHandler<Integer> rh = new ResponseHandler<Integer>()
.... elided ....
CloseableHttpClient httpclient = this.httpClientBuilder.build();
Integer statusCode = httpclient.execute(httpPost, rh);
我从文档中了解到,ResponseHandler 的使用负责return 租约
When using a ResponseHandler, HttpClient will automatically take care of ensuring release of the connection back to the connection manager
您的理解是正确的。只有在不再需要时才需要关闭连接管理器和底层连接池,以确保立即关闭和释放池中保持活动状态的持久连接。
ResponseHandler
确保从池中租用的连接被释放回管理器,无论请求执行的结果如何,但由管理器关闭连接或保持连接以供重新使用根据后续请求。