Apache HttpClient TCP保持活动状态(套接字保持活动状态) [英] Apache HttpClient TCP Keep-Alive (socket keep-alive)

查看:187
本文介绍了Apache HttpClient TCP保持活动状态(套接字保持活动状态)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有http请求,该请求花费太多时间由服务器处理(大约5分钟).由于连接闲置了5分钟,因此代理服务器将关闭连接. 我试图在Apache DefaultHttpClient中使用TCP Keep-Alive使连接保持活动状态很长时间(不要将TCP Keep-Alive与HTTP Keep-Alive混淆,后者在发送响应后根本不会关闭连接).

I have http request that takes too much time to be processed by the server (about 5 minutes). Because connection becomes idle for 5 minutes, proxy server shutdowns the connection. 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 http核心具有以下参数SO_KEEPALIVE: http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/params/CoreConnectionPNames.html#SO_KEEPALIVE .但是,由于DefaultHttpClient javadocs,我无法使用该参数来自定义客户端的行为:

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

但是,我这样做似乎不起作用:

I did this, however, seems it doesn't work:

HttpClient client = getHttpClient();
client.getParams().setParameter(CoreConnectionPNames.SO_KEEPALIVE, true);

您知道如何使DefaultHttpClient使用TCP Keep-Alive策略吗?

Do you know how to make DefaultHttpClient use TCP Keep-Alive strategy?

推荐答案

要使其正常运行,我需要设置keepalive超时时间. 但是只能在操作系统级别设置它们,而不能在Java代码中设置.据我所知,不可能在Java代码中设置keepalive超时.

To make it work I needed to set keepalive timeouts. But they can be set only on OS level, not in Java code. As I know, it's not possible to set keepalive timeouts in Java code.

这是我在Linux上设置它们的方法:

Here is how I set them on 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

值是秒数.

这篇关于Apache HttpClient TCP保持活动状态(套接字保持活动状态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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