I am trying to implement a connection pool with Apache HttpClient (4.1.3) and ThreadSafeClientConnManager. And I ran into a problem when trying to establish maximum connections for the route. I basically followed the examples from hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html . For example, I want to set default connections for each route up to 10 and for ceratin route up to 5 connections.
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
cm.setMaxTotal(30);
cm.setDefaultMaxPerRoute(10);
HttpHost host = new HttpHost("hc.apache.org", 80, "http");
cm.setMaxForRoute(new HttpRoute(host, null, false), 5);
DefaultHttpClient httpClient = new DefaultHttpClient(cm);
And then I execute requests in threads:
public void run() {
try {
HttpResponse response = this.httpClient.execute(this.httpget, this.context);
HttpEntity entity = response.getEntity();
if (entity != null) {
}
EntityUtils.consume(entity);
} catch (Exception ex) {
this.httpget.abort();
}
}
And get logs like this:
DEBUG Thread-0 [org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager]: Get connection: HttpRoute[{}->http://hc.apache.org], timeout = 10000
DEBUG Thread-4 [org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager]: Get connection: HttpRoute[{}->http://hc.apache.org], timeout = 10000
DEBUG Thread-1 [org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager]: Get connection: HttpRoute[{}->http://hc.apache.org], timeout = 10000
DEBUG Thread-7 [org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager]: Get connection: HttpRoute[{}->http://hc.apache.org], timeout = 10000
DEBUG Thread-3 [org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager]: Get connection: HttpRoute[{}->http://hc.apache.org], timeout = 10000
DEBUG Thread-5 [org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager]: Get connection: HttpRoute[{}->http://hc.apache.org], timeout = 10000
DEBUG Thread-8 [org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager]: Get connection: HttpRoute[{}->http://hc.apache.org], timeout = 10000
DEBUG Thread-2 [org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager]: Get connection: HttpRoute[{}->http://hc.apache.org], timeout = 10000
DEBUG Thread-6 [org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager]: Get connection: HttpRoute[{}->http://hc.apache.org], timeout = 10000
DEBUG Thread-0 [org.apache.http.impl.conn.tsccm.ConnPoolByRoute]: [HttpRoute[{}->http://hc.apache.org]] total kept alive: 0, total issued: 0, total allocated: 0 out of 30
DEBUG Thread-0 [org.apache.http.impl.conn.tsccm.ConnPoolByRoute]: No free connections [HttpRoute[{}->http://hc.apache.org]][null]
DEBUG Thread-0 [org.apache.http.impl.conn.tsccm.ConnPoolByRoute]: Available capacity: 10 out of 10 [HttpRoute[{}->http://hc.apache.org]][null]
DEBUG Thread-0 [org.apache.http.impl.conn.tsccm.ConnPoolByRoute]: Creating new connection [HttpRoute[{}->http://hc.apache.org]]
Why did I get Available capacity: 10 out of 10for this route, but not 5, as I indicated?
thank
UPD. cm.getMaxForRoute(new HttpRoute(host, null, false)) , 5.
( ):
HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
cm.getMaxForRoute(new HttpRoute(target));
, (10 ).
.