Java 连接和连接请求超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20271017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connection and connection request timeout
提问by mvb13
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.
我正在使用 Http Apache 组件来执行 http 交互。我需要调整我的 http 客户端。为此,我有两个参数:连接超时和连接请求超时。在库文档和源代码中(没有找到注释),我没有找到这些术语的定义。我需要知道它们到底是什么意思。可能它们是在 HTTP 协议文档中定义的,但我找不到。所以,我的问题是这两个术语是什么意思以及它们如何相互区分。
采纳答案by dgimenes
HttpClient
has a way to set connection and socket timeout (setConnectionTimeout()
and setTimeout()
) according to the HttpClient javadocs.
HttpClient
有一种方法可以根据HttpClient javadocs设置连接和套接字超时(setConnectionTimeout()
和setTimeout()
)。
Connection timeout
is the timeout until a connection with the server is established.
Connection timeout
是与服务器建立连接之前的超时时间。
Socket timeout
is the timeout to receive data (socket timeout).
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.
假设您将浏览器指向一个网页。如果服务器在 X 秒内没有 anwser,将发生连接超时。但是如果它建立了连接,那么服务器将开始为浏览器处理结果。如果它没有在 Y 秒内结束此处理,则会发生套接字超时。
回答by Gray
So, my question is what do [connection-timeout and connection-request-timeout] mean and how they distinct from each other.
所以,我的问题是 [connection-timeout 和 connection-request-timeout] 是什么意思以及它们之间的区别。
Connection-timeout is the timeout in milliseconds until the server accepts the request. If you specify 3000, the http-client will wait 3 seconds for the server to accept the TCP connection before timing out. This typically is used to make sure you don't have a networking issue or that you are contacting the right hostname or address. This is equivalent to curl's --connect-timeout seconds
option.
Connection-timeout 是服务器接受请求之前的超时时间(以毫秒为单位)。如果指定 3000,则 http-client 将在超时前等待 3 秒让服务器接受 TCP 连接。这通常用于确保您没有网络问题或您正在联系正确的主机名或地址。这相当于 curl 的--connect-timeout seconds
选项。
Connection-request-timeout is the input/output timeout afterthe connection has been established. If you specify this value as 10000 then after the http-client has connected to the server and sends a request, it will wait 10 seconds for the server to return a result. This typically is used to ensure that your job doesn't wait for a slow server forever. This is equivalent to curl's --max-time seconds
option.
Connection-request-timeout 是建立连接后的输入/输出超时。如果将此值指定为 10000,那么在 http-client 连接到服务器并发送请求后,它将等待 10 秒让服务器返回结果。这通常用于确保您的工作不会永远等待慢速服务器。这相当于 curl 的--max-time seconds
选项。
In HttpClient 4.X.X , the following is how you build a client that uses a particular connectTimeoutMillis
and requestTimeoutMillis
.
在 HttpClient 4.XX 中,以下是您如何构建使用特定connectTimeoutMillis
和的客户端requestTimeoutMillis
。
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder = requestBuilder.setConnectTimeout(connectTimeoutMillis);
requestBuilder = requestBuilder.setConnectionRequestTimeout(requestTimeoutMillis);
clientBuilder.setDefaultRequestConfig(requestBuilder.build());
CloseableHttpClient httpClient = clientBuilder.build();
...
Btw, the javadocs for this code sucks. Try to figure out by hand how to use the config builder. Holy crap.
顺便说一句,这段代码的 javadoc 很烂。尝试手动弄清楚如何使用配置生成器。碉堡了。
回答by Raj Hassani
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();