setMaxForRoute不ThreadSafeClientConnManager工作 [英] setMaxForRoute does not work in ThreadSafeClientConnManager

查看:222
本文介绍了setMaxForRoute不ThreadSafeClientConnManager工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力实现与Apache的HttpClient(4.1.3)和 ThreadSafeClientConnManager 连接池。我面临着一个问题,试图为路由的最大连接数时。基本上,我跟着从 hc.apache.org/httpcomponents-client-例子GA /教程/ HTML / connmgmt.html 。比如,我想设置为10和ceratin路线,5个连接每条路线的默认连接。

I have been trying to implement connection pool with Apache HttpClient (4.1.3) and ThreadSafeClientConnManager. And I faced with a problem when tried to set maximum connections for a route. Basically I followed examples from hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html. For example, I want to set default connections per route to 10 and for ceratin route 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) {
                // 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,因为我指定的

Why did I get Available capacity: 10 out of 10 for this route, but not 5 as I specified?

感谢

UPD :如果我运行该语句 cm.getMaxForRoute(新HttpRoute(主机,空,假))创建连接管理器后,它会返回5。
但是,如果我会尽量为您在线程航线最大连接数(获得响应后):

UPD: If I run this statement cm.getMaxForRoute(new HttpRoute(host, null, false)) after creating connection manager it will return 5. But if I'll try to check max connections for route in thread (after getting response):

HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
cm.getMaxForRoute(new HttpRoute(target));

连接管理器将返回同样的事情,在日志(10个连接)。

Connection manager will return the same thing that in logs (10 connections).

我会感谢任何帮助。

推荐答案

这是奇怪,但它工作得很好,当我创建 HttpHost 无端口和协议参数。

It is strange, but it works well when I create HttpHost without port and protocol arguments.

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

PS:我已经使用的HttpClient-4.2

这篇关于setMaxForRoute不ThreadSafeClientConnManager工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆