sofs1
sofs1

Reputation: 4176

How to resolve "Connect: operation timed out" and "RetryExec" error?

My Java program sends one thousand URL to a server and try to fetch result. I started getting this result. How to resolve this?

The code is given in this question 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

Upvotes: 2

Views: 10936

Answers (2)

songpeng
songpeng

Reputation: 1

related doc 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;
        }
    
    }


Upvotes: 0

Malt
Malt

Reputation: 30335

You're getting resets and timeouts so it seems that the web server is not listening for incoming connections.

Since you wrote that you "started" getting this, I'm assuming that you received responses for at least some requests, so the web server was up and was accessible. It might be some DoS protection that blocks clients that attempt to establish too many connections. In that case, if you don't control the web server, the only solution is to reduce the number of connections.

Upvotes: 2

Related Questions