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.io.entity.EntityUtils;
35  import org.apache.hc.core5.pool.PoolStats;
36  import org.apache.hc.core5.util.TimeValue;
37  
38  /**
39   * Example demonstrating how to evict expired and idle connections
40   * from the connection pool.
41   */
42  public class ClientEvictExpiredConnections {
43  
44      public static void main(final String[] args) throws Exception {
45          final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
46          cm.setMaxTotal(100);
47          try (final CloseableHttpClient httpclient = HttpClients.custom()
48                  .setConnectionManager(cm)
49                  .evictExpiredConnections()
50                  .evictIdleConnections(TimeValue.ofSeconds(5))
51                  .build()) {
52              // create an array of URIs to perform GETs on
53              final String[] urisToGet = {
54                      "http://hc.apache.org/",
55                      "http://hc.apache.org/httpcomponents-core-ga/",
56                      "http://hc.apache.org/httpcomponents-client-ga/",
57              };
58  
59              for (final String requestURI : urisToGet) {
60                  final HttpGet request = new HttpGet(requestURI);
61  
62                  System.out.println("Executing request " + request.getMethod() + " " + request.getRequestUri());
63  
64                  try (final CloseableHttpResponse response = httpclient.execute(request)) {
65                      System.out.println("----------------------------------------");
66                      System.out.println(response.getCode() + " " + response.getReasonPhrase());
67                      EntityUtils.consume(response.getEntity());
68                  }
69              }
70  
71              final PoolStats stats1 = cm.getTotalStats();
72              System.out.println("Connections kept alive: " + stats1.getAvailable());
73  
74              // Sleep 10 sec and let the connection evictor do its job
75              Thread.sleep(10000);
76  
77              final PoolStats stats2 = cm.getTotalStats();
78              System.out.println("Connections kept alive: " + stats2.getAvailable());
79  
80          }
81      }
82  
83  }