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

查看:160
本文介绍了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天全站免登陆