2012-07-09 47 views
3

一些使用后,HttpClient的似乎来阻塞,导致我的整个应用程序冻结:Apache HttpClient用尽了连接,但我不确定为什么?

Thread [main] (Suspended) 
    Unsafe.park(boolean, long) line: not available [native method] 
    LockSupport.park(Object) line: 186 
    AbstractQueuedSynchronizer$ConditionObject.await() line: 2043 
    AbstractConnPool$2(PoolEntryFuture<T>).await(Date) line: 131  
    HttpConnPool(AbstractConnPool<T,C,E>).getPoolEntryBlocking(T, Object, long, TimeUnit, PoolEntryFuture<E>) line: 281 
    AbstractConnPool<T,C,E>.access$000(AbstractConnPool, Object, Object, long, TimeUnit, PoolEntryFuture) line: 62 
    AbstractConnPool$2.getPoolEntry(long, TimeUnit) line: 176 
    AbstractConnPool$2.getPoolEntry(long, TimeUnit) line: 172 
    AbstractConnPool$2(PoolEntryFuture<T>).get(long, TimeUnit) line: 100  
    PoolingClientConnectionManager.leaseConnection(Future<HttpPoolEntry>, long, TimeUnit) line: 212 
    PoolingClientConnectionManager$1.getConnection(long, TimeUnit) line: 199  
    DefaultRequestDirector.execute(HttpHost, HttpRequest, HttpContext) line: 453  
    ContentEncodingHttpClient(AbstractHttpClient).execute(HttpHost, HttpRequest, HttpContext) line: 927 
    ContentEncodingHttpClient(AbstractHttpClient).execute(HttpUriRequest, HttpContext) line: 826  
    ContentEncodingHttpClient(AbstractHttpClient).execute(HttpUriRequest) line: 804 
    ... 

我没有看到任何“关闭()”或“发行()”方法对返回的HttpResponse,什么我不是在这里做我需要做的事吗?

每请求时,这里是我的代码:

final HttpResponse hr = Misc.httpClient.execute(hg); 
if (hr.getEntity().getContentType().getValue().toLowerCase().contains("html")) { 
    final Document doc = Jsoup.parse(hr.getEntity().getContent(), ContentType.getOrDefault(hr.getEntity()).getCharset(), urlAsString); 
    processDocument(url, doc); 
} else if (hr.getEntity().getContentType().getValue().toLowerCase().contains("text/xml")) { 
    final SyndFeedInput input = new SyndFeedInput(); 

    rssFeed = input.build(new InputStreamReader(hr.getEntity().getContent())); 
} 
+0

向我们展示你的代码,你如何使用'HttpClient' API。对于初学者,尝试使用'execute()'和'ResponseHandler'参数 – 2012-07-09 16:37:21

回答

8

,你得到的InputStream从您的HttpResponse需要有调用它close()。 HttpResponse本身没有close()

例如,当你得到你的HttpResponse并呼吁getEntity()得到一个HttpEntity,呼吁getContent()得到的InputStream,你做阅读的内容后,调用close()上的InputStream。

0

更多细节将有助于但这里有几个原因,这样的事情可能会发生:你一直开到同一服务器的连接

  1. ,并希望服务器,而服务器希望你重复使用它们关闭连接(http1.1),所以你实际上有泄漏。

  2. 您可能在导致此问题的基础传输(路由等)中存在连接问题,所以httpclient正在等待TCP超时。

  3. 服务器保持连接打开,因为它很忙但尚未回复。

相关问题