我正在使用HttpAsyncClient和Jsoup为特定站点编写Web抓取工具。我正在使用HttpAsyncClient的一个实例,该实例执行来自不同线程的多个请求。在几次成功的请求之后,客户端似乎挂起并且不会执行更多请求。调用execute
方法通常会返回FutureCallback
对象,但是不会执行其任何方法(completed
,failed
或cancelled
)。
通过查看日志,似乎从未发送过请求:
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依赖项的maven项目,则可以看到代码these,并且可以自己运行(它有一个主要方法)。
我感谢您的帮助。
最佳答案
看来我是通过调用同一HttpAsyncClient返回的FutureCallback的execute
方法内的completed
方法引起死锁的。我更改了代码,以使completed
方法仅返回方法完成后即可处理的链接列表。