java.io.IOException:异步http客户端调用的对等错误连接重置

时间:2018-05-18 11:33:30

标签: java http apache-httpcomponents apache-httpasyncclient

我使用Apache http异步客户端库进行并行HTTP调用并在回调上接收响应。在代码部署在服务器上的请求后,我收到以下错误。

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)

这是我的依赖

<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>

我该如何解决这个问题?提前谢谢。

添加我的代码以供参考

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

        } 

任何帮助都会受到影响。任何人吗?

0 个答案:

没有答案