[英]What is the difference between the setConnectionTimeout , setSoTimeout and “http.connection-manager.timeout” in apache HttpClient API
What is the difference between the three(marked as comments) : 三者之间有什么区别(标记为评论):
MultiThreadedHttpConnectionManager connManag = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams managParams = connManag.getParams();
managParams.setConnectionTimeout(connectiontimeout); // 1
managParams.setSoTimeout(sotimeout); //2
HttpMethodBase baseMethod = null;
try {
HttpClient client = new HttpClient(connManag);
client.getParams().setParameter("http.connection-manager.timeout", poolTimeout); //3
baseMethod = new GetMethod(…);
int statusCode = client.executeMethod(…);
…
}
catch (ConnectTimeoutException cte ){
//Took too long to connect to remote host
}
catch (SocketTimeoutException ste){
//Remote host didn’t respond in time
}
catch (Exception se){
//Some other error occurred
}
finally {
if (baseMethod != null)
baseMethod.releaseConnection();
}
1. setConnectionTimeout
- if it determines the timeout until connection is established. 1. setConnectionTimeout
- 如果确定连接建立之前的超时。
2. setSoTimeout
- if it determines the period of inactivity or time difference between two consecutive packets , 2. setSoTimeout
- 如果它确定两个连续数据包之间的不活动时间或时间差,
Then what does the below one do : 然后下面做了什么:
3. "http.connection-manager.timeout"
At the lowest level HTTP is TCP socket. 在最低级别,HTTP是TCP套接字。 So when you request a URL and get a response, at lower level, a client Socket is created which establishes connection to the remote Server Socket, sends some data and receives response.
因此,当您请求URL并获得响应时,在较低级别创建一个客户端Socket,它建立与远程服务器套接字的连接,发送一些数据并接收响应。
setConnectionTimeout : Client tries to connect to the server. setConnectionTimeout :客户端尝试连接到服务器。 This denotes the time elapsed before the connection established or Server responded to connection request.
这表示建立连接或服务器响应连接请求之前经过的时间。
setSoTimeout : After establishing the connection, the client socket waits for response after sending the request. setSoTimeout :建立连接后,客户端套接字在发送请求后等待响应。 This is the elapsed time since the client has sent request to the server before server responds.
这是自客户端在服务器响应之前向服务器发送请求以来经过的时间。 Please note that this is not same as
HTTP Error 408
which the server sends to the client. 请注意,这与服务器发送给客户端的
HTTP Error 408
。 In other words its maximum period inactivity between two consecutive data packets arriving at client side after connection is established. 换句话说,在建立连接之后到达客户端侧的两个连续数据分组之间的最大周期不活动 。
http.connection-manager.timeout : MultiThreadedHttpConnectionManager
uses a pool of HTTP connections . http.connection-manager.timeout :
MultiThreadedHttpConnectionManager
使用HTTP连接池 。 It has maximum and minimum values per host set for it. 它为每个主机设置了最大值和最小值。 If all the connections for particular host are has reached max value, the request for new connection for the same host will have to wait till any one of the existing connection becomes free.
如果特定主机的所有连接都已达到最大值,则同一主机的新连接请求必须等到任何一个现有连接变为空闲。 This parameter denotes the time elapsed when a connection request was made and before the
HttpConnectionManager
returned a connection. 此参数表示在发出连接请求时和
HttpConnectionManager
返回连接之前经过的时间。
Detailed explanation is provided in Connection management documentation on Apache HTTP client site. Apache HTTP客户端站点上的连接管理文档中提供了详细说明。
CoreConnectionPNames.SO_TIMEOUT='http.socket.timeout': defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets).
CoreConnectionPNames.SO_TIMEOUT ='http.socket.timeout':定义套接字超时(SO_TIMEOUT),以毫秒为单位,即等待数据的超时, 换句话说 ,两个连续数据包之间的最大周期不活动。 A timeout value of zero is interpreted as an infinite timeout.
超时值为零被解释为无限超时。 This parameter expects a value of type java.lang.Integer.
此参数需要java.lang.Integer类型的值。 If this parameter is not set, read operations will not time out (infinite timeout).
如果未设置此参数,则读取操作不会超时(无限超时)。
CoreConnectionPNames.CONNECTION_TIMEOUT='http.connection.timeout': determines the timeout in milliseconds until a connection is established.
CoreConnectionPNames.CONNECTION_TIMEOUT ='http.connection.timeout':确定建立连接之前的超时(以毫秒为单位)。 A timeout value of zero is interpreted as an infinite timeout.
超时值为零被解释为无限超时。 This parameter expects a value of type java.lang.Integer.
此参数需要java.lang.Integer类型的值。 If this parameter is not set, connect operations will not time out (infinite timeout).
如果未设置此参数,则连接操作不会超时(无限超时)。
the Connection Manager Timeout (http.connection-manager.timeout) – the time to wait for a connection from the connection manager/pool
连接管理器超时(http.connection-manager.timeout) - 等待连接管理器/池连接的时间
Just for the record 仅供记录
HttpParams httpParams = httpClient.getParams();
httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
is alternate way to do 是另一种方式
HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000);
HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000);
In simple words: 简单来说:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.