Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 529214e

Browse files
fmcarvalhoslandelle
authored andcommitted
Add example module and CompletableFuture usage demo to Readme.md (#1206)
1 parent 7f6fb74 commit 529214e

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

‎README.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ asyncHttpClient.prepareGet("http://www.example.com/").execute(new AsyncCompletio
6767

6868
(this will also fully read `Response` in memory before calling `onCompleted`)
6969

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+
7090
You can also mix Future with AsyncHandler to only retrieve part of the asynchronous response
7191

7292
```java

‎example/pom.xml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
<module>netty-utils</module>
211211
<module>client</module>
212212
<module>extras</module>
213+
<module>example</module>
213214
</modules>
214215
<dependencyManagement>
215216
<dependencies>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /