【问题标题】:How PoolingHttpClientConnectionManager manage connections?PoolingHttpClientConnectionManager 如何管理连接?
【发布时间】:2019-12-05 17:58:07
【问题描述】:

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

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

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

所以,这是我的问题,

如果我使用 PoolingHttpClientConnectionManager 来管理非保持活动服务器环境中的连接, Connectionmanager 是否管理连接?还是根据请求创建连接?

如果 ConnectionManager 管理连接,ConnectionManager 如何保持连接?管理器会定期发送字节吗?

【问题讨论】:

    标签: java connection-pooling keep-alive httpconnection apache-httpcomponents


    【解决方案1】:

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

    如果响应中不存在 Keep-Alive 标头,则 HttpClient 假设连接可以无限期地保持活动状态。

    如果您想定义 Keep-Alive 策略,请参阅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;
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多