Java Apache HttpClient 4.3 - 设置连接空闲超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21237391/
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 4.3 - setting connection idle timeout
提问by YaOg
What's the shortest way to configure connection idle timeout on Apache HttpClient 4.3 version?
在 Apache HttpClient 4.3 版本上配置连接空闲超时的最短方法是什么?
I've looked in the documentation and couldn't find anything. My goal is to reduce open connections to a minimum post server-peak.
我查看了文档并找不到任何内容。我的目标是将打开的连接减少到最小的服务器后峰值。
for example in Jetty Client 8.x you can set httpClient.setIdleTimeout: http://download.eclipse.org/jetty/stable-8/apidocs/org/eclipse/jetty/client/HttpClient.html#setIdleTimeout(long)
例如在 Jetty Client 8.x 你可以设置 httpClient.setIdleTimeout: http://download.eclipse.org/jetty/stable-8/apidocs/org/eclipse/jetty/client/HttpClient.html#setIdleTimeout(long)
采纳答案by Brett
The timeout is set in the RequestConfig so you could set the default when the HttpClientBuilder is called.
超时设置在 RequestConfig 中,因此您可以在调用 HttpClientBuilder 时设置默认值。
For example assuming your timeout variable is in seconds to create your custom RequestConfig you could do something like this:
例如,假设您的超时变量以秒为单位来创建您的自定义 RequestConfig,您可以执行以下操作:
RequestConfig config = RequestConfig.custom()
.setSocketTimeout(timeout * 1000)
.setConnectTimeout(timeout * 1000)
.build();
You could then build your HttpClient setting the default RequestConfig like this:
然后你可以像这样构建你的 HttpClient 设置默认的 RequestConfig :
HttpClients.custom()
.setDefaultRequestConfig(config);
回答by jasop
You can'tset an idle connection timeout in the config for Apache HTTP Client. The reason is that there is a performance overhead in doing so.
您不能在 Apache HTTP 客户端的配置中设置空闲连接超时。原因是这样做会产生性能开销。
The documentation clearly states why, and gives an example of an idle connection monitor implementation you can copy. Essentially this is another thread that you run to periodically call closeIdleConnections
on HttpClientConnectionManager
该文档清楚地说明了原因,并提供了一个您可以复制的空闲连接监视器实现示例。本质上,这是另一个线程运行以定期调用closeIdleConnections
上HttpClientConnectionManager
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
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). HttpClient tries to mitigate the problem by testing whether the connection is 'stale', that is no longer valid because it was closed on the server side, prior to using the connection for executing an HTTP request. The stale connection check is not 100% reliable and adds 10 to 30 ms overhead to each request execution. The only feasible solution that does not involve a one thread per socket model for idle connections is a dedicated monitor thread used to evict connections that are considered expired due to a long period of inactivity. The monitor thread can periodically call ClientConnectionManager#closeExpiredConnections() method to close all expired connections and evict closed connections from the pool. It can also optionally call ClientConnectionManager#closeIdleConnections() method to close all connections that have been idle over a given period of time.
经典阻塞 I/O 模型的主要缺点之一是网络套接字只有在 I/O 操作中被阻塞时才能对 I/O 事件做出反应。当一个连接被释放回管理器时,它可以保持活动状态,但是它无法监视套接字的状态并对任何 I/O 事件做出反应。如果连接在服务器端关闭,客户端连接将无法检测到连接状态的变化(并通过关闭其末端的套接字来做出适当的反应)。HttpClient 尝试通过测试连接是否“过时”来缓解问题,在使用连接执行 HTTP 请求之前,它不再有效,因为它在服务器端关闭。陈旧的连接检查不是 100% 可靠的,并且会为每个请求执行增加 10 到 30 毫秒的开销。对于空闲连接,每个套接字模型不涉及一个线程的唯一可行解决方案是专用监视器线程,用于驱逐由于长时间不活动而被认为已过期的连接。监控线程可以定期调用 ClientConnectionManager#closeExpiredConnections() 方法关闭所有过期的连接并从池中驱逐关闭的连接。它还可以选择调用 ClientConnectionManager#closeIdleConnections() 方法来关闭在给定时间段内空闲的所有连接。监控线程可以定期调用 ClientConnectionManager#closeExpiredConnections() 方法关闭所有过期的连接并从池中驱逐关闭的连接。它还可以选择调用 ClientConnectionManager#closeIdleConnections() 方法来关闭在给定时间段内空闲的所有连接。监控线程可以定期调用 ClientConnectionManager#closeExpiredConnections() 方法关闭所有过期的连接并从池中驱逐关闭的连接。它还可以选择调用 ClientConnectionManager#closeIdleConnections() 方法来关闭在给定时间段内空闲的所有连接。