1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
54 httpclient.start();
55
56
57 final SimpleHttpRequest request1 = SimpleHttpRequests.get("http://httpbin.org/get");
58 final Future<SimpleHttpResponse> future = httpclient.execute(request1, null);
59
60 final SimpleHttpResponse response1 = future.get();
61 System.out.println(request1.getRequestUri() + "->" + response1.getCode());
62
63
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
90
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
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 }