<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    少年阿賓

    那些青春的歲月

      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
    主站蜘蛛池模板: 亚洲精品一二三区| 国产片免费福利片永久| xxxxx做受大片在线观看免费| 免费看搞黄视频网站| 国产男女猛烈无遮挡免费视频| 亚洲AV无码一区二区大桥未久| 成人奭片免费观看| 亚洲一区二区三区在线网站| 37pao成人国产永久免费视频| 亚洲国产精品SSS在线观看AV| 国产精品一区二区三区免费| 在线看片无码永久免费aⅴ| 亚洲综合亚洲综合网成人| 亚洲国产精品日韩在线观看| 国产在线观a免费观看| 亚洲男人第一无码aⅴ网站| 日韩国产欧美亚洲v片| 男人的好免费观看在线视频| 亚洲视频在线观看网址| 69视频免费在线观看| 亚洲精品综合久久中文字幕| 一级毛片免费观看| 亚洲最大在线观看| 一二三四免费观看在线视频中文版 | 成人精品一区二区三区不卡免费看| 亚洲国产精品毛片av不卡在线| 亚洲日韩精品A∨片无码加勒比| 四虎在线视频免费观看视频| 亚洲精品二三区伊人久久| 黄瓜视频高清在线看免费下载| 亚洲日韩国产精品乱-久| a毛片基地免费全部视频| 亚洲综合国产精品| 91大神在线免费观看| 激情内射亚洲一区二区三区爱妻| 丁香花免费完整高清观看| 亚洲性无码AV中文字幕| 国产一级淫片a免费播放口之 | 国产精品福利片免费看| 久久亚洲精品成人| 成人福利免费视频|