gpt4 book ai didi

java - PoolingHttpClientConnectionManager 如何管理连接?

转载 作者:行者123 更新时间:2023-11-30 06:31:05 26 4
gpt4 key购买 nike

我有一个问题,PoolingHttpClientConnectionManager 的 httpconnection 是什么?

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

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

所以,这是我的问题,

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

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

最佳答案

如果不定义 HttpClient 将充当连接可以无限期保持 Activity 状态,从 Apache http docs :

If the Keep-Alive header is not present in the response, HttpClient assumes the connection can be kept alive indefinitely.

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

关于java - PoolingHttpClientConnectionManager 如何管理连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46109431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com