Java Apache HttpClient:如何通过服务器的保持活动时间自动关闭连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18697290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache HttpClient: How to auto close connections by server's keep-alive time?
提问by Lunigorn
Apache HttpClient 4.3b2, HttpCore 4.3.
Apache HttpClient 4.3b2、HttpCore 4.3。
I use PoolingHttpClientConnectionManager
to manage 5 connections concurrently:
我用来PoolingHttpClientConnectionManager
同时管理 5 个连接:
PoolingHttpClientConnectionManager connectionManager;
HttpClient httpclient;
connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setDefaultMaxPerRoute(5);
httpclient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
Server have 5 seconds keep-alive time.
When server initiate close connection process it is staying in FIN_WAIT2 state until I'll execute connectionManager.shutdown()
or connectionManager.closeExpiredConnections()
or connectionManager.closeIdleConnections(5, TimeUnit.SECONDS)
manually. Server waits FIN package. How can I automatically close connections on client side after server start closing process?
服务器有 5 秒的保持活动时间。当服务器启动关闭连接过程是停留在FIN_WAIT2状态,直到我将执行connectionManager.shutdown()
或connectionManager.closeExpiredConnections()
或connectionManager.closeIdleConnections(5, TimeUnit.SECONDS)
手动。服务器等待 FIN 包。服务器开始关闭过程后,如何自动关闭客户端的连接?
When I do requests from Chrome browser, server stay in TIME_WAIT state when it try to close connection by keep-alive (FIN_WAIT2 state changes very quickly). How can I get the same behavior with Apache HttpClient?
当我从 Chrome 浏览器发出请求时,服务器在尝试通过保持连接关闭连接时保持 TIME_WAIT 状态(FIN_WAIT2 状态变化非常快)。如何使用 Apache HttpClient 获得相同的行为?
采纳答案by ok2c
This problem is explained in details in HttpClient tutorial
这个问题在HttpClient教程中有详细说明
One of the major shortcomings of the classic blocking I/O model is that the network socket can react to I/O events only when blocked in an I/O operation. When a connection is released back to the manager, it can be kept alive however it is unable to monitor the status of the socket and react to any I/O events. If the connection gets closed on the server side, the client side connection is unable to detect the change in the connection state (and react appropriately by closing the socket on its end).
经典阻塞 I/O 模型的主要缺点之一是网络套接字只有在 I/O 操作中被阻塞时才能对 I/O 事件做出反应。当一个连接被释放回管理器时,它可以保持活动状态,但是它无法监视套接字的状态并对任何 I/O 事件做出反应。如果连接在服务器端关闭,客户端连接将无法检测到连接状态的变化(并通过关闭其末端的套接字做出适当的反应)。
If you want expired connections to get pro-actively evicted from the connection pool there is no way around running an additional thread enforcing a connection eviction policy that suits your application.
如果您希望从连接池中主动驱逐过期的连接,则无法运行额外的线程来强制执行适合您的应用程序的连接驱逐策略。
回答by ivan
In PoolingHttpClientConnectionManager
class there is a method setValidateAfterInactivity
that sets period of connection inactivity in milliseconds. If this period has been exceeded connection pool revalidates connection before passing it to HttpClient.
This method is available since v.4.4.
In prior versions RequestConfig.Builder.setStaleConnectionCheckEnabled
method could have been used.
在PoolingHttpClientConnectionManager
课堂上,有一种方法setValidateAfterInactivity
可以以毫秒为单位设置连接不活动的时间。如果超过此期限,连接池会在将连接传递给 HttpClient 之前重新验证连接。此方法自 v.4.4 起可用。在以前的版本中RequestConfig.Builder.setStaleConnectionCheckEnabled
可以使用方法。