Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

java - 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.

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"

question from:https://stackoverflow.com/questions/18184899/what-is-the-difference-between-the-setconnectiontimeout-setsotimeout-and-http

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...