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  
28  package org.apache.hc.client5.http.examples;
29  
30  
31  import java.io.IOException;
32  import java.nio.CharBuffer;
33  import java.util.concurrent.CountDownLatch;
34  import java.util.concurrent.Future;
35  
36  import org.apache.hc.client5.http.async.methods.AbstractCharResponseConsumer;
37  import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
38  import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
39  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
40  import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
41  import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
42  import org.apache.hc.core5.concurrent.FutureCallback;
43  import org.apache.hc.core5.http.ContentType;
44  import org.apache.hc.core5.http.HttpException;
45  import org.apache.hc.core5.http.HttpResponse;
46  import org.apache.hc.core5.http.nio.AsyncRequestProducer;
47  import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
48  
49  public class AsyncQuickStart {
50  
51      public static void main (final String[] args) throws Exception {
52          try (final CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault()) {
53              // Start the client
54              httpclient.start();
55  
56              // Execute request
57              final SimpleHttpRequest request1 = SimpleHttpRequests.get("http://httpbin.org/get");
58              final Future<SimpleHttpResponse> future = httpclient.execute(request1, null);
59              // and wait until response is received
60              final SimpleHttpResponse response1 = future.get();
61              System.out.println(request1.getRequestUri() + "->" + response1.getCode());
62  
63              // One most likely would want to use a callback for operation result
64              final CountDownLatch latch1 = new CountDownLatch(1);
65              final SimpleHttpRequest request2 = SimpleHttpRequests.get("http://httpbin.org/get");
66              httpclient.execute(request2, new FutureCallback<SimpleHttpResponse>() {
67  
68                  @Override
69                  public void completed(final SimpleHttpResponse response2) {
70                      latch1.countDown();
71                      System.out.println(request2.getRequestUri() + "->" + response2.getCode());
72                  }
73  
74                  @Override
75                  public void failed(final Exception ex) {
76                      latch1.countDown();
77                      System.out.println(request2.getRequestUri() + "->" + ex);
78                  }
79  
80                  @Override
81                  public void cancelled() {
82                      latch1.countDown();
83                      System.out.println(request2.getRequestUri() + " cancelled");
84                  }
85  
86              });
87              latch1.await();
88  
89              // In real world one most likely would want also want to stream
90              // request and response body content
91              final CountDownLatch latch2 = new CountDownLatch(1);
92              final AsyncRequestProducer producer3 = AsyncRequestBuilder.get("http://httpbin.org/get").build();
93              final AbstractCharResponseConsumer<HttpResponse> consumer3 = new AbstractCharResponseConsumer<HttpResponse>() {
94  
95                  HttpResponse response;
96  
97                  @Override
98                  protected void start(final HttpResponse response, final ContentType contentType) throws HttpException, IOException {
99                      this.response = response;
100                 }
101 
102                 @Override
103                 protected int capacityIncrement() {
104                     return Integer.MAX_VALUE;
105                 }
106 
107                 @Override
108                 protected void data(final CharBuffer data, final boolean endOfStream) throws IOException {
109                     // Do something useful
110                 }
111 
112                 @Override
113                 protected HttpResponse buildResult() throws IOException {
114                     return response;
115                 }
116 
117                 @Override
118                 public void releaseResources() {
119                 }
120 
121             };
122             httpclient.execute(producer3, consumer3, new FutureCallback<HttpResponse>() {
123 
124                 @Override
125                 public void completed(final HttpResponse response3) {
126                     latch2.countDown();
127                     System.out.println(request2.getRequestUri() + "->" + response3.getCode());
128                 }
129 
130                 @Override
131                 public void failed(final Exception ex) {
132                     latch2.countDown();
133                     System.out.println(request2.getRequestUri() + "->" + ex);
134                 }
135 
136                 @Override
137                 public void cancelled() {
138                     latch2.countDown();
139                     System.out.println(request2.getRequestUri() + " cancelled");
140                 }
141 
142             });
143             latch2.await();
144 
145         }
146     }
147 
148 }