[英]org.apache.commons.httpclient.HttpClient stuck on request
I have that code : 我有该代码:
while(!lastPage && currentPage < maxPageSize){
StringBuilder request = new StringBuilder("http://catalog.bizrate.com/services/catalog/v1/us/" + " some more ...");
currentPage++;
HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
client.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
GetMethod get = new GetMethod(request.toString());
HostConfiguration configuration = new HostConfiguration();
int iGetResultCode = client.executeMethod(configuration, get);
if (iGetResultCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + get.getStatusLine());
return;
}
XMLStreamReader reader
= XMLInputFactory.newInstance().createXMLStreamReader(get.getResponseBodyAsStream());
while (reader.hasNext()) {
int type = reader.next();
// some more xml parsing ...
}
reader.close();
get.releaseConnection();
}
Somehow the code gets suck from time to time on line : executing request. 不知何故,代码不时在线上:执行请求。
I cant find the configuration for a request time out (not the connection timeout) , can someone help me maybe , or is there something that I am doing basely wrong ? 我找不到请求超时(而不是连接超时)的配置,也许有人可以帮助我,还是我在做一些根本错误的事情?
You can also set socket read timeou using setSoTimeout()
but that is no guarantee either. 您也可以使用
setSoTimeout()
设置套接字读取timeou,但这也不保证。
The only solution is to run the request in a different thread and interrupt the thread after timeout. 唯一的解决方案是在另一个线程中运行请求,并在超时后中断该线程。 You can use FutureTask to do this.
您可以使用FutureTask来执行此操作。 See my answer to this question for examples,
有关示例,请参见我对这个问题的回答,
PoolingConnectionManager
maintains a maximum limit of connection on a per route basis and in total. PoolingConnectionManager
维护每个路由的总连接数上限。 Per default this implementation will create no more than than 2 concurrent connections per given route and no more 20 connections in total. 默认情况下,此实现将为每个给定路由创建不超过2个并发连接,并且总共不超过20个连接。 For many real-world applications these limits may prove too constraining, especially if they use HTTP as a transport protocol for their services.
对于许多现实应用程序,这些限制可能证明过于严格,特别是如果它们使用HTTP作为其服务的传输协议。 Connection limits, however, can be adjusted using HTTP parameters.
但是,可以使用HTTP参数来调整连接限制。 For more information, you can refer to
PoolingClientConnectionManager
Java API 有关更多信息,您可以参考
PoolingClientConnectionManager
Java API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.