gpt4 book ai didi

java - Apache 的 HttpAsyncClient 在执行后永远不会返回

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:29 26 4
gpt4 key购买 nike

我正在使用 HttpAsyncClient 和 Jsoup 为特定站点编写网络抓取工具。我正在使用 HttpAsyncClient 的一个实例,它执行来自不同线程的多个请求。在几次成功的请求之后,客户端似乎挂起并且不会执行更多请求。调用 execute 方法会正常返回 FutureCallback 对象,但它的两个方法(completedfailedcancelled)都不会执行。

通过查看日志,似乎请求从未发送过:

33572 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.MainClientExec  - [exchange: 5] start execution
33573 [I/O dispatcher 1] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: default
33575 [I/O dispatcher 1] DEBUG org.apache.http.client.protocol.RequestAddCookies - Cookie [version: 0][name: .ASPXAUTH][value: removed][domain: www.predictit.org][path: /][expiry: null] match [(secure)www.predictit.org:443/Home/SingleOption]
33576 [I/O dispatcher 1] DEBUG org.apache.http.client.protocol.RequestAddCookies - Cookie [version: 0][name: ARRAffinity][value: removed][domain: www.predictit.org][path: /][expiry: null] match [(secure)www.predictit.org:443/Home/SingleOption]
33577 [I/O dispatcher 1] DEBUG org.apache.http.client.protocol.RequestAddCookies - Cookie [version: 0][name: ASP.NET_SessionId][value: removed][domain: www.predictit.org][path: /][expiry: null] match [(secure)www.predictit.org:443/Home/SingleOption]
33579 [I/O dispatcher 1] DEBUG org.apache.http.client.protocol.RequestAddCookies - Cookie [version: 0][name: __RequestVerificationToken][value: removed][domain: www.predictit.org][path: /][expiry: null] match [(secure)www.predictit.org:443/Home/SingleOption]
33579 [I/O dispatcher 1] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
33580 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.InternalHttpAsyncClient - [exchange: 5] Request connection for {s}->https://www.predictit.org:443
33580 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager - Connection request: [route: {s}->https://www.predictit.org:443][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
33581 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl - http-outgoing-0 192.168.1.6:38559<->23.96.96.142:443[ACTIVE][r:r][ACTIVE][r][NOT_HANDSHAKING][0][0][0][0]: Set timeout 0
33581 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager - Connection leased: [id: http-outgoing-0][route: {s}->https://www.predictit.org:443][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
33582 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.client.InternalHttpAsyncClient - [exchange: 5] Connection allocated: CPoolProxy{http-outgoing-0 [ACTIVE]}
33582 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl - http-outgoing-0 192.168.1.6:38559<->23.96.96.142:443[ACTIVE][r:r][ACTIVE][r][NOT_HANDSHAKING][0][0][0][0]: Set attribute http.nio.exchange-handler
33583 [I/O dispatcher 1] DEBUG org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionImpl - http-outgoing-0 192.168.1.6:38559<->23.96.96.142:443[ACTIVE][rw:r][ACTIVE][rw][NOT_HANDSHAKING][0][0][0][0]: Event set [w]

它只是卡在最后一行,什么也没有发生。这似乎是某种死锁,但 CloseableHttpAsyncClient 应该是线程安全的。

您可以看到代码here如果你使用 these 创建一个 Maven 项目,你可以自己运行它(它有一个 main 方法)依赖关系。

感谢您的帮助。

最佳答案

看来我通过调用同一 HttpAsyncClient 返回的 FutureCallback 的 completed 方法内的 execute 方法导致了死锁。我更改了代码,以便 completed 方法仅返回在该方法完成后处理的链接列表,现在一切正常。

关于java - Apache 的 HttpAsyncClient 在执行后永远不会返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32031846/

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