View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.examples;
28  
29  import org.apache.hc.client5.http.classic.methods.HttpGet;
30  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
31  import org.apache.hc.client5.http.impl.classic.HttpClients;
32  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
33  import org.apache.hc.core5.http.HttpEntity;
34  import org.apache.hc.core5.http.io.entity.EntityUtils;
35  import org.apache.hc.core5.http.protocol.BasicHttpContext;
36  import org.apache.hc.core5.http.protocol.HttpContext;
37  
38  /**
39   * An example that performs GETs from multiple threads.
40   *
41   */
42  public class ClientMultiThreadedExecution {
43  
44      public static void main(final String[] args) throws Exception {
45          // Create an HttpClient with the PoolingHttpClientConnectionManager.
46          // This connection manager must be used if more than one thread will
47          // be using the HttpClient.
48          final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
49          cm.setMaxTotal(100);
50  
51          try (final CloseableHttpClient httpclient = HttpClients.custom()
52                  .setConnectionManager(cm)
53                  .build()) {
54              // create an array of URIs to perform GETs on
55              final String[] urisToGet = {
56                      "http://hc.apache.org/",
57                      "http://hc.apache.org/httpcomponents-core-ga/",
58                      "http://hc.apache.org/httpcomponents-client-ga/",
59              };
60  
61              // create a thread for each URI
62              final GetThread[] threads = new GetThread[urisToGet.length];
63              for (int i = 0; i < threads.length; i++) {
64                  final HttpGet httpget = new HttpGet(urisToGet[i]);
65                  threads[i] = new GetThread(httpclient, httpget, i + 1);
66              }
67  
68              // start the threads
69              for (final GetThread thread : threads) {
70                  thread.start();
71              }
72  
73              // join the threads
74              for (final GetThread thread : threads) {
75                  thread.join();
76              }
77  
78          }
79      }
80  
81      /**
82       * A thread that performs a GET.
83       */
84      static class GetThread extends Thread {
85  
86          private final CloseableHttpClient httpClient;
87          private final HttpContext context;
88          private final HttpGet httpget;
89          private final int id;
90  
91          public GetThread(final CloseableHttpClient httpClient, final HttpGet httpget, final int id) {
92              this.httpClient = httpClient;
93              this.context = new BasicHttpContext();
94              this.httpget = httpget;
95              this.id = id;
96          }
97  
98          /**
99           * Executes the GetMethod and prints some status information.
100          */
101         @Override
102         public void run() {
103             try {
104                 System.out.println(id + " - about to get something from " + httpget.getUri());
105                 this.httpClient.execute(httpget, response -> {
106                     System.out.println(id + " - get executed");
107                     // get the response body as an array of bytes
108                     final HttpEntity entity = response.getEntity();
109                     if (entity != null) {
110                         final byte[] bytes = EntityUtils.toByteArray(entity);
111                         System.out.println(id + " - " + bytes.length + " bytes read");
112                     }
113                     return null;
114                 });
115             } catch (final Exception e) {
116                 System.out.println(id + " - error: " + e);
117             }
118         }
119 
120     }
121 
122 }