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.nio.CharBuffer;
32 import java.util.concurrent.Future;
33
34 import org.apache.hc.client5.http.async.methods.AbstractBinPushConsumer;
35 import org.apache.hc.client5.http.async.methods.AbstractCharResponseConsumer;
36 import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
37 import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
38 import org.apache.hc.core5.function.Supplier;
39 import org.apache.hc.core5.http.ContentType;
40 import org.apache.hc.core5.http.HttpException;
41 import org.apache.hc.core5.http.HttpHost;
42 import org.apache.hc.core5.http.HttpRequest;
43 import org.apache.hc.core5.http.HttpResponse;
44 import org.apache.hc.core5.http.Method;
45 import org.apache.hc.core5.http.message.StatusLine;
46 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
47 import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
48 import org.apache.hc.core5.http2.HttpVersionPolicy;
49 import org.apache.hc.core5.http2.config.H2Config;
50 import org.apache.hc.core5.io.CloseMode;
51 import org.apache.hc.core5.reactor.IOReactorConfig;
52 import org.apache.hc.core5.util.Timeout;
53
54
55
56
57 public class AsyncClientH2ServerPush {
58
59 public static void main(final String[] args) throws Exception {
60
61 final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
62 .setSoTimeout(Timeout.ofSeconds(5))
63 .build();
64
65 final H2Config h2Config = H2Config.custom()
66 .setPushEnabled(true)
67 .build();
68
69 final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
70 .setIOReactorConfig(ioReactorConfig)
71 .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2)
72 .setH2Config(h2Config)
73 .build();
74
75 client.start();
76
77 client.register("*", new Supplier<AsyncPushConsumer>() {
78
79 @Override
80 public AsyncPushConsumer get() {
81 return new AbstractBinPushConsumer() {
82
83 @Override
84 protected void start(
85 final HttpRequest promise,
86 final HttpResponse response,
87 final ContentType contentType) throws HttpException, IOException {
88 System.out.println(promise.getPath() + " (push)->" + new StatusLine(response));
89 }
90
91 @Override
92 protected int capacityIncrement() {
93 return Integer.MAX_VALUE;
94 }
95
96 @Override
97 protected void data(final ByteBuffer data, final boolean endOfStream) throws IOException {
98 }
99
100 @Override
101 protected void completed() {
102 }
103
104 @Override
105 public void failed(final Exception cause) {
106 System.out.println("(push)->" + cause);
107 }
108
109 @Override
110 public void releaseResources() {
111 }
112
113 };
114 }
115
116 });
117
118 final HttpHost target = new HttpHost("nghttp2.org");
119 final String requestURI = "/httpbin/";
120 final Future<Void> future = client.execute(
121 new BasicRequestProducer(Method.GET, target, requestURI),
122 new AbstractCharResponseConsumer<Void>() {
123
124 @Override
125 protected void start(
126 final HttpResponse response,
127 final ContentType contentType) throws HttpException, IOException {
128 System.out.println(requestURI + "->" + new StatusLine(response));
129 }
130
131 @Override
132 protected int capacityIncrement() {
133 return Integer.MAX_VALUE;
134 }
135
136 @Override
137 protected void data(final CharBuffer data, final boolean endOfStream) throws IOException {
138 }
139
140 @Override
141 protected Void buildResult() throws IOException {
142 return null;
143 }
144
145 @Override
146 public void failed(final Exception cause) {
147 System.out.println(requestURI + "->" + cause);
148 }
149
150 @Override
151 public void releaseResources() {
152 }
153
154 }, null);
155 future.get();
156
157 System.out.println("Shutting down");
158 client.close(CloseMode.GRACEFUL);
159 }
160
161 }