gpt4 book ai didi

apache-httpclient-4.x - 为什么我使用 HttpClients.createDefault() 作为 HttpClient 单例实例执行第三个请求总是挂起

转载 作者:行者123 更新时间:2023-12-01 22:48:21 27 4
gpt4 key购买 nike

全部,

我创造:

public static final HttpClient DEFAULT_HTTPCLIENT = HttpClients
.createDefault();

for(int i=0 ; i<5; i++){
DEFAULT_HTTPCLIENT.execute(requests[i]);
}

但是当循环到 i =2 时,这意味着只执行前两个请求,直到第三个请求,客户端将挂起并且似乎死循环。

我引用了一些资料,我得到的可能是Http Thread Pool配置受限导致的。但我知道这个问题的标准解决方案是什么?因为我想随时发送任何请求,但我不想每次都创建新的 HttpClient 。那么对于这个问题,您有什么好的、规范的建议吗?

在我调试这个问题之后,我发现它在 HttpClient 上被阻止了,代码如下:PoolingHttpClientConnectionManager -> leaseConnection -> entry = future.get(timeout, tunit);

protected HttpClientConnection leaseConnection(
final Future<CPoolEntry> future,
final long timeout,
final TimeUnit tunit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException {
final CPoolEntry entry;
try {
entry = future.get(timeout, tunit);
if (entry == null || future.isCancelled()) {
throw new InterruptedException();
}
Asserts.check(entry.getConnection() != null, "Pool entry with no connection");
if (this.log.isDebugEnabled()) {
this.log.debug("Connection leased: " + format(entry) + formatStats(entry.getRoute()));
}
return CPoolProxy.newProxy(entry);
} catch (final TimeoutException ex) {
throw new ConnectionPoolTimeoutException("Timeout waiting for connection from pool");
}
}

最佳答案

那是因为您的代码正在泄漏连接。默认情况下,HttpClient 配置为允许同一路由不超过两个并发连接,因此在池完全耗尽之前只需要执行两次请求。

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e145

关于apache-httpclient-4.x - 为什么我使用 HttpClients.createDefault() 作为 HttpClient 单例实例执行第三个请求总是挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19920037/

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