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.CloseableHttpResponse;
32  import org.apache.hc.client5.http.impl.classic.HttpClients;
33  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
34  import org.apache.hc.core5.http.HttpEntity;
35  import org.apache.hc.core5.http.io.entity.EntityUtils;
36  import org.apache.hc.core5.http.protocol.BasicHttpContext;
37  import org.apache.hc.core5.http.protocol.HttpContext;
38  
39  /**
40   * An example that performs GETs from multiple threads.
41   *
42   */
43  public class ClientMultiThreadedExecution {
44  
45      public static void main(final String[] args) throws Exception {
46          // Create an HttpClient with the PoolingHttpClientConnectionManager.
47          // This connection manager must be used if more than one thread will
48          // be using the HttpClient.
49          final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
50          cm.setMaxTotal(100);
51  
52          try (final CloseableHttpClient httpclient = HttpClients.custom()
53                  .setConnectionManager(cm)
54                  .build()) {
55              // create an array of URIs to perform GETs on
56              final String[] urisToGet = {
57                      "http://hc.apache.org/",
58                      "http://hc.apache.org/httpcomponents-core-ga/",
59                      "http://hc.apache.org/httpcomponents-client-ga/",
60              };
61  
62              // create a thread for each URI
63              final GetThread[] threads = new GetThread[urisToGet.length];
64              for (int i = 0; i < threads.length; i++) {
65                  final HttpGet httpget = new HttpGet(urisToGet[i]);
66                  threads[i] = new GetThread(httpclient, httpget, i + 1);
67              }
68  
69              // start the threads
70              for (final GetThread thread : threads) {
71                  thread.start();
72              }
73  
74              // join the threads
75              for (final GetThread thread : threads) {
76                  thread.join();
77              }
78  
79          }
80      }
81  
82      /**
83       * A thread that performs a GET.
84       */
85      static class GetThread extends Thread {
86  
87          private final CloseableHttpClient httpClient;
88          private final HttpContext context;
89          private final HttpGet httpget;
90          private final int id;
91  
92          public GetThread(final CloseableHttpClient httpClient, final HttpGet httpget, final int id) {
93              this.httpClient = httpClient;
94              this.context = new BasicHttpContext();
95              this.httpget = httpget;
96              this.id = id;
97          }
98  
99          /**
100          * Executes the GetMethod and prints some status information.
101          */
102         @Override
103         public void run() {
104             try {
105                 System.out.println(id + " - about to get something from " + httpget.getUri());
106                 try (CloseableHttpResponse response = httpClient.execute(httpget, context)) {
107                     System.out.println(id + " - get executed");
108                     // get the response body as an array of bytes
109                     final HttpEntity entity = response.getEntity();
110                     if (entity != null) {
111                         final byte[] bytes = EntityUtils.toByteArray(entity);
112                         System.out.println(id + " - " + bytes.length + " bytes read");
113                     }
114                 }
115             } catch (final Exception e) {
116                 System.out.println(id + " - error: " + e);
117             }
118         }
119 
120     }
121 
122 }