简体   繁体   English

apache HttpClient API中的setConnectionTimeout,setSoTimeout和“http.connection-manager.timeout”之间有什么区别

[英]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.timeoutMultiThreadedHttpConnectionManager使用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返回连接之前经过的时间。

This sequence diagram might help. 此序列图可能会有所帮助。

apache http api

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: 简单来说:

  • connection time out: the limit of time your application waits for when trying to establish a connection with the server and is not able to (the address is wrong, or the server is down etc...) 连接超时:应用程序在尝试与服务器建立连接时等待的时间限制(无法解决,或者服务器已关闭等等)
  • Socket time out: the limit of time your application waits for after being connected to the server but still waiting for a response (delay can be caused by hanging server for example) 套接字超时:应用程序在连接到服务器但等待响应后等待的时间限制(例如,挂起服务器可能导致延迟)
  • Connection manager time out: the limit of time a request waits for when is in the queue waiting for one of the requests in the pool to be freed. 连接管理器超时:请求在队列中等待释放池中的一个请求时等待的时间限制。 In other words, in HTTP we have a pool of connections with a max size. 换句话说,在HTTP中,我们有一个最大大小的连接池。 When there is load on the application the pool maybe full and any new request must wait for another request from the pool to finish. 当应用程序上有负载时,池可能已满,任何新请求必须等待池中的另一个请求完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改“ http.connection-manager.timeout”? - How could I change “http.connection-manager.timeout”? Apache HttpClient API中的CloseableHttpClient和HttpClient有什么区别? - What is the difference between CloseableHttpClient and HttpClient in Apache HttpClient API? ftp apache setDefaultimeout(),setSoTimeout()和setDataTimeout()之间的区别 - Difference between ftp apache setDefaultimeout() , setSoTimeout() and setDataTimeout() HttpClient中的“拒绝连接”异常和超时之间的区别 - Difference between a “Connection Refused” exception and a timeout in HttpClient 最新的Apache HTTP客户端中的setConnectionTimeout()相当于什么? - What's the equivalent of setConnectionTimeout() in latest Apache HTTP client? HttpClient 4.3.1的org.apache.http.client.utils.URIBuilder中`removeQuery()`和`clearParameters()`有什么区别? - What is the difference between `removeQuery()` and `clearParameters()` in org.apache.http.client.utils.URIBuilder of HttpClient 4.3.1? 套接字的连接超时和读取超时有什么区别? - What is the difference between connection and read timeout for sockets? WebClient请求timeout()和HttpClient超时的区别 - Difference between timeout() in WebClient request and the timeout in HttpClient Apache HttpClient 4.3 - 设置连接空闲超时 - Apache HttpClient 4.3 - setting connection idle timeout httpclient 异常“org.apache.http.conn.ConnectionPoolTimeoutException:等待连接超时” - httpclient exception “org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM