Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HttpAsyncClients: keep connection alive for long time

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?

like image 400
Bulbasaur Avatar asked Mar 19 '26 06:03

Bulbasaur


1 Answers

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).

like image 150
ok2c Avatar answered Mar 21 '26 18:03

ok2c



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!