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 java.util.concurrent.Future;
30  import java.util.concurrent.TimeUnit;
31  
32  import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
33  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
34  import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
35  import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
36  import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
37  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
38  import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
39  import org.apache.hc.core5.concurrent.FutureCallback;
40  import org.apache.hc.core5.http.HttpHost;
41  import org.apache.hc.core5.http.message.StatusLine;
42  import org.apache.hc.core5.io.CloseMode;
43  import org.apache.hc.core5.reactor.IOReactorConfig;
44  import org.apache.hc.core5.util.TimeValue;
45  import org.apache.hc.core5.util.Timeout;
46  
47  /**
48   * Example demonstrating how to evict expired and idle connections
49   * from the connection pool.
50   */
51  public class AsyncClientConnectionEviction {
52  
53      public static void main(final String[] args) throws Exception {
54  
55          final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
56                  .setSoTimeout(Timeout.ofSeconds(5))
57                  .build();
58  
59          final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
60                  .setIOReactorConfig(ioReactorConfig)
61                  .evictExpiredConnections()
62                  .evictIdleConnections(TimeValue.ofSeconds(10))
63                  .build();
64  
65          client.start();
66  
67          final SimpleHttpRequest request = SimpleRequestBuilder.get()
68                  .setHttpHost(new HttpHost("httpbin.org"))
69                  .setPath("/")
70                  .build();
71  
72          System.out.println("Executing request " + request);
73          final Future<SimpleHttpResponse> future1 = client.execute(
74                  SimpleRequestProducer.create(request),
75                  SimpleResponseConsumer.create(),
76                  new FutureCallback<SimpleHttpResponse>() {
77  
78                      @Override
79                      public void completed(final SimpleHttpResponse response) {
80                          System.out.println(request + "->" + new StatusLine(response));
81                          System.out.println(response.getBody());
82                      }
83  
84                      @Override
85                      public void failed(final Exception ex) {
86                          System.out.println(request + "->" + ex);
87                      }
88  
89                      @Override
90                      public void cancelled() {
91                          System.out.println(request + " cancelled");
92                      }
93  
94                  });
95  
96          future1.get();
97  
98          Thread.sleep(TimeUnit.SECONDS.toMillis(30));
99  
100         // Previous connection should get evicted from the pool by now
101 
102         final Future<SimpleHttpResponse> future2 = client.execute(
103                 request,
104                 new FutureCallback<SimpleHttpResponse>() {
105 
106                     @Override
107                     public void completed(final SimpleHttpResponse response) {
108                         System.out.println(request + "->" + new StatusLine(response));
109                         System.out.println(response.getBody());
110                     }
111 
112                     @Override
113                     public void failed(final Exception ex) {
114                         System.out.println(request + "->" + ex);
115                     }
116 
117                     @Override
118                     public void cancelled() {
119                         System.out.println(request + " cancelled");
120                     }
121 
122                 });
123 
124         future2.get();
125 
126         System.out.println("Shutting down");
127         client.close(CloseMode.GRACEFUL);
128     }
129 
130 }