java - setMaxForRoute 在 ThreadSafeClientConnManager 中不起作用

标签 java apache httpclient

我一直在尝试使用 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/

相关文章:

java - 我不断收到这个 java.lang.OutOfMemoryError

c# - HTTPClient 错误提供了无效的请求 URI

java - 如何通过java登录网站?

java - 如何从服务器端代码(JSP 或 Servlet)读取客户端本地磁盘上的文件

java - 如何获取 JDK 8 的二进制构建和源代码 tarball?

java - JTextArea 的 Apache DefaultExecutor 和 Ping 命令

async-await - Blazor、Httpclient、同步和异步 WebApi 调用

java - 机器人为什么要不停地运转?是否陷入了循环?

java - 我用什么代替 Mockito 2.2 中的 Whitebox 来设置字段?

apache - ubuntu 14.04LTS apache2 漂亮的网址不起作用