HTTP connection pooling using HttpClient

后端 未结 7 984
南笙
南笙 2020-12-02 18:44
  • How can I create a pool of connections using HttpClient?
  • I have to make frequent connections to the same server. Is it worth creating such a pool?
相关标签:
7条回答
  • 2020-12-02 19:34

    [assuming Java, and Apache's HttpClient]

    Use a ThreadSafeClientConnManager. Pass a single global instance to the constructor of every HttpClient instance. I don't think there's any point in pooling the HttpClients themselves.

    0 讨论(0)
  • 2020-12-02 19:36

    HttpClient has already have a connection pool.So you do not need to create it. Just use it.

    0 讨论(0)
  • 2020-12-02 19:40

    PoolingClientConnectionManager is Deprecated now . from (4.3 version) use PoolingHttpClientConnectionManager.

    0 讨论(0)
  • 2020-12-02 19:40

    This is an example of an Apache HttpClient 4.3 pool of connections which do not require authentication:

    public class PoolOfHttpConnections{
       static String[] urisToGet = {"http://www.site1.com", "http://www.site2.com"};
    
        public static void main(String[] args) throws Exception {
               CloseableHttpClient httpclient = HttpClients.createDefault();
               // create a thread for each link
               GetThread[] threads = new GetThread[urisToGet.length];
               for (int i = 0; i < threads.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();
               }
        } //end main
    
        private 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;
                }
    
                @Override
                public void run() {
                       try {
                           CloseableHttpResponse response = httpClient.execute(httpget, context);
                           try {
                               HttpEntity entity = response.getEntity();
                               System.out.println("----------------------------------------");
                               Date date = new Date();
                               System.out.println("Beginning*******************");
                               System.out.println(date.toString());
                               System.out.println("There are "+urisToGet.length+" threads running in parallel!");
                               System.out.println(response.getStatusLine());
                               if (entity != null) {
                                  System.out.println("Response content length: " + entity.getContentLength());
                               }
                               System.out.println(EntityUtils.toString(entity));
                               EntityUtils.consume(entity);
                           } finally {
                             response.close();
                             System.out.println("End*******************");
                           }
                       } catch (ClientProtocolException ex) {
                              // Handle protocol errors
                       } catch (IOException ex) {
                              // Handle I/O errors
                       }
                }
        } /*end private class*/  }//end public class PoolOfHttpConnections
    
    0 讨论(0)
  • 2020-12-02 19:42

    ThreadSafeClientConnManager is deprecated now, use PoolingClientConnectionManager instead.

    0 讨论(0)
  • 2020-12-02 19:44

    For HttpClient 4x:

    ThreadSafeClientConnManager ... manages a pool of client connections and is able to service connection requests from multiple execution threads.

    Connections are pooled on a per route basis. A request for a route for which the manager already has a persistent connection available in the pool will be serviced by leasing a connection from the pool rather than creating a brand new connection.

    http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

    0 讨论(0)
提交回复
热议问题