3

经过一些使用,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 上没有看到任何“close()”或“release()”方法,我在这里没有做什么我需要做的事情?

根据请求,这是我的代码:

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()));
}
4

2 回答 2

8

您从 HttpResponse 中获得的InputStream内容需要close()调用它。HttpResponse 本身没有close()

例如,当您获取 HttpResponse 并调用getEntity()以获取 HttpEntity 时,调用getContent()以获取 InputStream,在您完成读取内容后,调用close()InputStream。

于 2012-07-09T16:41:00.973 回答
0

更多细节会有所帮助,但以下是可能发生此类事情的几个原因:

  1. 您不断打开与同一台服务器的连接并期望服务器关闭它们,而服务器希望您重用连接(http1.1),因此您实际上有泄漏。

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

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

于 2012-07-09T16:46:17.540 回答