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

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

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

    少年阿賓

    那些青春的歲月

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      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();
             /* 從連接池中取連接的超時(shí)時(shí)間 */
             ConnManagerParams.setTimeout(params, 1000);
            /* 連接超時(shí) */
             HttpConnectionParams.setConnectionTimeout(params, 2000);
             /* 請(qǐng)求超時(shí) */
             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創(chuàng)建多少線程,url多時(shí)機(jī)子撐不住
    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,等待所有請(qǐng)求完成
    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));
             }
            
            
             //創(chuàng)建線程池,每次調(diào)用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) 評(píng)論(0)  編輯  收藏 所屬分類: httpClient
    主站蜘蛛池模板: 国产成人va亚洲电影| 亚洲精彩视频在线观看| 久久精品国产亚洲av品善| 91免费在线播放| 亚洲色偷偷av男人的天堂| 色播在线永久免费视频网站| 亚洲免费视频一区二区三区| 猫咪www免费人成网站| 免费人成视频在线观看视频 | a色毛片免费视频| 久久精品亚洲男人的天堂| 无码亚洲成a人在线观看| 色吊丝永久在线观看最新免费| 国内外成人免费视频| 亚洲中文字幕一二三四区| 97人伦色伦成人免费视频| 亚洲无码一区二区三区| 黄瓜视频高清在线看免费下载| 亚洲高清免费视频| 国产黄在线观看免费观看不卡| 91九色老熟女免费资源站| 久久久亚洲欧洲日产国码二区| 国产精品亚洲专区无码唯爱网 | 国产亚洲精品无码成人| 最好免费观看高清在线| 亚洲第一成年网站大全亚洲| 亚洲人成电影网站免费| 国产99久久亚洲综合精品| 一区国严二区亚洲三区| 日本三级在线观看免费| 亚洲国产高清视频在线观看| 日韩免费a级在线观看| 久久国产精品免费| 亚洲欧洲日产国产最新| 拔擦拔擦8x华人免费久久| 成人无码视频97免费| 91亚洲自偷在线观看国产馆| 免费一级毛片女人图片| 你懂的免费在线观看网站| 一本色道久久88—综合亚洲精品| 猫咪免费人成网站在线观看|