简体   繁体   English

Apache HttpClient:如何通过服务器的保持活动时间自动关闭连接?

[英]Apache HttpClient: How to auto close connections by server's keep-alive time?

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.服务器有 5 秒的保持活动时间。 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.当服务器启动关闭连接过程时,它一直处于 FIN_WAIT2 状态,直到我手动执行connectionManager.shutdown()connectionManager.closeExpiredConnections()connectionManager.closeIdleConnections(5, TimeUnit.SECONDS)为止。 Server waits FIN package.服务器等待 FIN 包。 How can I automatically close connections on client side after server start closing process?服务器开始关闭过程后,如何自动关闭客户端的连接?

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).当我从 Chrome 浏览器发出请求时,服务器在尝试通过保持连接关闭连接时保持 TIME_WAIT 状态(FIN_WAIT2 状态变化非常快)。 How can I get the same behavior with Apache HttpClient?如何使用 Apache HttpClient 获得相同的行为?

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.经典阻塞 I/O 模型的主要缺点之一是网络套接字只有在 I/O 操作中被阻塞时才能对 I/O 事件做出反应。 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.当一个连接被释放回管理器时,它可以保持活动状态,但是它无法监视套接字的状态并对任何 I/O 事件做出反应。 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).如果连接在服务器端关闭,客户端连接将无法检测到连接状态的变化(并通过关闭其末端的套接字做出适当的反应)。

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.如果您希望从连接池中主动驱逐过期的连接,则无法运行额外的线程来强制执行适合您的应用程序的连接驱逐策略。

In PoolingHttpClientConnectionManager class there is a method setValidateAfterInactivity that sets period of connection inactivity in milliseconds.PoolingHttpClientConnectionManager类有一种方法setValidateAfterInactivity ,其设定以毫秒为单位的连接的非活动时段。 If this period has been exceeded connection pool revalidates connection before passing it to HttpClient.如果超过此期限,连接池会在将连接传递给 HttpClient 之前重新验证连接。 This method is available since v.4.4.此方法自 v.4.4 起可用。 In prior versions RequestConfig.Builder.setStaleConnectionCheckEnabled method could have been used.在以前的版本中,可以使用RequestConfig.Builder.setStaleConnectionCheckEnabled方法。

I found this question multiple times while working on an Apache HttpClient 5 based client implementation to figure out whether a idle http connection monitor is still required.我在处理基于 Apache HttpClient 5 的客户端实现时多次发现这个问题,以确定是否仍然需要空闲的 http 连接监视器。

Apparently, since Apache HttpClient 4.4, there is org.apache.hc.client5.http.impl.IdleConnectionEvictor which does exactly the thing described in HttpClient tutorial (which isn't mentioned in the tutorial).显然,从 Apache HttpClient 4.4 开始,有org.apache.hc.client5.http.impl.IdleConnectionEvictor完全符合HttpClient 教程教程中没有提到)中描述的内容。

Thought it might be useful to be aware of this for others as well.认为对其他人也了解这一点可能很有用。

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

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