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

StackOverflow https://stackoverflow.com/questions/18184899

Вопрос

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.

2. setSoTimeout - if it determines the period of inactivity or time difference between two consecutive packets ,

Then what does the below one do :

3. "http.connection-manager.timeout"

Это было полезно?

Решение

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.

  • 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 408 which the server sends to the client. 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. 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.

Другие советы

This sequence diagram might help.

apache http api

Detailed explanation is provided in Connection management documentation on Apache HTTP client site.

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

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. 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 с атрибуция
Не связан с StackOverflow
scroll top