17

不推荐使用ThreadSafeClientConnManager并引入了一个新方法PoolingClientConnectionManager

PoolingClientConnectionManager 的文档说

管理客户端连接池,并能够为来自多个执行线程的连接请求提供服务。连接按每条路由汇集。

我的问题

这里的每条路线基础是什么意思?

4

2 回答 2

17

简而言之,每条路由意味着您要连接的每台主机。

PoolingHttpClientConnectionManager在每条路由的基础上和总计上保持连接的最大限制。默认情况下,此实现将为每个给定路由创建不超过 2 个并发连接,并且总共不超过 20 个连接。

于 2017-06-13T00:21:57.080 回答
4

它指的是HttpRoute。HttpRoute 是用来描绘在同一个 Web 服务器上运行的多个应用程序。

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/conn/routing/HttpRoute.html

它的用法如下:

ClientConnectionRequest connRequest = connMrg.requestConnection(
        new HttpRoute(new HttpHost("localhost", 80)), null);
ManagedClientConnection conn = connRequest.getConnection(10, TimeUnit.SECONDS);
try {
    BasicHttpRequest request = new BasicHttpRequest("GET", "/");
    conn.sendRequestHeader(request);
    HttpResponse response = conn.receiveResponseHeader();
    conn.receiveResponseEntity(response);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        BasicManagedEntity managedEntity = new BasicManagedEntity(entity, conn, true);
        // Replace entity
        response.setEntity(managedEntity);
    }
    // Do something useful with the response
    // The connection will be released automatically 
    // as soon as the response content has been consumed
} catch (IOException ex) {
    // Abort connection upon an I/O error.
    conn.abortConnection();
    throw ex;
}

来源:http ://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

于 2012-08-21T19:45:37.393 回答