少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          package com.abin.lee.junit.httpasyncclient.example;

          import java.util.List;
          import java.util.concurrent.ExecutorService;
          import java.util.concurrent.Executors;
           
           import org.apache.http.HttpEntity;
          import org.apache.http.HttpResponse;
          import org.apache.http.client.HttpClient;
          import org.apache.http.client.methods.HttpGet;
          import org.apache.http.conn.ClientConnectionManager;
          import org.apache.http.conn.params.ConnManagerParams;
          import org.apache.http.conn.scheme.PlainSocketFactory;
          import org.apache.http.conn.scheme.Scheme;
          import org.apache.http.conn.scheme.SchemeRegistry;
          import org.apache.http.impl.client.DefaultHttpClient;
          import org.apache.http.impl.conn.PoolingClientConnectionManager;
          import org.apache.http.params.BasicHttpParams;
          import org.apache.http.params.HttpConnectionParams;
          import org.apache.http.params.HttpParams;
          import org.apache.http.protocol.BasicHttpContext;
          import org.apache.http.protocol.HttpContext;
          import org.apache.http.util.EntityUtils;
          import org.junit.Test;
           
           public class ThreadPoolHttpClient {
               // 線程池
               private ExecutorService exe = null;
               // 線程池的容量
               private static final int POOL_SIZE = 20;
               private HttpClient client = null;
               String[] urls=null;
               public ThreadPoolHttpClient(String[] urls){
                   this.urls=urls;
               }
               @Test
               public void test() throws Exception {
                   exe = Executors.newFixedThreadPool(POOL_SIZE);
                   HttpParams params =new BasicHttpParams();
                   /* 從連接池中取連接的超時時間 */
                   ConnManagerParams.setTimeout(params, 1000);
                  /* 連接超時 */
                   HttpConnectionParams.setConnectionTimeout(params, 2000);
                   /* 請求超時 */
                   HttpConnectionParams.setSoTimeout(params, 4000);
                   SchemeRegistry schemeRegistry = new SchemeRegistry();
                  schemeRegistry.register(
                           new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
           
                   //ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
                   PoolingClientConnectionManager cm=new PoolingClientConnectionManager(schemeRegistry);
                   cm.setMaxTotal(10);
                   final HttpClient httpClient = new DefaultHttpClient(cm,params);
           
                   // URIs to perform GETs on
                   final String[] urisToGet = urls;
                   /* 有多少url創建多少線程,url多時機子撐不住
          56         // create a thread for each URI
          57         GetThread[] threads = new GetThread[urisToGet.length];
          58         for (int i = 0; i < threads.length; i++) {
          59             HttpGet httpget = new HttpGet(urisToGet[i]);
          60             threads[i] = new GetThread(httpClient, httpget);           
          61         }
          62         // start the threads
          63         for (int j = 0; j < threads.length; j++) {
          64             threads[j].start();
          65         }
          66
          67         // join the threads,等待所有請求完成
          68         for (int j = 0; j < threads.length; j++) {
          69             threads[j].join();
          70         }
          71         使用線程池*/
                   for (int i = 0; i < urisToGet.length; i++) {
                       final int j=i;
                       System.out.println(j);
                       HttpGet httpget = new HttpGet(urisToGet[i]);
                       exe.execute( new GetThread(httpClient, httpget));
                   }
                  
                  
                   //創建線程池,每次調用POOL_SIZE
                   /*
                   for (int i = 0; i < urisToGet.length; i++) {
                       final int j=i;
                       System.out.println(j);
                       exe.execute(new Thread() {
                           @Override
                           public void run() {
                               this.setName("threadsPoolClient"+j);
                              
                                   try {
                                       this.sleep(100);
                                       System.out.println(j);
                                   } catch (InterruptedException e) {
                                       // TODO Auto-generated catch block
                                       e.printStackTrace();
                                   }
                                  
                                   HttpGet httpget = new HttpGet(urisToGet[j]);
                                   new GetThread(httpClient, httpget).get();
                               }
                              
                              
                          
                       });
                   }
                  
                   */
                   //exe.shutdown();
                   System.out.println("Done");
               }
               static class GetThread extends Thread{
                  
                   private final HttpClient httpClient;
                   private final HttpContext context;
                   private final HttpGet httpget;
                  
                   public GetThread(HttpClient httpClient, HttpGet httpget) {
                       this.httpClient = httpClient;
                       this.context = new BasicHttpContext();
                       this.httpget = httpget;
                   }
                   @Override
                   public void run(){
                       this.setName("threadsPoolClient");
                       try {
                           Thread.sleep(5000);
                       } catch (InterruptedException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                       }
                       get();
                   }
                  
                   public void get() {
                       try {
                           HttpResponse response = this.httpClient.execute(this.httpget, this.context);
                           HttpEntity entity = response.getEntity();
                           if (entity != null) {
                               System.out.println(this.httpget.getURI()+": status"+response.getStatusLine().toString());
                           }
                           // ensure the connection gets released to the manager
                           EntityUtils.consume(entity);
                       } catch (Exception ex) {
                           this.httpget.abort();
                       }finally{
                           httpget.releaseConnection();
                       }
                   }
               }
           }

          posted on 2013-01-08 23:51 abin 閱讀(3016) 評論(0)  編輯  收藏 所屬分類: httpClient
          主站蜘蛛池模板: 广平县| 新龙县| 东港市| 靖州| 固安县| 榆中县| 都昌县| 宝坻区| 观塘区| 南阳市| 广平县| 樟树市| 丰都县| 四平市| 洪湖市| 澄迈县| 任丘市| 望都县| 阿城市| 明光市| 泗阳县| 宝山区| 侯马市| 宁强县| 洛扎县| 衡阳县| 永泰县| 霍城县| 屏山县| 瑞昌市| 额济纳旗| 合作市| 六盘水市| 田阳县| 抚顺县| 永福县| 江源县| 夏津县| 留坝县| 瑞金市| 丽江市|