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 package org.apache.hc.core5.http.examples;
28
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.Future;
31 import java.util.concurrent.TimeUnit;
32
33 import org.apache.hc.core5.concurrent.FutureCallback;
34 import org.apache.hc.core5.http.HttpConnection;
35 import org.apache.hc.core5.http.HttpHost;
36 import org.apache.hc.core5.http.HttpRequest;
37 import org.apache.hc.core5.http.HttpResponse;
38 import org.apache.hc.core5.http.Message;
39 import org.apache.hc.core5.http.impl.Http1StreamListener;
40 import org.apache.hc.core5.http.impl.bootstrap.AsyncRequesterBootstrap;
41 import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester;
42 import org.apache.hc.core5.http.message.RequestLine;
43 import org.apache.hc.core5.http.message.StatusLine;
44 import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
45 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
46 import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
47 import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
48 import org.apache.hc.core5.io.CloseMode;
49 import org.apache.hc.core5.reactor.IOReactorConfig;
50 import org.apache.hc.core5.util.Timeout;
51
52
53
54
55 public class AsyncPipelinedRequestExecutionExample {
56
57 public static void main(final String[] args) throws Exception {
58
59 final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
60 .setSoTimeout(5, TimeUnit.SECONDS)
61 .build();
62
63
64 final HttpAsyncRequester requester = AsyncRequesterBootstrap.bootstrap()
65 .setIOReactorConfig(ioReactorConfig)
66 .setStreamListener(new Http1StreamListener() {
67
68 @Override
69 public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
70 System.out.println(connection.getRemoteAddress() + " " + new RequestLine(request));
71
72 }
73
74 @Override
75 public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
76 System.out.println(connection.getRemoteAddress() + " " + new StatusLine(response));
77 }
78
79 @Override
80 public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
81 if (keepAlive) {
82 System.out.println(connection.getRemoteAddress() + " exchange completed (connection kept alive)");
83 } else {
84 System.out.println(connection.getRemoteAddress() + " exchange completed (connection closed)");
85 }
86 }
87
88 })
89 .create();
90
91 Runtime.getRuntime().addShutdownHook(new Thread() {
92 @Override
93 public void run() {
94 System.out.println("HTTP requester shutting down");
95 requester.close(CloseMode.GRACEFUL);
96 }
97 });
98 requester.start();
99
100 final HttpHost target = new HttpHost("httpbin.org");
101 final String[] requestUris = new String[] {"/", "/ip", "/user-agent", "/headers"};
102
103 final Future<AsyncClientEndpoint> future = requester.connect(target, Timeout.ofSeconds(5));
104 final AsyncClientEndpoint clientEndpoint = future.get();
105
106 final CountDownLatch latch = new CountDownLatch(requestUris.length);
107 for (final String requestUri: requestUris) {
108 clientEndpoint.execute(
109 AsyncRequestBuilder.get()
110 .setHttpHost(target)
111 .setPath(requestUri)
112 .build(),
113 new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
114 new FutureCallback<Message<HttpResponse, String>>() {
115
116 @Override
117 public void completed(final Message<HttpResponse, String> message) {
118 latch.countDown();
119 final HttpResponse response = message.getHead();
120 final String body = message.getBody();
121 System.out.println(requestUri + "->" + response.getCode());
122 System.out.println(body);
123 }
124
125 @Override
126 public void failed(final Exception ex) {
127 latch.countDown();
128 System.out.println(requestUri + "->" + ex);
129 }
130
131 @Override
132 public void cancelled() {
133 latch.countDown();
134 System.out.println(requestUri + " cancelled");
135 }
136
137 });
138 }
139
140 latch.await();
141
142
143 clientEndpoint.releaseAndDiscard();
144
145 System.out.println("Shutting down I/O reactor");
146 requester.initiateShutdown();
147 }
148
149 }