当CountDownLatch计数为零时,丢失的HTTP请求在哪里?

时间:2018-10-29 06:17:25

标签: apache-httpasyncclient connectionexception

我正在测试使用 CloseableHttpAsyncClient 客户端发送一组HTTP请求的多线程代码(请参见下面的代码片段)。

我正在获得以下输出:

 Failed ->java.io.IOException: Connection reset by peer-null
 Failed ->org.apache.http.ConnectionClosedException: Connection closed-null
 Thread: 0-Time: 2955ms-Completed: 1000-Failed: 0-Cancelled: 0- Countdown: 0
 Thread: 1-Time: 2855ms-Completed: 999-Failed: 0-Cancelled: 0-Countdown: 0
 Thread: 2-Time: 2741ms-Completed: 999-Failed: 1-Cancelled: 0-Countdown: 0
 Thread: 3-Time: 2678ms-Completed: 999-Failed: 1-Cancelled: 0-Countdown: 0
 Thread: 4-Time: 2654ms-Completed: 1000-Failed: 0-Cancelled: 0-Countdown: 0

因此,两个线程中的两个正确执行了所有1000个请求,另外两个线程中的连接错误被正确捕获,其中一个线程(第1个)完成了999个请求,没有任何失败通知或取消。

我的问题是:

  1. 失败方法中是否有任何方法可以了解哪个是失败请求,所以我可以进行后处理以重新发送那些失败请求?

  2. 为什么在没有完成所有请求的情况下,如果没有失败,取消或异常情况导致倒数次数达到0?

    class AsynchThread extends Thread{      
        CloseableHttpAsyncClient httpclient;
        int n;
        int ncompleted =0;
        int nfailed =0;
        int ncancelled =0;
        long time;
        CountDownLatch latch;
    
        public AsynchThread(CloseableHttpAsyncClient httpclient, int n) throws IOReactorException {
            this.jobs = jobs;
            this.httpclient = httpclient;
            this.n = n;
        }
    
        public void process() throws InterruptedException, IOException {
            latch = new CountDownLatch(n);
            long starttime = System.currentTimeMillis();
            for (int v=0;v<n; v++) {
                    HttpPost httppost = ...
                    httpclient.execute(httppost, new FutureCallback<HttpResponse>() {
    
                   public void completed(final HttpResponse response) {
                        latch.countDown();
                        ncompleted += 1;
                    }
    
                    public void failed(final Exception ex) {
                         latch.countDown();
                         nfailed += 1;
                         System.out.println("Failed ->" + ex);
                    }
    
                    public void cancelled() {
                        latch.countDown();
                        ncancelled += 1;
                        System.out.println("Cancelled ->" + ex);
                   }
              });
            }
            latch.await();
            time = System.currentTimeMillis()-starttime;
        }
    
        public void run() {
            try {
                process();
            }catch(Exception e) {
                System.out.println(e.getStackTrace());
            }
        }
    }
    
    public static void main(final String[] args) throws Exception {
        CloseableHttpAsyncClient httpclient = ...
        int n = 5;
        int nprocthread = 1000;
        AsynchThread[] threads = new AsynchThread[n];
        for (int i=0; i<n; i++) {
            threads[i] = acall.createThread(httpclient, nprocthread);
            threads[i].run();
        }
        for(int i = 0; i < threads.length; i++)
             threads[i].join();
        for(int i = 0; i < threads.length; i++) {
            System.out.println("Thread: " + i + "-Time: " + threads[i].time + "ms-Completed: " + 
                            threads[i].ncompleted + "-Failed: " + threads[i].nfailed + "-Cancelled: " + 
                            threads[i].ncancelled + "-Countdown: " + threads[i].latch.getCount());
        }
    }
    

非常感谢。

1 个答案:

答案 0 :(得分:0)

添加标题“ Connection:close”时,所有问题均已解决