Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient 4.3.5 ConnectionRequestTimeOut vs ConnectTimeout for HttpConnectionParams.setConnectionTimeout in 4.0.1

I am trying to upgrade our HttpClient Dependency from 4.0.1 to 4.3.5. And was confused on setting the connection timeout in the new lib. For the following code:

HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 300);

It will be replaced with the code below:

RequestConfig config = RequestConfig.custom().setConnectTimeout(300).build();
HttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

However, I am not sure if I should use setConnectTimeout or setConnectionRequestTimeout since I am not familiar with HttpClient. It seems to me I should use setConnectionTimeout based on the java doc in the source code. Can somebody please confirm? Thanks in advance.

https://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.3.3/httpclient/src/main/java/org/apache/http/client/config/RequestConfig.java

/**
 * Returns the timeout in milliseconds used when requesting a connection
 * from the connection manager. A timeout value of zero is interpreted
 * as an infinite timeout.
 * <p/>
 * A timeout value of zero is interpreted as an infinite timeout.
 * A negative value is interpreted as undefined (system default).
 * <p/>
 * Default: <code>-1</code>
 */
public int getConnectionRequestTimeout() {
    return connectionRequestTimeout;
}

/**
 * Determines the timeout in milliseconds until a connection is established.
 * A timeout value of zero is interpreted as an infinite timeout.
 * <p/>
 * A timeout value of zero is interpreted as an infinite timeout.
 * A negative value is interpreted as undefined (system default).
 * <p/>
 * Default: <code>-1</code>
 */
public int getConnectTimeout() {
    return connectTimeout;
}
like image 254
Zhao Avatar asked Jan 02 '15 23:01

Zhao


2 Answers

In version 4.3 of Apache Http Client the configuration was refactored (again). the new way as the following code:

RequestConfig requestConfig =RequestConfig.custom()
.setConnectTimeout(connectTimeout)
.setConnectionRequestTimeout(connectionRequestTimeout)
.setSocketTimeout(socketTimeout).build();

connectTimeout is the timeout until a connection with the server is established. connectionRequestTimeout is used when requesting a connection from the connection manager.

like image 72
Meysam Avatar answered Nov 04 '22 14:11

Meysam


Difference between the three timeouts in Apache HttpClient :

connectTimeout max time to establish a connection with remote host/server.

connectionRequestTimeout time to wait for getting a connection from the connection manager/pool. (HttpClient maintains a connection pool to manage the connections. Similar to database connection pool)

socketTimeout max time gap between two consecutive data packets while transferring data from server to client.

like image 21
Divyam Goel Avatar answered Nov 04 '22 14:11

Divyam Goel