2015-08-16 155 views
0

我正在用HttpAsyncClient和Jsoup爲特定網站編寫Web刮取器。我正在使用一個HttpAsyncClient實例,它執行來自不同線程的多個請求。經過幾次成功的請求,客戶端似乎掛起並不會執行更多請求。調用execute方法通常會返回FutureCallback對象,但其方法(completedfailedcancelled)都不會執行。Apache的HttpAsyncClient在執行後永遠不會返回

通過查看日誌看來,如果請求永遠不會發送:

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項目,您可以自己運行它(它有一個主要方法)。

我感謝您的幫助。

回答

0

看起來我是通過在同一個HttpAsyncClient返回的FutureCallback的completed方法內調用execute方法導致死鎖的。我更改了代碼,以便completed方法返回方法完成後處理的鏈接列表,現在所有工作都可以使用。

+1

此處記錄問題和解決方法:https://issues.apache.org/jira/browse/HTTPCLIENT-1805 –

相關問題