简体   繁体   English

使用HttpAsyncClients:长时间保持连接活动

[英]Using HttpAsyncClients: keep connection alive for long time

I am trying to use HTTPAsyncCLient for fire and forget in my app. 我正在尝试使用HTTPAsyncCLient触发并忘记我的应用程序。

http://hc.apache.org/httpcomponents-asyncclient-4.0.x/ 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. 因此,有一种想法是我不会关闭httpclient,而是继续将其用于多个URL。 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? 一段时间后客户会变得陈旧吗?

The re-use of the same instance of HttpAsyncClient is highly recommended. 强烈建议HttpAsyncClient的同一实例。 HttpAsyncClient will not get 'stale'. HttpAsyncClient不会“过时”。 One only needs to close HttpAsyncClient once there are no more requests to be executed (for instance, in case of application shut down). 一旦不再有要执行的请求(例如,在应用程序关闭的情况下),就只需要关闭HttpAsyncClient

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM