zoukankan      html  css  js  c++  java
  • httpclient 多线程请求

    线程请求执行

    当配备一个线程池管理器后,如PollingClientConnectionManager,HttpClient就能使用执行着的多线程去执行并行的多请求。

    PollingClientConnectionManager会基于它的配置去分配连接。如果一个指定的路由连接已经被租用了,连接请求会被阻塞直到有一个连接被释回到池里。
    可以给http.connmanager.timeout设定一个正值以确保连接管理器在连接请求操作里不会无限的阻塞下去。如果连接请求不能在指定的时间获取服务就抛出
    ConnectionPoolTimeoutException异常。
    PollingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    CloseableHttpClient httpClient = HttpClients
                                     .custom()
                                     .setConnectionManager(cm)
                                     .build();
    //URIs to perform GETs on
    String[] urisToGet={
     "http://www.domain1.com/",
     "http://www.domain2.com/",
     "http://www.domain3.com/",
     "http://www.domain4.com/"
    };
    //create a thread for each URI
    GetThread[] threads = new GetThread[uriToGet.length];
    for(int i=0;i<thread.length;i++){
      HttpGet httpget = new HttpGet(urisToGet[i]);
      threads[i]=new GetThread(httpClient,httpget);
    }
    //start the threads
    for(int j=0;j<threads.length;j++){
      threads[j].start();
    }
    //join the threads
    for(int j=0;j<threads.length;j++){
      threads[j].join();
    }
    

    HttpClient实例是线程安全的并且可以在执行着的多线程之间共享,并强烈推荐每个线程维护自己的HttpContext专用实例。

    Static class GetThread extends Thread{
     private final CloseableHttpClient httpClient;
     private final HttpContext context;
     private final HttpGet httpget;
    ​
     public GetThread(CloseableHttpClient httpClient, HttpGet httpGet){
       this.httpClient = httpClient;
       this.context=HttpClientContext.create();
       this.httpget=httpget;
     }
    ​
     public void run(){
      try{
         CloseableHttpResponse response = httpClient.execute(httpGet,context);
         try{
           HttpEntity entity = response.getEntity();
          }finally{
            response.close();
          }
       }catch(ClientProtocolException ex){
        //handle protocol errors
       }catch(IOException ex){
         //Handle I/O errors
       }
     }
    }
    
  • 相关阅读:
    java之正则表达式
    mysql之自定义函数
    mysql之replace into与 insert into duplicat key for update
    mysql之命令行导入导出
    Echarts修改legend样式
    ubuntu出现 E: Sub-process /usr/bin/dpkg returned an error code
    ubuntu总是提是E: 不能满足依赖关系。不妨试一下 -f 选项
    ubuntu安装和查看已安装软件
    放爬虫nginx
    nginx日志切割
  • 原文地址:https://www.cnblogs.com/ssgao/p/8829320.html
Copyright © 2011-2022 走看看