[英]Apache HttpClient TCP Keep-Alive (socket keep-alive)
I have http request that takes too much time to be processed by the server (about 5 minutes). 我有http请求需要花费太多时间来处理服务器(大约5分钟)。 Because connection becomes idle for 5 minutes, proxy server shutdowns the connection.
由于连接空闲5分钟,代理服务器会关闭连接。 I'm trying to use TCP Keep-Alive in Apache DefaultHttpClient to make connection be alive for a long time (Not confuse TCP Keep-Alive with HTTP Keep-Alive that simply doesn't closes connection after response is sent).
我正在尝试在Apache DefaultHttpClient中使用TCP Keep-Alive来使连接长时间处于活动状态(不要混淆TCP Keep-Alive和HTTP Keep-Alive,它只是在发送响应后不会关闭连接)。
Apache http core has following parameter SO_KEEPALIVE: http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/params/CoreConnectionPNames.html#SO_KEEPALIVE . Apache http核心具有以下参数SO_KEEPALIVE: http : //hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/params/CoreConnectionPNames.html#SO_KEEPALIVE 。 However, due to DefaultHttpClient javadocs I can't customize client's behavior with that parameter: https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html .
但是,由于DefaultHttpClient javadocs,我无法使用该参数自定义客户端的行为: https : //hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html 。
I did this, however, seems it doesn't work: 我做了这个,然而,似乎它不起作用:
HttpClient client = getHttpClient();
client.getParams().setParameter(CoreConnectionPNames.SO_KEEPALIVE, true);
Do you know how to make DefaultHttpClient use TCP Keep-Alive strategy? 你知道如何使DefaultHttpClient使用TCP Keep-Alive策略吗?
To make it work I needed to set keepalive timeouts. 为了使它工作,我需要设置keepalive超时。 But they can be set only on OS level, not in Java code.
但它们只能在OS级别设置,而不能在Java代码中设置。 As I know, it's not possible to set keepalive timeouts in Java code.
据我所知,在Java代码中设置keepalive超时是不可能的。
Here is how I set them on Linux: 以下是我在Linux上设置它们的方法:
sudo sysctl -w net.ipv4.tcp_keepalive_time=60
sudo sysctl -w net.ipv4.tcp_keepalive_intvl=60
sudo sysctl -w net.ipv4.tcp_keepalive_probes=10
Value is amount of seconds. 值是秒数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.