我一直在尝试使用Apache HttpClient(4.1.3)和ThreadSafeClientConnManager
实现连接池。当我尝试为路线设置最大连接时,我遇到了一个问题。基本上我遵循hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html的例子。例如,我想将每个路由的默认连接设置为10,将ceratin路由设置为5个连接。
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);
然后我在线程中执行请求:
public void run() {
try {
HttpResponse response = this.httpClient.execute(this.httpget, this.context);
HttpEntity entity = response.getEntity();
if (entity != null) {
// do something useful with the entity
}
// ensure the connection gets released to the manager
EntityUtils.consume(entity);
} catch (Exception ex) {
this.httpget.abort();
}
}
获取这样的日志:
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]]
为什么我为这条路线获得Available capacity: 10 out of 10
,而不是我指定的5?
由于
UPD :如果我在创建连接管理器后运行此语句cm.getMaxForRoute(new HttpRoute(host, null, false))
,它将返回5。
但是如果我尝试在线程中检查路由的最大连接数(获得响应之后):
HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
cm.getMaxForRoute(new HttpRoute(target));
连接管理器将返回与日志(10个连接)相同的内容。
如果有任何帮助,我将不胜感激。
答案 0 :(得分:1)
这很奇怪,但是当我创建没有端口和协议参数的HttpHost
时,它运行良好。
HttpHost host = new HttpHost("hc.apache.org");
HttpRoute route = new HttpRoute(httpHost);
conman.setMaxPerRoute(route, 13);
PS:我使用过httpclient-4.2