Search code examples
javaapachehttpclientsocket-timeout-exception

Apache HttpClient 3.1 socket timeout


I'm executing webservice requests with Apache HttpClient 3.1 in the following way:

private MultiThreadedHttpConnectionManager cm = null;
private HttpClient client = null;

// Setting up connection manager during init process
cm = new MultiThreadedHttpConnectionManager();
int connectionTimeout = 12000; // it actually comes from config file, but this is the current value
cm.getParams().setConnectionTimeout(connectionTimeout);
cm.getParams().setTcpNoDelay(true);
client = new HttpClient(cm);

// method is prepared with request data earlier
int socketTimeout = 30000; // it actually comes from config file, but this is the current value
method.getParams().setSoTimeout(socketTimeout);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));

logger.info("Starting request");
int statusCode = client.executeMethod(method);
logger.info("StatusCode = " + statusCode);
String response = method.getResponseBodyAsString();
logger.info("Response = " + response);

The timestamps in the log file are the following:

2017-05-02 08:50:03,881 Starting request
2017-05-02 08:50:16,680 StatusCode = 200
2017-05-02 08:50:46,708 java.net.SocketTimeoutException

So even though connection timeout is set to 12 seconds, executeMethod call took almost 13 seconds. I'm confused about this, as the documentation is not clear here: is it correct to say that executeMethod and getResponseBodyAsString both use socket timeout separately? So, in this case, socket timeout is 30 seconds, so theoretically could the execution take 60 seconds to complete?


Solution

  • The connect timeout is irrelevant. You got a status code of 200, which means that the connect succeeded and you read at least some headers. The read timeout is set to 30 seconds, which means the server didn't send anything else within 30 seconds.