Search code examples
javatomcatconnection-timeoutunirest

UnirestException: connect timed out on Tomcat 7


At work, I am trying to deploy a java 8-based application on Tomcat 7 server.

The application should call some RESTful api based on another server (which is still ours). By reading some resources online I decided to use a HTTP request client library called Unirest.

I tried to call the api using Postman and everything works fine. I deployed my application on my LOCAL Tomcat 7 server and it runs smoothly without problems and Unirest does its job correctly.

When I deploy the application on the Tomcat 7 server, every Unirest call, leads to a connect timeout.

This is the stacktrace:

    com.mashape.unirest.http.exceptions.UnirestException: org.apache.http.conn.ConnectTimeoutException: Connect to srv-erp2web.rold.corp:80 [srv-erp2web.rold.corp/192.168.5.100] failed: connect timed out
        at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:143)
        at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
        at com.rold.industry.api.common.ApiCaller.getAccessToken(ApiCaller.java:66)
        at com.rold.industry.context.ContextListener.contextInitialized(ContextListener.java:118)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5010)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5504)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1073)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:553)
        at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1648)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1445)
        at org.apache.catalina.manager.HTMLManagerServlet.upload(HTMLManagerServlet.java:284)
        at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:206)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:213)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:610)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to srv-erp2web.rold.corp:80 [srv-erp2web.rold.corp/192.168.5.100] failed: connect timed out
        at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:134)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319)
        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.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 com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:138)
        ... 46 more
Caused by: java.net.SocketTimeoutException: connect timed out
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
        at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:125)
        ... 57 more

I haven't find any clue on the internet about how this can happen. The really strange thing is that Unirest calls correctly the api on my local Tomcat.

Here a snapshot of the code that I use to call the api:

public static String getAccessToken() {
        logger.log(Level.FINE, "calling smartfabauth to get authentication token");
        String accessToken = "";
        try {
            Unirest.setTimeouts(0, 0); // NO TIMEOUT
            HttpResponse<JsonNode> response = Unirest.post(POST_TOKEN_ENDPOINT)
                    .header("content-type", "application/x-www-form-urlencoded")
                    .header("cache-control", "no-cache")
                    .body("grant_type=" + GRANT_TYPE + "&client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&scope=" + SCOPE)
                    .asJson();

            accessToken = response.getBody().getObject().getString("access_token");

        } catch (UnirestException | JSONException e) {
            logger.log(Level.SEVERE, "[Exception] getAccessToken", e);
        }

        logger.log(Level.FINE, "authentication token obtained");
        return accessToken;
    }

Note that POST_TOKEN_ENDPOINT, GRANT_TYPE, CLIENT_ID, CLIENT_SECRET and SCOPE are some constants declared in class necessary to correctly call the api.

Any help would be apreciated.

Thanks in advance.


Solution

  • I suspect this is a firewall/proxy issue.

    You can check the connectivity by pinging srv-erp2web.rold.corp from the machine where you want to run Tomcat 7.

    If you see packet loss, try below tracert(windows) or traceroute(*nix) machines.

    Usual syntax is tracert srv-erp2web.rold.corp

    Above tracert shows the request path from source to destination. And also tells where the request is failing.

    If the packet loss is because of proxy server, provide proxy host, port and credentials if any.

    If the packet loss is because of firewall, enable the port.

    Hope this helps!