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

4

44

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"

Pantalets answered 12/8, 2013 at 10:38 Comment(3)
What do the docs say? Are they silent?Babylon
@Marko Not able to understabd thats why posted herePantalets
You should at least mention that you have read them, and point out particular aspects which trouble you. This way your question looks like you're just asking us to go read the docs for you.Babylon
I
68

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.

Ia answered 12/8, 2013 at 11:14 Comment(4)
so that means we have to close either of socket or connectionPantalets
When you deal with APIs like HttpClient, you don't have to deal with sockets directly. Even in case of HttpClient there is a method (httpclient.getConnectionManager().shutdown();) to shutdown the the very HttpClient which does the job of closing down the connections.Ia
but i dont have to close connections as i m doing connection pooling.what should i do to stop waiting for a response for a request.Pantalets
??? but thats what you asked (_that means we have to close either of socket or connection _). For not waiting for response you are already using HttpConnectionManagerParams.setSoTimeout().Ia
F
39

This sequence diagram might help.

apache http api

Frozen answered 15/2, 2018 at 21:12 Comment(2)
Excellent diagram ;)Tattler
@Maria which sequence diagram? not visible on my browser. Any link?Crusade
R
7

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);
Readus answered 27/9, 2014 at 9:43 Comment(4)
How do you set http.connection-manager.timeout, I can't find the correct way to do this.Rancid
Answering my own comment, you should do it this way. setConnectionRequestTimeout is the one: RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(connectTimeout) .setConnectionRequestTimeout(connectionRequestTimeout) .setSocketTimeout(socketTimeout).build();Rancid
...and set the config to the HttpGet or other methods. httpGet.setConfig(requestConfig);.Pointtopoint
Just a side-note: in 4.5.7, as per the Javadoc, all the three args has the default value -1, which means "undefined(system default)".Pointtopoint
L
2

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.
Liberalism answered 14/2, 2018 at 11:20 Comment(1)
it's worth adding to the point 3 above that the threads will wait INFINITELY for a connection to be released back to the pool. Which is a very bad pattern to follow in applications where reliability and responsiveness are importantAbvolt

© 2022 - 2024 — McMap. All rights reserved.