On Thu, 2012-12-13 at 22:18 +0200, Marko Asplund wrote:
> 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.
> 

This can happen if you have a pool with the number of concurrent
connections much smaller than the number of work threads (which causes a
high resource contention) combined with an aggressive timeout value. 


> 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();
> }
> 

Closing the response content stream is perfectly sufficient. Just make
sure your code _always_ consumes response entities even for non-200
responses.

Hope this helps

Oleg 



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

Reply via email to