PoolingClientConnectionManager 中“每路由基础”的含义是什么?

2022-09-02 21:36:10

ThreadSafeClientConnManager 已被弃用,并引入了一个新方法 PoolingClientConnectionManager

PoolingClientConnectionManager的文档说

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

我的问题

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


答案 1

简单来说,每个路由意味着您要连接到的每个主机。

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


答案 2

它指的是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


推荐