gpt4 book ai didi

connection - org.apache.http.conn.ConnectionPoolTimeoutException : Timeout waiting for connection from pool

转载 作者:行者123 更新时间:2023-12-03 21:19:39 25 4
gpt4 key购买 nike

我在java中使用多线程同时扫描不同的URL。如果请求时间总和超过 100,000,则存在错误。我已经关闭了我应该关闭的。这里是我的 servlet 中的代码

private String proyGetHttp(String url) throws ParseException, IOException,
InterruptedException {

String content = "";
getMethod = new HttpGet(url);
HttpResponse response = null;
HttpEntity httpEntity = null;
boolean success = false;
while (!success) {
System.out.println("url:" + url + ",connect...");
try {
response = client.execute(getMethod);
httpEntity = response.getEntity();
StringBuffer sb = new StringBuffer();
if (httpEntity != null) {
BufferedReader in = null;
InputStream instream = httpEntity.getContent();
try {
in = new BufferedReader(new InputStreamReader(instream));
String lineContent = "";
while(lineContent != null){
sb.append(lineContent);
lineContent = in.readLine();
}

} catch (Exception ex)
getMethod.abort();
throw ex;
} finally {
// Closing the input stream will trigger connection release
try { instream.close(); in.close();} catch (Exception ignore) {}
}
}
content = sb.toString();
success = true;
System.out.println("connect successfully...");
} catch (Exception e) {
e.printStackTrace();
getMethod.abort();
System.out.println("connect fail, please waitting...");
Thread.sleep(sleepTime);
}finally{
getMethod.releaseConnection();
}
}
return content;
}

这里代码创建默认客户端

        PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
cm.setMaxTotal(100);
DefaultHttpClient client = null;
client = new DefaultHttpClient(cm);
client.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 5000);

最佳答案

我有同样的问题,我找到了解决办法。此超时是由于连接泄漏。就我而言,我使用的是 httpDelete 方法而不是使用响应。相反,我正在检查响应的状态。

解决方法是,需要消耗响应实体。为了确保系统资源的正确释放,必须关闭与实体关联的内容流。

所以我使用了 EntityUtils.consumeQuietly(response.getEntity()); 来确保实体内容被完全消费并且内容流(如果存在)被关闭。

关于connection - org.apache.http.conn.ConnectionPoolTimeoutException : Timeout waiting for connection from pool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16019612/

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