java - 如何解决 "Connect: operation timed out"和 "RetryExec"错误?

标签 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

最佳答案

您遇到重置和超时,因此网络服务器似乎没有监听传入连接。

自从您写道您“开始”收到此消息以来,我假设您至少收到了一些请求的响应,因此网络服务器已启动并且可以访问。可能是某种 DoS 保护阻止了尝试建立过多连接的客户端。在这种情况下,如果您不控制 Web 服务器,唯一的解决方案就是减少连接数。

关于java - 如何解决 "Connect: operation timed out"和 "RetryExec"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25347864/

相关文章:

java - 如何使用输入流java的markSupported特性

multithreading - 如何只使用程序可用的带宽

iOS:核心图像和多线程应用程序

java - java中的 "simple"线程池

C - 程序卡住; futex_wait_queue_me

java - 为什么我的内容 uri 出现错误?

Ruby 线程和变量

java - 在android中实现Parcelable,onRestoreInstanceState没有被调用

java - 是否可以使用 Jackson 将静态类转换为 JSON?

java - 键入时,从存储的现有文本中将单词(文本)插入到 JTextArea 中