【问题标题】:How to resolve "Connect: operation timed out" and "RetryExec" error?如何解决“连接:操作超时”和“RetryExec”错误?
【发布时间】:2014-08-17 09:16:27
【问题描述】:

我的 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

【问题讨论】:

    标签: java multithreading network-programming threadpool


    【解决方案1】:

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

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

    【讨论】:

    • 是的,最初我收到了大量请求的响应。现在,当我通过网络浏览器进行操作时,我也会得到响应,但是当我尝试通过我的程序请求一个请求(这是一个 url)时,我仍然没有得到它。我设置了连接超时和套接字超时。现在我得到“org.apache.http.conn.HttpHostConnectException:连接到 MyURl 失败:操作超时”
    • 我遇到了同样的问题。你找到解决方案了吗?
    【解决方案2】:

    相关文档 https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.server.server.tomcat.keep-alive-timeout

    
    
        import org.apache.catalina.connector.Connector;
        import org.apache.coyote.AbstractProtocol;
        import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
        import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
        import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configuration;
        
        @Configuration
        public class Config {
        
            @Bean
            public ServletWebServerFactory servletContainer() {
                TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
                // close keepAlive
                tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {
                    @Override
                    public void customize(Connector connector) {
                        ((AbstractProtocol) connector.getProtocolHandler()).setKeepAliveTimeout(-1);
                    }
                });
                return tomcat;
            }
        
        }
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多