如何解决"连接:操作超时"和" RetryExec"错误?

时间:2014-08-17 09:16:27

标签: java multithreading network-programming threadpool

我的Java程序将一千个URL发送到服务器并尝试获取结果。我开始得到这个结果。如何解决这个问题?

此问题中提供了代码Why does the following executor service java Thread program doesn't shut down?

   Aug 17, 2014 2:09:40 AM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURL:80: Connection reset
    Aug 17, 2014 2:09:40 AM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {}->MyURL:80
    org.apache.http.conn.HttpHostConnectException: Connect to MyAnotherURL:80 [MyAnotherURL/115.249.106.144] failed: Operation timed out
        at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:140)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
        at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
        at MyProgram$MyRunnable.run(MyProgram.java:224)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
        at java.lang.Thread.run(Thread.java:695)
    Caused by: java.net.ConnectException: Operation timed out
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:382)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:241)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:228)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:431)
        at java.net.Socket.connect(Socket.java:527)
        at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
        at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
        ... 14 more
    Aug 17, 2014 2:10:14 AM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURL:80: Connection reset
    Exception in thread "pool-1-thread-302" java.lang.NullPointerException
        at MyProgram$MyRunnable.run(MyProgram.java:243)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
        at java.lang.Thread.run(Thread.java:695)
    Aug 17, 2014 2:10:14 AM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {}->MyURL:80
    Aug 17, 2014 2:10:14 AM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURL:80: Connection reset
    Aug 17, 2014 2:10:14 AM org.apache.http.impl.execchain.RetryExec execute
    INFO: Retrying request to {}->MyURL:80
    Aug 17, 2014 2:10:14 AM org.apache.http.impl.execchain.RetryExec execute
    INFO: I/O exception (java.net.SocketException) caught when processing request to {}->MyURL:80: Connection reset
    Aug 17, 2014 2:10:14 AM org.apache.http.impl.execchain.RetryExec execute

1 个答案:

答案 0 :(得分:2)

您正在进行重置和超时,因此似乎Web服务器没有侦听传入连接。

既然你写了“开始”得到这个,我假设你收到了至少一些请求的回复,所以网络服务器已经启动并且可以访问。可能是某些DoS保护会阻止尝试建立过多连接的客户端。在这种情况下,如果您不控制Web服务器,唯一的解决方案是减少连接数。