HttpAsyncClient 的简单使用

下载地址:http://hc.apache.org/downloads.cgi

在NetBeans中导入以下jar文件:

1:一次请求:

 public static void oneReuest(){
        final CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        httpClient.start();
        final HttpGet request = new HttpGet("http://www.apache.org/");
        final Future future = httpClient.execute(request, null);
        try {
            HttpResponse response = (HttpResponse) future.get();
            System.out.println("Response:" + response.getStatusLine());
            System.out.println("Shutting down");
        } catch (Exception ex) {
            Logger.getLogger(Httpasyncclient.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                httpClient.close();
            } catch (IOException ex) {
                Logger.getLogger(Httpasyncclient.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        System.out.println("执行完毕");
    }

2:多次异步请求:

 public static void moreRequest(){
        final RequestConfig requestConfitg = RequestConfig.custom()
                .setSocketTimeout(3000)
                .setConnectTimeout(3000).build();
        
        final CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom()
                .setDefaultRequestConfig(requestConfitg)
                .build();
        
        httpClient.start();
        
        final HttpGet[] requests = new HttpGet[]{
            new HttpGet("http://www.apache.org/"),
            new HttpGet("http://www.baidu.com/"),
            new HttpGet("http://www.oschina.net/")
        };
        
        final CountDownLatch latch = new CountDownLatch(requests.length);
        for(final HttpGet request: requests){
            
                httpClient.execute(request, new FutureCallback(){
                    @Override
                    public void completed(Object obj) {
                       final HttpResponse response = (HttpResponse)obj;
                       latch.countDown();
                       System.out.println(request.getRequestLine() + "->" + response.getStatusLine());
                    }

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

                    @Override
                    public void cancelled() {
                        latch.countDown();
                        System.out.println(request.getRequestLine() + "cancelled");
                    }
                });
         }       
        
        try {
            latch.await();
            System.out.println("Shutting Down");
        } catch (InterruptedException ex) {
            Logger.getLogger(Httpasyncclient.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                httpClient.close();
            } catch (IOException ex) {
                Logger.getLogger(Httpasyncclient.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        System.out.println("Finish!");
    }

运行结果:

run:
GET http://www.baidu.com/ HTTP/1.1->HTTP/1.1 200 OK
GET http://www.oschina.net/ HTTP/1.1->HTTP/1.1 200 OK
GET http://www.apache.org/ HTTP/1.1->HTTP/1.1 200 OK
Shutting Down
Finish!
成功构建 (总时间: 2 秒)

可以看出是异步执行的!不是按照我们传入的URL参数顺序执行的!

 

posted @ 2014-01-05 22:15  yshy  阅读(9377)  评论(0编辑  收藏  举报