Apache 的 HttpAsyncClient 在执行后从不 returns

Apache's HttpAsyncClient never returns after execute

我正在使用 HttpAsyncClient 和 Jsoup 为特定站点编写网络抓取工具。我正在使用一个 HttpAsyncClient 实例,它执行来自不同线程的多个请求。几次请求成功后,客户端好像挂了,不会再执行更多的请求了。 execute 方法 returns 对象的 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 and you can run it yourself (it has a main method) if you create a maven project with these依赖关系。

感谢您的帮助。

我似乎是通过在同一 HttpAsyncClient 返回的 FutureCallback 的 completed 方法中调用 execute 方法而导致死锁的。我更改了代码,以便 completed 方法只是 returns 方法完成后处理的链接列表,现在一切正常。