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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18184899/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:05:35  来源:igfitidea点击:

What is the difference between the setConnectionTimeout , setSoTimeout and "http.connection-manager.timeout" in apache HttpClient API

javaapache-httpclient-4.xhttpconnection

提问by Prateek

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"

3. "http.connection-manager.timeout"

采纳答案by Santosh

At the lowest level HTTP is TCP socket. 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.

在最低级别的 HTTP 是 TCP 套接字。因此,当您请求一个 URL 并获得响应时,在较低级别,会创建一个客户端 Socket,它与远程服务器 Socket 建立连接,发送一些数据并接收响应。

  • setConnectionTimeout: Client tries to connect to the server. 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. 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 408which the server sends to the client. In other words its maximum period inactivitybetween two consecutive data packets arriving at client side after connection is established.

  • http.connection-manager.timeout: MultiThreadedHttpConnectionManageruses a pool of HTTP connections. 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 HttpConnectionManagerreturned a connection.

  • setConnectionTimeout:客户端尝试连接到服务器。这表示在建立连接或服务器响应连接请求之前经过的时间。

  • setSoTimeout:建立连接后,客户端socket发送请求后等待响应。这是自客户端在服务器响应之前向服务器发送请求以来经过的时间。请注意,这HTTP Error 408与服务器发送给客户端的不同。换句话说,它在连接建立后到达客户端的两个连续数据包之间的最长不活动时间段

  • http.connection-manager.timeoutMultiThreadedHttpConnectionManager使用HTTP 连接池。它为每个主机设置了最大值和最小值。如果特定主机的所有连接都达到最大值,则同一主机的新连接请求将不得不等待,直到现有连接中的任何一个空闲。此参数表示从发出连接请求到HttpConnectionManager返回连接所经过的时间。

回答by Aniket Thakur

Detailed explanation is provided in Connection management documentationon 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). A timeout value of zero is interpreted as an infinite timeout. This parameter expects a value of type 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. A timeout value of zero is interpreted as an infinite timeout. This parameter expects a value of type 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

CoreConnectionPNames.SO_TIMEOUT='http.socket.timeout':以毫秒为单位定义套接字超时(SO_TIMEOUT),它是等待数据的超时,或者换句话说,两个连续数据包之间的最长不活动时间。超时值为零被解释为无限超时。此参数需要一个 java.lang.Integer 类型的值。如果未设置此参数,则读取操作不会超时(无限超时)。

CoreConnectionPNames.CONNECTION_TIMEOUT='http.connection.timeout':确定建立连接之前的超时时间(以毫秒为单位)。超时值为零被解释为无限超时。此参数需要一个 java.lang.Integer 类型的值。如果未设置此参数,则连接操作不会超时(无限超时)。

连接管理器超时(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);

回答by user666

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. 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.
  • 连接超时:您的应用程序在尝试与服务器建立连接但无法建立连接时等待的时间限制(地址错误,或服务器关闭等...)
  • 套接字超时:您的应用程序在连接到服务器后仍等待响应的时间限制(例如,延迟可能由挂起服务器引起)
  • 连接管理器超时:请求在队列中等待池中的一个请求被释放时等待的时间限制。换句话说,在 HTTP 中,我们有一个最大大小的连接池。当应用程序加载时,池可能已满,任何新请求都必须等待池中的另一个请求完成。

回答by Maria Ines Parnisari

This sequence diagram might help.

这个序列图可能会有所帮助。

apache http api

阿帕奇http api