ThreadSafeClientConnManager用来支持多线程的使用http client

ThreadSafeClientConnManager用来支持多线程的使用http client

  1. import org.apache.http.HttpEntity;    
  2. import org.apache.http.HttpResponse;    
  3. import org.apache.http.client.HttpClient;    
  4. import org.apache.http.client.methods.HttpGet;    
  5. import org.apache.http.impl.client.DefaultHttpClient;    
  6. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;    
  7. import org.apache.http.protocol.BasicHttpContext;    
  8. import org.apache.http.protocol.HttpContext;    
  9. import org.apache.http.util.EntityUtils;    
  10.     
  11. /**  
  12.  * An example that performs GETs from multiple threads.  
  13.  *  
  14.  */    
  15. public class ClientMultiThreadedExecution {    
  16.     
  17.     public static void main(String[] args) throws Exception {    
  18.         // Create an HttpClient with the ThreadSafeClientConnManager.    
  19.         // This connection manager must be used if more than one thread will    
  20.         // be using the HttpClient.    
  21.         ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();    
  22.         cm.setMaxTotal(100);    
  23.     
  24.         HttpClient httpclient = new DefaultHttpClient(cm);    
  25.         try {    
  26.             // create an array of URIs to perform GETs on    
  27.             String[] urisToGet = {    
  28.                 "http://hc.apache.org/",    
  29.                 "http://hc.apache.org/httpcomponents-core-ga/",    
  30.                 "http://hc.apache.org/httpcomponents-client-ga/",    
  31.                 "http://svn.apache.org/viewvc/httpcomponents/"    
  32.             };    
  33.     
  34.             // create a thread for each URI    
  35.             GetThread[] threads = new GetThread[urisToGet.length];    
  36.             for (int i = 0; i < threads.length; i++) {    
  37.                 HttpGet httpget = new HttpGet(urisToGet[i]);    
  38.                 threads[i] = new GetThread(httpclient, httpget, i + 1);    
  39.             }    
  40.     
  41.             // start the threads    
  42.             for (int j = 0; j < threads.length; j++) {    
  43.                 threads[j].start();    
  44.             }    
  45.     
  46.             // join the threads    
  47.             for (int j = 0; j < threads.length; j++) {    
  48.                 threads[j].join();    
  49.             }    
  50.     
  51.         } finally {    
  52.             // When HttpClient instance is no longer needed,    
  53.             // shut down the connection manager to ensure    
  54.             // immediate deallocation of all system resources    
  55.             httpclient.getConnectionManager().shutdown();    
  56.         }    
  57.     }    
  58.     
  59.     /**  
  60.      * A thread that performs a GET.  
  61.      */    
  62.     static class GetThread extends Thread {    
  63.     
  64.         private final HttpClient httpClient;    
  65.         private final HttpContext context;    
  66.         private final HttpGet httpget;    
  67.         private final int id;    
  68.     
  69.         public GetThread(HttpClient httpClient, HttpGet httpget, int id) {    
  70.             this.httpClient = httpClient;    
  71.             this.context = new BasicHttpContext();    
  72.             this.httpget = httpget;    
  73.             this.id = id;    
  74.         }    
  75.     
  76.         /**  
  77.          * Executes the GetMethod and prints some status information.  
  78.          */    
  79.         @Override    
  80.         public void run() {    
  81.     
  82.             System.out.println(id + " - about to get something from " + httpget.getURI());    
  83.     
  84.             try {    
  85.     
  86.                 // execute the method    
  87.                 HttpResponse response = httpClient.execute(httpget, context);    
  88.     
  89.                 System.out.println(id + " - get executed");    
  90.                 // get the response body as an array of bytes    
  91.                 HttpEntity entity = response.getEntity();    
  92.                 if (entity != null) {    
  93.                     byte[] bytes = EntityUtils.toByteArray(entity);    
  94.                     System.out.println(id + " - " + bytes.length + " bytes read");    
  95.                 }    
  96.     
  97.             } catch (Exception e) {    
  98.                 httpget.abort();    
  99.                 System.out.println(id + " - error: " + e);    
  100.             }    
  101.         }    
  102.     
  103.     }    
  104.     
  105. }   
原文地址:https://www.cnblogs.com/timssd/p/4864839.html