Category Archives: asynccallback

Intermittent ConnectionClosedException with httpasynclient

we have three applications A,B and C deployed on same tomcat server.There is an HTTP call that happens between A to B(REST CALL) and Another http call from B to C(REST CALL)

We were using synchronous HTTP call initially,recently we changed our code to Asynchronous call. We have used HttpAsyncClients for (Application A to call Application B) and for (Application B to call Application C)..

we are getting intermittent org.apache.http.ConnectionClosedException: Some time we are getting this exception in Logs of application A(Async Client to B) and some time in Application B(Async Client to C)

 CloseableHttpAsyncClient  client = HttpAsyncClients.custom()
                    .setMaxConnPerRoute(100))
                    .setMaxConnTotal(config.getInt(150)).build();

and for Every request we gave timeout of

final RequestConfig params = RequestConfig.custom().setConnectTimeout(300000) // 5min
                    .setSocketTimeout(300000).setConnectionRequestTimeout(300000).build();

Please find the stack trace below:

org.apache.http.ConnectionClosedException: Connection closed
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.connectionAllocated(AbstractClientExchangeHandler.java:320) [httpasyncclient-4.1.2.jar:na]
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.access$000(AbstractClientExchangeHandler.java:62) [httpasyncclient-4.1.2.jar:na]
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler$1.completed(AbstractClientExchangeHandler.java:373) [httpasyncclient-4.1.2.jar:na]
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler$1.completed(AbstractClientExchangeHandler.java:369) [httpasyncclient-4.1.2.jar:na]
        at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119) [httpcore-4.4.4.jar:na]
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalPoolEntryCallback.completed(PoolingNHttpClientConnectionManager.java:494) [httpasyncclient-4.1.2.jar:4.1.2]
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalPoolEntryCallback.completed(PoolingNHttpClientConnectionManager.java:477) [httpasyncclient-4.1.2.jar:4.1.2]
        at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119) [httpcore-4.4.4.jar:na]
        at org.apache.http.nio.pool.AbstractNIOConnPool.fireCallbacks(AbstractNIOConnPool.java:456) [httpcore-nio-4.4.5.jar:na]
        at org.apache.http.nio.pool.AbstractNIOConnPool.lease(AbstractNIOConnPool.java:286) [httpcore-nio-4.4.5.jar:na]
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(PoolingNHttpClientConnectionManager.java:266) [httpasyncclient-4.1.2.jar:4.1.2]
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.requestConnection(AbstractClientExchangeHandler.java:363) [httpasyncclient-4.1.2.jar:na]