Reputation: 2315
Following the documentation at 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();
My confusion is about conflating the the instance going out of scope and needing to shutdown the connection manager.
In my use case, I am using the PoolingConnection so I want to keep the connections open, but of course return them back to the pool.
In my client code I have
ResponseHandler<Integer> rh = new ResponseHandler<Integer>()
.... elided ....
CloseableHttpClient httpclient = this.httpClientBuilder.build();
Integer statusCode = httpclient.execute(httpPost, rh);
My understanding from the docs is that that the useage of ResponseHandler takes care of return the lease
When using a ResponseHandler, HttpClient will automatically take care of ensuring release of the connection back to the connection manager
Upvotes: 1
Views: 3605
Reputation: 27548
You understanding is correct. One needs to shut down the connection manager and the underlying connection pool only once it is no longer needed in order to ensure immediate shutdown and dealocation of persistent connections kept alive in the pool.
ResponseHandler
ensures the connection leased from the pool gets released back to the manager no matter the outcome of request execution but it is up to the manager either to close the connection or keep it alive for re-use by subsequent requests.
Upvotes: 3