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.client5.http.examples;
28
29 import java.io.IOException;
30 import java.nio.ByteBuffer;
31 import java.util.List;
32 import java.util.concurrent.CountDownLatch;
33 import java.util.concurrent.TimeUnit;
34
35 import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
36 import org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient;
37 import org.apache.hc.core5.http.ContentType;
38 import org.apache.hc.core5.http.EntityDetails;
39 import org.apache.hc.core5.http.Header;
40 import org.apache.hc.core5.http.HttpException;
41 import org.apache.hc.core5.http.HttpResponse;
42 import org.apache.hc.core5.http.message.BasicHttpRequest;
43 import org.apache.hc.core5.http.message.StatusLine;
44 import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
45 import org.apache.hc.core5.http.nio.CapacityChannel;
46 import org.apache.hc.core5.http.nio.DataStreamChannel;
47 import org.apache.hc.core5.http.nio.RequestChannel;
48 import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
49 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
50 import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
51 import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
52 import org.apache.hc.core5.http.protocol.HttpContext;
53 import org.apache.hc.core5.http.support.BasicRequestBuilder;
54 import org.apache.hc.core5.http2.HttpVersionPolicy;
55 import org.apache.hc.core5.http2.config.H2Config;
56 import org.apache.hc.core5.io.CloseMode;
57 import org.apache.hc.core5.reactor.IOReactorConfig;
58 import org.apache.hc.core5.util.Timeout;
59
60
61
62
63 public class AsyncClientH2FullDuplexExchange {
64
65 public static void main(final String[] args) throws Exception {
66
67 final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
68 .setSoTimeout(Timeout.ofSeconds(5))
69 .build();
70
71 final MinimalHttpAsyncClient client = HttpAsyncClients.createMinimal(
72 HttpVersionPolicy.FORCE_HTTP_2, H2Config.DEFAULT, null, ioReactorConfig);
73
74 client.start();
75
76 final BasicHttpRequest request = BasicRequestBuilder.post("https://nghttp2.org/httpbin/post").build();
77 final BasicRequestProducer requestProducer = new BasicRequestProducer(request,
78 new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
79 final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(
80 new StringAsyncEntityConsumer());
81
82 System.out.println("Executing request " + request);
83 final CountDownLatch latch = new CountDownLatch(1);
84 client.execute(new AsyncClientExchangeHandler() {
85
86 @Override
87 public void releaseResources() {
88 requestProducer.releaseResources();
89 responseConsumer.releaseResources();
90 latch.countDown();
91 }
92
93 @Override
94 public void cancel() {
95 System.out.println(request + " cancelled");
96 }
97
98 @Override
99 public void failed(final Exception cause) {
100 System.out.println(request + "->" + cause);
101 }
102
103 @Override
104 public void produceRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
105 requestProducer.sendRequest(channel, context);
106 }
107
108 @Override
109 public int available() {
110 return requestProducer.available();
111 }
112
113 @Override
114 public void produce(final DataStreamChannel channel) throws IOException {
115 requestProducer.produce(channel);
116 }
117
118 @Override
119 public void consumeInformation(
120 final HttpResponse response,
121 final HttpContext context) throws HttpException, IOException {
122 System.out.println(request + "->" + new StatusLine(response));
123 }
124
125 @Override
126 public void consumeResponse(
127 final HttpResponse response,
128 final EntityDetails entityDetails,
129 final HttpContext context) throws HttpException, IOException {
130 System.out.println(request + "->" + new StatusLine(response));
131 responseConsumer.consumeResponse(response, entityDetails, context, null);
132 }
133
134 @Override
135 public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
136 responseConsumer.updateCapacity(capacityChannel);
137 }
138
139 @Override
140 public void consume(final ByteBuffer src) throws IOException {
141 responseConsumer.consume(src);
142 }
143
144 @Override
145 public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
146 responseConsumer.streamEnd(trailers);
147 }
148
149 });
150 latch.await(1, TimeUnit.MINUTES);
151
152 System.out.println("Shutting down");
153 client.close(CloseMode.GRACEFUL);
154 }
155
156 }