Category Archives: asynchttpclient

java.io.IOException connection reset by peer in asynchronous http client

I am using the Apache http asynchronous client library to make parallel HTTP calls and receive the response on a callback. I am getting the following error after my request when the code is deployed on the server.

java.io.IOException: Connection reset by peer
    at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:197)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:384)
    at org.apache.http.nio.reactor.ssl.SSLIOSession.receiveEncryptedData(SSLIOSession.java:450)
    at org.apache.http.nio.reactor.ssl.SSLIOSession.isAppInputReady(SSLIOSession.java:504)
    at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:120)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
    at java.lang.Thread.run(Thread.java:745)

here are my dependencies

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
        <version>4.1.3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4</version>
    </dependency>

how do I solve this? Thanks in advance.

Adding my code for reference

        CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().build();
        try {
            httpclient.start();
            final HttpGet[] requests = new HttpGet[] {new HttpGet("https://www.api.com/api/api1"),
                    new HttpGet("https://www.api.com/api/api2"),
                    new HttpGet("https://www.api.com/api/api3")};
            final CountDownLatch latch = new CountDownLatch(requests.length);
            final LocationData location= new LocationData();
            for (final HttpGet request: requests) {
                request.addHeader("Authorization", "Bearer "+token);
                httpclient.execute(request, new FutureCallback<HttpResponse>() {

                    @Override
                    public void completed(final HttpResponse response) {

                        latch.countDown();

                            BufferedReader in = null;
                            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                            String data = null;

                            while((data =in.readLine())!= null)
                            {
                            //process the data
                            }
                    }


                    @Override
                    public void failed(final Exception ex) {
                        latch.countDown();
                        System.out.println(request.getRequestLine() + "->" + ex);
                        ex.printStackTrace();
                    }

                    @Override
                    public void cancelled() {
                        latch.countDown();
                        System.out.println(request.getRequestLine() + " cancelled");
                    }

                });
            }
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //some more processinng here 
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        } 

Anyhelp would be appriciated. Anyone?

EDIT adding the logs from the server

status code | last accessed at             | api

200         | 2018-05-16 10:42:05.241000+0000 |  /api1
499         | 2018-05-16 10:42:05.290000+0000 | /api2
499         | 2018-05-16 10:42:05.416000+0000 |   /api3
200         | 2018-05-16 10:42:05.255000+0000 |  /api1
499         | 2018-05-16 10:42:05.415000+0000 | /api2
499         | 2018-05-16 10:42:05.425000+0000 |   /api3
499         | 2018-05-16 10:42:05.416000+0000 |  /api1
200         | 2018-05-16 10:42:05.256000+0000 | /api2
499         | 2018-05-16 10:42:05.460000+0000 |   /api3

Apache HttpAsyncClient thread count configuration

I'm trying to use AsyncRestTemplate with custom Apache HttpAsyncClient.

First i used simple new AsyncRestTemplate(); And when i added logs with thread name to future callback i saw that all of them was different (SimpleAsyncTaskExecutor-1..n).

Later i needed to implement custom ssl logic in my AsyncRestTemplate, so i added HttpComponentsAsyncClientHttpRequestFactory to constructor.

Then i had some issues in my BL so i started to debug and saw that only two threads was used in future callback I/O dispatcher 1 and I/O dispatcher 2.

I tried to use this configuration but i still have only two threads.

return new AsyncRestTemplate(
       new HttpComponentsAsyncClientHttpRequestFactory(
               HttpAsyncClients.custom()
                    .setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(20).build())
                    .build()
       )
);

What i'm doing wrong?

Apache Http HttpAsyncClient NoClassDefFoundError

I'm using apache http async client library in order to create async http requests.

added to my maven dependencies:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
        <version>4.1.2</version>
    </dependency>

My code is using HttpAsyncClientBuilder in order to build the client. In runtime I get an exception

java.lang.NoClassDefFoundError: org/apache/http/conn/util/PublicSuffixMatcherLoader

It seems that this jar is missing some dependencies. Any advice?

Not getting response with Http Async Client

I am stuck with this weird situation where sometimes my HTTP requests don't go out or I don't get a HTTP response to my request sporadically. My application makes several (100s) http requests to other 3rd party service periodically most of which work absolutely fine. I use the CloseableHttpAsyncClient (Version 4.0) with a custom HttpRequestIntercerptor and HttpResponseInterceptor. These were mainly added for debugging purpose with the RequestInterceptor is the last interceptor in the chain and the ResponseInterceptor is the first one. The idea was to log each http request at the last stage before it sends the actual request and to log each http response when it is first received.

I have the following pattern to setup the async client:

    HttpAsyncClientBuilder asyncClientBuilder = HttpAsyncClientBuilder.create();
    asyncClientBuilder.addInterceptorLast(new MyHttpRequestInterceptor());
    asyncClientBuilder.addInterceptorFirst(new MyHttpResponseInterceptor());
    IOReactorConfig reactorConfig = IOReactorConfig.DEFAULT;

    reactorConfig.setConnectTimeout(5 * 60 * 1000); // 5 mins 
    reactorConfig.setSoTimeout(5 * 60 * 1000); // 5 mins
    asyncClientBuilder.setDefaultIOReactorConfig(reactorConfig);
    System.setProperty("http.maxConnections", "100");

    this.asyncHttpClient = asyncClientBuilder.useSystemProperties().build();
    this.asyncHttpClient.start();

To make the request I do:

HttpGet httpGet = new HttpGet("some url");
asyncHttpClient.execute(httpGet, new AsyncHTTPResponseHandler(requestMetadata));

Here is my AsyncHTTPResponseHandler class:

class AsyncHTTPResponseHandler implements FutureCallback<HttpResponse> {

    // local copy of the request for reference while processing the response.
    private RequestMetadata requestMetadata;

    public AsyncHTTPResponseHandler(final RequestMetadata requestMetadata) {
        this.setRequestMetadata(requestMetadata);
        Thread.currentThread().setUncaughtExceptionHandler(new HttpUncaughtExceptionHandler(requestMetadata));
    }

    @Override
    public void cancelled() {
        logger.error("AsyncHTTPResponseHandler#Http request id: {} cancelled",
                requestMetadata.getRequestId()));
    }

    @Override
    public void completed(HttpResponse response) {
        logger.debug("Received HTTP Response for request id: {}",
                requestMetadata.getRequestId());
        //handleHttpResponse(requestMetadata, response);
    }

    @Override
    public void failed(Exception e) {
        logger.error("AsyncHTTPResponseHandler#Error in Http request id: " + requestMetadata.getRequestId(), e);
    }

}

Based on this setup, I see the following cases based on my interceptors logs: 1. My application http request triggers an asyncclient HttpRequest and I get the HttpResponse -- Success. 2. My application http request triggers an asyncclient HttpRequest (the interceptor logs it) and I don't get the HttpResponse for this request --- Don't know why? 3. My application http request does not trigger an asyncclient HttpRequest (the interceptor does not log it) and I don't get the HttpResponse for this request --- Don't know why?

Any tips or suggestions on what I can do fix this or debug this problem further?

Thanks!!