Home » Java » java.io.IOException: Connection reset by peer error for asynchronous http client calls

java.io.IOException: Connection reset by peer error for asynchronous http client calls

Posted by: admin October 29, 2018 Leave a comment

Questions:

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();
                }
            }

        } 
Answers: