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.config.Http1Config;
43 import org.apache.hc.core5.http.message.BasicHttpRequest;
44 import org.apache.hc.core5.http.message.StatusLine;
45 import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
46 import org.apache.hc.core5.http.nio.CapacityChannel;
47 import org.apache.hc.core5.http.nio.DataStreamChannel;
48 import org.apache.hc.core5.http.nio.RequestChannel;
49 import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
50 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
51 import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
52 import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
53 import org.apache.hc.core5.http.protocol.HttpContext;
54 import org.apache.hc.core5.http.support.BasicRequestBuilder;
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
59
60
61
62 public class AsyncClientFullDuplexExchange {
63
64 public static void main(final String[] args) throws Exception {
65
66 final MinimalHttpAsyncClient client = HttpAsyncClients.createMinimal(
67 H2Config.DEFAULT,
68 Http1Config.DEFAULT,
69 IOReactorConfig.DEFAULT);
70
71 client.start();
72
73 final BasicHttpRequest request = BasicRequestBuilder.post("http://httpbin.org/post").build();
74 final BasicRequestProducer requestProducer = new BasicRequestProducer(request,
75 new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
76 final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(
77 new StringAsyncEntityConsumer());
78
79 System.out.println("Executing request " + request);
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(request + " cancelled");
93 }
94
95 @Override
96 public void failed(final Exception cause) {
97 System.out.println(request + "->" + 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) {
119 System.out.println(request + "->" + new StatusLine(response));
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(request + "->" + new StatusLine(response));
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 }