hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marko Asplund <marko.aspl...@ixonos.com>
Subject ConnectionPoolTimeoutException with multi-threaded HttpClient usage
Date Thu, 13 Dec 2012 20:18:38 GMT
Hi,

I'm having problems using HttpClient in a multi-threaded environment.
When HttpClient.execute is called I occasionally get the following
error, even when there
probably should be connections available in the pool.

org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting
for connection from pool
        at org.apache.http.impl.conn.PoolingClientConnectionManager.leaseConnection(PoolingClientConnectionManager.java:232)
[httpclient-4.2.2.jar:4.2.2]
        at org.apache.http.impl.conn.PoolingClientConnectionManager$1.getConnection(PoolingClientConnectionManager.java:199)
[httpclient-4.2.2.jar:4.2.2]
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:455)
[httpclient-4.2.2.jar:4.2.2]
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
[httpclient-4.2.2.jar:4.2.2]
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
[httpclient-4.2.2.jar:4.2.2]
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
[httpclient-4.2.2.jar:4.2.2]
...

Should I explicitly release the connection after each HTTP request is executed?
Is there something else that should be done to clean up after each request?
The HC tutorial recommends passing a per-thread HttpContext object to
HC.execute but is this required?
What kind of state is actually stored in HttpContext?

Below is a simplified version of the code.

// one-time initialization
PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
cm.setDefaultMaxPerRoute(20);
HttpClient httpClient = new DefaultHttpClient(cm);

// executed repeatedly by multiple concurrent threads
HttpGet rq = new HttpGet(uri);
InputStream is = null;
try {
  HttpResponse res = httpClient.execute(rq);
  // ...
  is = res.getEntity().getContent();
} finally {
  is.close();
}

// one-time disposal
httpClient.getConnectionManager().shutdown();


thanks,

marko

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message