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
28 package org.apache.hc.client5.http.examples;
29
30 import java.io.IOException;
31 import java.util.concurrent.Future;
32
33 import org.apache.hc.client5.http.async.AsyncExecCallback;
34 import org.apache.hc.client5.http.async.AsyncExecChain;
35 import org.apache.hc.client5.http.async.AsyncExecChainHandler;
36 import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
37 import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
38 import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
39 import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
40 import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
41 import org.apache.hc.client5.http.impl.ChainElement;
42 import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
43 import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
44 import org.apache.hc.core5.concurrent.FutureCallback;
45 import org.apache.hc.core5.http.ContentType;
46 import org.apache.hc.core5.http.HttpException;
47 import org.apache.hc.core5.http.HttpRequest;
48 import org.apache.hc.core5.http.message.StatusLine;
49 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
50 import org.apache.hc.core5.http.nio.entity.DigestingEntityProducer;
51 import org.apache.hc.core5.io.CloseMode;
52 import org.apache.hc.core5.reactor.IOReactorConfig;
53 import org.apache.hc.core5.util.Timeout;
54
55
56
57
58
59 public class AsyncClientMessageTrailers {
60
61 public final static void main(final String[] args) throws Exception {
62
63 final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
64 .setSoTimeout(Timeout.ofSeconds(5))
65 .build();
66
67 final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
68 .setIOReactorConfig(ioReactorConfig)
69 .addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new AsyncExecChainHandler() {
70
71 @Override
72 public void execute(
73 final HttpRequest request,
74 final AsyncEntityProducer entityProducer,
75 final AsyncExecChain.Scope scope,
76 final AsyncExecChain chain,
77 final AsyncExecCallback asyncExecCallback) throws HttpException, IOException {
78
79 chain.proceed(
80 request,
81 entityProducer != null ? new DigestingEntityProducer("MD5", entityProducer) : null,
82 scope,
83 asyncExecCallback);
84 }
85
86 })
87 .build();
88
89 client.start();
90
91 final SimpleHttpRequest request = SimpleRequestBuilder.post("http://httpbin.org/post")
92 .setBody("some stuff", ContentType.TEXT_PLAIN)
93 .build();
94
95 System.out.println("Executing request " + request);
96 final Future<SimpleHttpResponse> future = client.execute(
97 SimpleRequestProducer.create(request),
98 SimpleResponseConsumer.create(),
99 new FutureCallback<SimpleHttpResponse>() {
100
101 @Override
102 public void completed(final SimpleHttpResponse response) {
103 System.out.println(request + "->" + new StatusLine(response));
104 System.out.println(response.getBody());
105 }
106
107 @Override
108 public void failed(final Exception ex) {
109 System.out.println(request + "->" + ex);
110 }
111
112 @Override
113 public void cancelled() {
114 System.out.println(request + " cancelled");
115 }
116
117 });
118 future.get();
119
120 System.out.println("Shutting down");
121 client.close(CloseMode.GRACEFUL);
122 }
123
124 }
125