Reputation: 714
I am trying to use HTTPAsyncCLient for fire and forget in my app.
http://hc.apache.org/httpcomponents-asyncclient-4.0.x/
But for real fire and forget, I need to avoid closing the client, as I do not want to wait for the response, and if I close, the connection might have not been made till then.
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
httpclient.start();
HttpGet request = new HttpGet(url);
Future<HttpResponse> future = httpclient.execute(request, null);
HttpResponse resp = future.get();
httpclient.close();
So one thought is that I do not close the httpclient, and keep using it for multiple URLs. So I will spawn a client on server startup, and keep using the same client for all requests.
This way, close is not needed as it be released only when server stops. My question is will this be a problem? Will the client become stale after some time?
Upvotes: 3
Views: 2326
Reputation: 27593
The re-use of the same instance of HttpAsyncClient
is highly recommended. HttpAsyncClient will not get 'stale'. One only needs to close HttpAsyncClient
once there are no more requests to be executed (for instance, in case of application shut down).
Upvotes: 6