Apache HttpClient:如何通过服务器的保持活动时间自动关闭连接? [英] Apache HttpClient: How to auto close connections by server's keep-alive time?

查看:51
本文介绍了Apache HttpClient:如何通过服务器的保持活动时间自动关闭连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apache HttpClient 4.3b2、HttpCore 4.3.

Apache HttpClient 4.3b2, HttpCore 4.3.

我使用 PoolingHttpClientConnectionManager 同时管理 5 个连接:

I use PoolingHttpClientConnectionManager to manage 5 connections concurrently:

PoolingHttpClientConnectionManager connectionManager;
HttpClient httpclient;
connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setDefaultMaxPerRoute(5);
httpclient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();

服务器有 5 秒的保持活动时间.当服务器启动关闭连接过程时,它一直处于 FIN_WAIT2 状态,直到我执行 connectionManager.shutdown()connectionManager.closeExpiredConnections()connectionManager.closeIdleConnections(5、TimeUnit.SECONDS) 手动.服务器等待 FIN 包.服务器开始关闭过程后,如何自动关闭客户端的连接?

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?

当我从 Chrome 浏览器发出请求时,服务器在尝试通过保持连接关闭连接时保持 TIME_WAIT 状态(FIN_WAIT2 状态变化非常快).如何使用 Apache HttpClient 获得相同的行为?

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?

推荐答案

这个问题在HttpClient 教程

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

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

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

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.

这篇关于Apache HttpClient:如何通过服务器的保持活动时间自动关闭连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆