Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HttpClient 4.3 - setting connection idle timeout

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.

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)

like image 695
YaOg Avatar asked Jan 20 '14 15:01

YaOg


People also ask

How do I set HttpClient timeout?

The default value is 100,000 milliseconds (100 seconds). To set an infinite timeout, set the property value to InfiniteTimeSpan. A Domain Name System (DNS) query may take up to 15 seconds to return or time out.

What is connection timeout in HttpClient?

timeout) – the time waiting for data – after establishing the connection; maximum time of inactivity between two data packets. the Connection Manager Timeout (http. connection-manager. timeout) – the time to wait for a connection from the connection manager/pool.

What is HTTP idle timeout?

The Idle Timeout setting in the TCP profile specifies the length of time that a connection is idle before the connection is eligible for deletion. If no traffic flow is detected within the idle session timeout, the BIG-IP system can delete the session. The default is 300 seconds.

What is idle HTTP connection?

A connection kept open until the next HTTP request reduces latency and TCP connection establishment overhead.


1 Answers

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

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();

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

HttpClients.custom()
    .setDefaultRequestConfig(config);
like image 112
Brett Avatar answered Sep 17 '22 17:09

Brett