PoolingHttpClientConnectionManager如何管理连接?

时间:2017-09-08 05:45:20

标签: connection-pooling keep-alive httpconnection

我有一个问题,它是What is httpconnection of PoolingHttpClientConnectionManager?.

我知道如果我们使用PoolingHttpClientConnectionManager,它会减少花费连接建立的时间(例如ssl handshaketcp enter code herehandshake等),因为它会重用连接。

但是,我对http连接重用的理解是keep-alive,我们可以在服务器支持时使用它。如果主机不支持keep-alive连接,我们就无法通过keep-alive与主机通信。

所以,这是我的问题,

如果我使用PoolingHttpClientConnectionManager来管理非保持活动服务器环境中的连接, Connectionmanager管理连接吗?或者它为每个请求创建连接?

如果ConnectionManager管理连接,ConnectionManager如何保持连接?经理是否定期发送字节?

1 个答案:

答案 0 :(得分:0)

如果您没有定义HttpClient将充当连接,可以无限期地保持活动,来自Apache http docs

  

如果响应中没有Keep-Alive标头,则为HttpClient   假设连接可以无限期地保持活跃。

如果您想定义保持活动策略,请参阅example

ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
    @Override
    public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
        HeaderElementIterator it = new BasicHeaderElementIterator
            (response.headerIterator(HTTP.CONN_KEEP_ALIVE));
        while (it.hasNext()) {
            HeaderElement he = it.nextElement();
            String param = he.getName();
            String value = he.getValue();
            if (value != null && param.equalsIgnoreCase
               ("timeout")) {
                return Long.parseLong(value) * 1000;
            }
        }
        return 5 * 1000;
    }
};