gpt4 book ai didi

java - Apache HttpClient 连接耗尽,但我不确定为什么?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:14:06 24 4
gpt4 key购买 nike

经过一些使用后,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()));
}

最佳答案

您从 HttpResponse 获得的 InputStream 需要对其调用 close()。 HttpResponse 本身没有close()

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

关于java - Apache HttpClient 连接耗尽,但我不确定为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11399343/

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