代码之家  ›  专栏  ›  技术社区  ›  jongseok

PoolighttpClientConnectionManager如何管理连接?

  •  1
  • jongseok  · 技术社区  · 7 年前

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

    PoolingHttpClientConnectionManager ,它减少了建立连接的时间(例如 ssl handshake , tcp enter code herehandshake 等等),因为它重用了连接。

    下面是我的问题,

    Connectionmanager是否管理连接?或者它会为每个请求创建连接?

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

    1 回复  |  直到 5 年前
        1
  •  2
  •   Ori Marko    7 年前

    如果您不定义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;
        }
    };