少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          實(shí)例一:

          package com.abin.lee.async;

          import org.apache.http.HttpResponse;
          import org.apache.http.client.methods.HttpPost;
          import org.apache.http.impl.nio.client.DefaultHttpAsyncClient;
          import org.apache.http.nio.client.HttpAsyncClient;
          import org.apache.http.nio.reactor.IOReactorException;
          import org.junit.Test;

          import java.io.IOException;
          import java.util.concurrent.Future;

          /**
           * Created with IntelliJ IDEA.
           * User: abin
           * Date: 13-4-23
           * Time: 下午6:13
           * To change this template use File | Settings | File Templates.
           */
          public class HttpAsyncClientTest {
              private static final String HttpUrl="http://localhost:8100/MyThread/HttpClientPostProxyServlet";
              @Test
              public void testHttpAsyncClient() throws IOException {
                   HttpAsyncClient httpAsyncClient=new DefaultHttpAsyncClient();
                   httpAsyncClient.start();
                  HttpPost request=null;
                  try {
                      request=new HttpPost(HttpUrl);
                      Future<HttpResponse> future=httpAsyncClient.execute(request,null);
                      HttpResponse response=future.get();
                      System.out.println("response="+response);
                  }catch(Exception e){
                       e.printStackTrace();
                  }finally {
                      if(!request.isAborted()){
                          request.abort();;
                      }
                      httpAsyncClient.getConnectionManager().shutdown();
                  }

              }
          }








          實(shí)例二:

          package com.abin.lee.async;

          import org.apache.http.HttpResponse;
          import org.apache.http.client.methods.HttpGet;
          import org.apache.http.client.methods.HttpPost;
          import org.apache.http.concurrent.FutureCallback;
          import org.apache.http.impl.nio.client.DefaultHttpAsyncClient;
          import org.apache.http.nio.client.HttpAsyncClient;
          import org.junit.Test;

          import java.io.IOException;
          import java.util.concurrent.CountDownLatch;
          import java.util.concurrent.Future;

          /**
           * Created with IntelliJ IDEA.
           * User: abin
           * Date: 13-4-23
           * Time: 下午6:13
           * To change this template use File | Settings | File Templates.
           */
          public class HttpAsyncClientFutureCallBackTest {
              private static final String HttpUrl="http://localhost:8100/MyThread/HttpClientGetProxyServlet";
              private static final String HttpOneUrl="http://localhost:8100/MyThread/HttpClientGetOneServlet";
              private static final String HttpTwoUrl="http://localhost:8100/MyThread/HttpClientGetTwoServlet";
              @Test
              public void testHttpAsyncClientFutureCallBack() throws IOException {
                   HttpAsyncClient httpAsyncClient=new DefaultHttpAsyncClient();
                   httpAsyncClient.start();
                  HttpGet[] requests=null;
                  try {
                      requests=new HttpGet[]{new HttpGet(HttpUrl),new HttpGet(HttpOneUrl),new HttpGet(HttpTwoUrl)};
                      final CountDownLatch latch=new CountDownLatch(requests.length);
                      for(final HttpGet request:requests){
                          httpAsyncClient.execute(request,new FutureCallback<HttpResponse>() {
                              @Override
                              public void completed(HttpResponse httpResponse) {
                                  latch.countDown();
                                  System.out.println(request.getRequestLine()+"--->"+httpResponse.getStatusLine());
                                  //To change body of implemented methods use File | Settings | File Templates.
                              }

                              @Override
                              public void failed(Exception e) {
                                  latch.countDown();
                                  System.out.println(request.getRequestLine()+"-->"+e.getMessage());
                                  //To change body of implemented methods use File | Settings | File Templates.
                              }

                              @Override
                              public void cancelled() {
                                  latch.countDown();
                                  System.out.println(request.getRequestLine()+"--->"+" cancelled");
                                   //To change body of implemented methods use File | Settings | File Templates.
                              }
                          }) ;
                      }
                      latch.await();
                      System.out.println("shutting down");
                  }catch(Exception e){
                       e.printStackTrace();
                  }finally {
                      httpAsyncClient.getConnectionManager().shutdown();
                  }

              }
          }

          posted on 2013-04-28 16:26 abin 閱讀(2382) 評(píng)論(0)  編輯  收藏 所屬分類: httpClient
          主站蜘蛛池模板: 泸溪县| 苗栗市| 大竹县| 拜泉县| 临江市| 寿阳县| 华蓥市| 织金县| 密云县| 宁都县| 鄂尔多斯市| 南宫市| 新民市| 尼勒克县| 密云县| 保山市| 项城市| 繁昌县| 元氏县| 长寿区| 定南县| 社会| 监利县| 武清区| 永城市| 若羌县| 如皋市| 巴马| 靖宇县| 泸溪县| 观塘区| 准格尔旗| 顺昌县| 鹿邑县| 株洲县| 兴安县| 绥宁县| 广昌县| 虎林市| 福安市| 江源县|