Java HttpMethod.releaseConnection()用法
我正在将HttpClient 3..0.1与Java HttpMethod.releaseConnection()用法,java,httpclient,Java,Httpclient,我正在将HttpClient 3..0.1与多线程HttpConnectionManager一起使用,并使用下面的代码获取页面,同时获取该页面的最终重定向url 多个线程并行访问此代码。在运行这段代码一段时间后,我开始不断地获取ConnectionPoolTimeoutException,然后不再获取任何页面 这与我应该增加的connectionManagerParam值有关,还是我在代码中做了一些错误的事情 GetMethod get = null; try { get
多线程HttpConnectionManager
一起使用,并使用下面的代码获取页面,同时获取该页面的最终重定向url
多个线程并行访问此代码。在运行这段代码一段时间后,我开始不断地获取ConnectionPoolTimeoutException
,然后不再获取任何页面
这与我应该增加的connectionManagerParam
值有关,还是我在代码中做了一些错误的事情
GetMethod get = null;
try {
get = new GetMethod();
get.setURI(uri);
get.setFollowRedirects(false);
int status = httpClient.executeMethod(null, get, new HttpState());
String location = null;
int retry = 2;
if (get.getResponseHeader("location") != null) {
location = get.getResponseHeader("location").getValue();
}
while (retry > 0 && ((int) (status / 100) != 2) && ((int) (status / 100) == 3) && location.length() > 0) {
// To get the final redirected url.
uri = URLUtil.createAbsoluteURIWithFix(location, null);
get = new GetMethod();
get.setURI(uri);
get.setFollowRedirects(false);
status = httpClient.executeMethod(null, get, new HttpState());
if (get.getResponseHeader("location") != null) {
location = get.getResponseHeader("location").getValue();
}
retry--;
}
if (status == 200) {
uri = get.getURI();
String html = URLUtil.getResponseBodyAsString(get, charsets);
}
} catch (Exception e) {
} finally {
if (get != null) {
get.releaseConnection();
}
}
异常堆栈跟踪
org.apache.commons.httpclient.ConnectionPoolTimeoutException: Timeout waiting for connection
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.doGetConnection(MultiThreadedHttpConnectionManager.java:490)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.getConnectionWithTimeout(MultiThreadedHttpConnectionManager.java:394)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:152)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
我是否需要在每个执行方法之后调用get.releaseConnection()?或者当前的编码还可以?似乎您没有关闭第一个get并创建一个新的get,从而影响到同一个var。 在循环中,你也会做同样的事情
您应该在这段代码中只保留一个实例,并在最后通过全局try/finally清理它。get.releaseConnection();-没有关闭第一个get?get的实例被loopah中的矫揉造作覆盖…没有在循环中看到另一个“new GetMethod()”