Proper timeout handling with Apache HttpClient

August 24th, 2009 by Brian | Filed under work.

I’ve seen some really bad things happen when developers don’t code in proper timeout handling. Occasionally I’ve been asked what the best way to handle timeouts is – so I thought I’d share my take on it:

MultiThreadedHttpConnectionManager connectionManager =  new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = connectionManager.getParams();

params.setConnectionTimeout(connectiontimeout); //set connection timeout (how long it takes to connect to remote host)
params.setSoTimeout(sotimeout); //set socket timeout (how long it takes to retrieve data from remote host)

HttpMethodBase baseMethod = null;

try {
  HttpClient httpClient = new HttpClient(connectionManager);
  httpClient.getParams().setParameter("http.connection-manager.timeout", poolTimeout); //set timeout on how long we’ll wait for a connection from the pool

  baseMethod = new GetMethod();
  int statusCode = httpClient.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();
}

Tags: , , ,

5 Responses to “Proper timeout handling with Apache HttpClient”

  1. subdigit | 24/08/09

    Dug through my old code. Is setting the timeout at the HttpClient level as above:

    httpClient.getParams().setParameter(“http.connection-manager.timeout”, poolTimeout);

    the same thing as setting the timeout at the ConnectionManager level?

    connectionManager.getParams().setConnectionTimeout(poolTimeout);

    Does the manager automatically pass the parameters to the client as it creates the client?

  2. subdigit | 24/08/09

    Oh, and which packages are the ConnectionTimeoutException and SocketTimeoutException from? And what actually throws them? I can only seem to find that an HttpException gets thrown from within the .executeMethod.

  3. Brian | 25/08/09

    Yep, both setting them on the connection manager & on the httpclient should have same effect. I think there are a few more settings you can do within the connection manager and/or it’s good for reuse – building multiple httpclient objects without needing to re-specify all the settings.

  4. Brian | 25/08/09

    org.apache.commons.httpclient.ConnectTimeoutException
    and
    java.net.SocketTimeoutException

    The SocketTimeoutException is thrown by HttpMethodBase on things like getResponseStreamAsBody() which throws an IOException which wraps an InterruptedException which wraps a SocketTimeoutException. Simple right? :)

    More information on exception handling here

  5. subdigit | 26/08/09

    Ah, all those wrappers. Ok. Added them to my code. I was just catching a generic Exception before, but having the specific exception can be useful.

Share Your Thoughts