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