I am using Http Apache Components to perform the http interactions. I need to adjust my http client. For this purpose I have two parameters: connection timeout and connection request timeout. In library documentation and in source code(no comments were found) I didn't found definition of this terms. I need to know what do they exactly mean. May be they were defined in HTTP protocol documentation but I can't find it. So, my question is what do this two terms mean and how they distinct from each other.
Asked
Active
Viewed 4.9k times
33
-
Try searching them without apache nor java keywords: http://en.wikipedia.org/wiki/Timeout_(computing) – Luiggi Mendoza Nov 28 '13 at 16:38
2 Answers
35
HttpClient
has a way to set connection and socket timeout (setConnectionTimeout()
and setTimeout()
) according to the HttpClient javadocs.
Connection timeout
is the timeout until a connection with the server is established.
Socket timeout
is the timeout to receive data (socket timeout).
Example:
Let's say you point your browser to access a web page. If the server does not anwser in X seconds, a connection timeout will occur. But if it establishes the connection, then the server will start to process the result for the browser. If it does not ends this processing in Y seconds, a socket timeout will occur.
-
7Thank you for your reply, but still I have a problem. Here is request config builder http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/apidocs/org/apache/http/client/config/RequestConfig.Builder.html and it has three methods setConnectionRequestTimeout, setConnectTimeout, setSocketTimeout(and no comments!). So I still don't know what exactly each method does and what methods I should to use. – mvb13 Nov 29 '13 at 07:04
-
40Ok, I finally found that getConnectionRequestTimeout returns imeout in milliseconds used when requesting a connection from the connection manager. I guess that connection manager is apache-httpcomponents library class, so I will not use it. I will use setConnectionTimeout() and setTimeout(). – mvb13 Nov 29 '13 at 07:22
-
5
-
2The answer does not tell us what the connection request timeout is. Had to check the comments. – beinghuman Jun 27 '20 at 00:52
15
From the documentation:
/**
* 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>
* <p>
* Default: {@code -1}
* </p>
*/
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>
* <p>
* Default: {@code -1}
* </p>
*/
public int getConnectTimeout() {
return connectTimeout;
}
This is how the code should look:
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
RequestConfig.Builder requestBuilder = RequestConfig.custom();
// Connection Timeout to establish a connection
requestBuilder = requestBuilder.setConnectTimeout(connectTimeoutMillis);
// Timeout to get a connection from the connection manager for the Http Client
requestBuilder = requestBuilder.setConnectionRequestTimeout(requestTimeoutMillis);
// Timeout between two data packets from the server
requestBuilder = requestBuilder.setSocketTimeout(requestTimeoutMillis);
clientBuilder.setDefaultRequestConfig(requestBuilder.build());
CloseableHttpClient httpClient = clientBuilder.build();

Raj Hassani
- 1,577
- 1
- 19
- 26
-
1this answer is at least talking about what was asked, unlike the accepted one. however, in which case would requesting a connection from connection manager time out? what are the implications of setting it? when should you set it? It's really quite unclear. – eis Apr 29 '21 at 09:52
-
The connection manager could be a pool like `PoolingHttpClientConnectionManager`. When all connections from the pool are used, then the `ConnectionRequestTimeout` indicates how long your code should wait for a connection to be freed up. – Chris Sekas Jul 13 '21 at 14:34