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