HTTP connection pooling using HttpClient

2019-01-11 01:10发布

  • 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?
  • Is it possible to keep live connections and use it for various requests, and if yes how can I do so?

I am developing in Java, using Apache HTTP Client.

7条回答
做自己的国王
2楼-- · 2019-01-11 02:08

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
查看更多
登录 后发表回答