2

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

为什么我得到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 个连接)。

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

4

1 回答 1

1

这很奇怪,但是当我在HttpHost没有端口和协议参数的情况下创建时效果很好。

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

PS:我用过httpclient-4.2

于 2012-07-16T12:35:05.710 回答