File tree 4 files changed +80
-0
lines changed
src/main/java/org/asynchttpclient/example/completable 4 files changed +80
-0
lines changed Original file line number Diff line number Diff line change @@ -67,6 +67,26 @@ asyncHttpClient.prepareGet("http://www.example.com/").execute(new AsyncCompletio
67
67
68
68
(this will also fully read ` Response ` in memory before calling ` onCompleted ` )
69
69
70
+ Alternatively you may use continuations (through Java 8 class ` CompletableFuture<T> ` ) to accomplish asynchronous (non-blocking) solution. The equivalent continuation approach to the previous example is:
71
+
72
+ ``` java
73
+ import org.asynchttpclient.* ;
74
+ import java.util.concurrent.CompletableFuture ;
75
+
76
+ import static org.asynchttpclient.Dsl.asyncHttpClient ;
77
+
78
+ AsyncHttpClient asyncHttpClient = asyncHttpClient();
79
+ CompletableFuture<Response > promise = asyncHttpClient
80
+ .prepareGet(" http://www.example.com/" )
81
+ .execute()
82
+ .toCompletableFuture()
83
+ .exceptionally(t - > { /* Something wrong happened... */ } )
84
+ .thenApply(resp - > { /* Do something with the Response */ return resp; });
85
+ promise. join(); // wait for completion
86
+ ```
87
+
88
+ You may get the complete maven project for this simple demo from [ org.asynchttpclient.example] ( https://github.com/AsyncHttpClient/async-http-client/tree/master/example/src/main/java/org/asynchttpclient/example )
89
+
70
90
You can also mix Future with AsyncHandler to only retrieve part of the asynchronous response
71
91
72
92
``` java
Original file line number Diff line number Diff line change
1
+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
2
+ <parent >
3
+ <groupId >org.asynchttpclient</groupId >
4
+ <artifactId >async-http-client-project</artifactId >
5
+ <version >2.0.16-SNAPSHOT</version >
6
+ </parent >
7
+ <modelVersion >4.0.0</modelVersion >
8
+ <artifactId >async-http-client-example</artifactId >
9
+ <name >Asynchronous Http Client Example</name >
10
+ <packaging >jar</packaging >
11
+ <description >
12
+ The Async Http Client example.
13
+ </description >
14
+ <dependencies >
15
+ <dependency >
16
+ <groupId >org.asynchttpclient</groupId >
17
+ <artifactId >async-http-client</artifactId >
18
+ <version >${project.version} </version >
19
+ </dependency >
20
+ </dependencies >
21
+ </project >
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright (c) 2016 AsyncHttpClient Project. All rights reserved.
3
+ *
4
+ * Ning licenses this file to you under the Apache License, version 2.0
5
+ * (the "License"); you may not use this file except in compliance with the
6
+ * License. You may obtain a copy of the License at:
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ * License for the specific language governing permissions and limitations
14
+ * under the License.
15
+ *
16
+ */
17
+ package org .asynchttpclient .example .completable ;
18
+
19
+ import org .asynchttpclient .AsyncHttpClient ;
20
+ import org .asynchttpclient .Response ;
21
+
22
+ import java .io .IOException ;
23
+
24
+ import static org .asynchttpclient .Dsl .asyncHttpClient ;
25
+
26
+ public class CompletableFutures {
27
+ public static void main (String [] args ) throws IOException {
28
+ try (AsyncHttpClient asyncHttpClient = asyncHttpClient ()) {
29
+ asyncHttpClient
30
+ .prepareGet ("http://www.example.com/" )
31
+ .execute ()
32
+ .toCompletableFuture ()
33
+ .thenApply (Response ::getResponseBody )
34
+ .thenAccept (System .out ::println )
35
+ .join ();
36
+ }
37
+ }
38
+ }
Original file line number Diff line number Diff line change 210
210
<module >netty-utils</module >
211
211
<module >client</module >
212
212
<module >extras</module >
213
+ <module >example</module >
213
214
</modules >
214
215
<dependencyManagement >
215
216
<dependencies >
You can’t perform that action at this time.
0 commit comments