1

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
DeathMate
  • 17
  • 1
  • 4

1 Answers1

0

Unless you have error logs from the remote end, this is going to be a blind ride. You might need to figure out which service - api1, api2, or api3 - hung up. If it’s consistently the same one, you have grounds to investigate specific service logs. If it’s random or always all, investigate the API gateway error logs, Nginx, httpd, other. SSL negotiation is usually a major suspect, but we’re flying blind without the logs above.

Timir
  • 1,395
  • 8
  • 16
  • added the logs. Could you please explain how SSL negotiation could be a roadblock. – DeathMate May 21 '18 at 06:01
  • These are the access logs, not error logs. You can still tell from them that not all API requests succeeded, many returned HTTP 499. [This discussion](https://stackoverflow.com/q/12973304/1110636) may be relevant in your case, and the answers will point to debugging your application further. – Timir May 21 '18 at 10:24