Apache HttpClient 4.3-设置连接空闲超时

Apache HttpClient 4.3-设置连接空闲超时

问题描述:

在Apache HttpClient 4.3版本上配置连接空闲超时的最短方法是什么?

What's the shortest way to configure connection idle timeout on Apache HttpClient 4.3 version?

我已经查看了文档,但找不到任何东西.我的目标是将开放的连接减少到服务器后的最低峰值.

I've looked in the documentation and couldn't find anything. My goal is to reduce open connections to a minimum post server-peak.

例如在Jetty Client 8.x中,您可以设置httpClient.setIdleTimeout:

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)

超时是在RequestConfig中设置的,因此可以在调用HttpClientBuilder时设置默认值.

The timeout is set in the RequestConfig so you could set the default when the HttpClientBuilder is called.

例如,假设您的超时变量以秒为单位来创建自定义RequestConfig,则可以执行以下操作:

For example assuming your timeout variable is in seconds to create your custom RequestConfig you could do something like this:

RequestConfig config = RequestConfig.custom()
    .setSocketTimeout(timeout * 1000)
    .setConnectTimeout(timeout * 1000)
    .build();

然后您可以像这样设置默认的RequestConfig来构建HttpClient:

You could then build your HttpClient setting the default RequestConfig like this:

HttpClients.custom()
    .setDefaultRequestConfig(config);