gpt4 book ai didi

java - setMaxForRoute 在 ThreadSafeClientConnManager 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:31 24 4
gpt4 key购买 nike

我一直在尝试使用 Apache HttpClient (4.1.3) 和 ThreadSafeClientConnManager 实现连接池。当我尝试设置路由的最大连接数时,我遇到了一个问题。基本上我遵循 hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html 中的示例。例如,我想将每个路由的默认连接设置为 10 个,将某些路由的默认连接设置为 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]]

为什么我获得此路线的可用容量:10 分(共 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 个连接)。

如果有任何帮助,我将不胜感激。

最佳答案

这很奇怪,但当我创建不带端口和协议(protocol)参数的 HttpHost 时,效果很好。

HttpHost host = new HttpHost("hc.apache.org");
HttpRoute route = new HttpRoute(httpHost);
conman.setMaxPerRoute(route, 13);

PS:我使用过httpclient-4.2

关于java - setMaxForRoute 在 ThreadSafeClientConnManager 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10673517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com