From 5bc840ef262ce049f0ea761dbd7c5e0e586daea3 Mon Sep 17 00:00:00 2001 From: fmcarvalho Date: Fri, 8 Jul 2016 17:44:18 +0100 Subject: [PATCH] Add example module and CompletableFuture usage demo to Readme.md --- README.md | 20 ++++++++++ example/pom.xml | 21 ++++++++++ .../completable/CompletableFutures.java | 38 +++++++++++++++++++ pom.xml | 1 + 4 files changed, 80 insertions(+) create mode 100644 example/pom.xml create mode 100644 example/src/main/java/org/asynchttpclient/example/completable/CompletableFutures.java diff --git a/README.md b/README.md index cbd817f135..8931b8eda3 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,26 @@ asyncHttpClient.prepareGet("http://www.example.com/").execute(new AsyncCompletio (this will also fully read `Response` in memory before calling `onCompleted`) +Alternatively you may use continuations (through Java 8 class `CompletableFuture`) to accomplish asynchronous (non-blocking) solution. The equivalent continuation approach to the previous example is: + +```java +import org.asynchttpclient.*; +import java.util.concurrent.CompletableFuture; + +import static org.asynchttpclient.Dsl.asyncHttpClient; + +AsyncHttpClient asyncHttpClient = asyncHttpClient(); +CompletableFuture promise = asyncHttpClient + .prepareGet("http://www.example.com/") + .execute() + .toCompletableFuture() + .exceptionally(t -> { /* Something wrong happened... */ } ) + .thenApply(resp -> { /* Do something with the Response */ return resp; }); +promise.join(); // wait for completion +``` + +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) + You can also mix Future with AsyncHandler to only retrieve part of the asynchronous response ```java diff --git a/example/pom.xml b/example/pom.xml new file mode 100644 index 0000000000..a7e57a5418 --- /dev/null +++ b/example/pom.xml @@ -0,0 +1,21 @@ + + + org.asynchttpclient + async-http-client-project + 2.0.11-SNAPSHOT + + 4.0.0 + async-http-client-example + Asynchronous Http Client Example + jar + + The Async Http Client example. + + + + org.asynchttpclient + async-http-client + ${project.version} + + + diff --git a/example/src/main/java/org/asynchttpclient/example/completable/CompletableFutures.java b/example/src/main/java/org/asynchttpclient/example/completable/CompletableFutures.java new file mode 100644 index 0000000000..172876113c --- /dev/null +++ b/example/src/main/java/org/asynchttpclient/example/completable/CompletableFutures.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2016 AsyncHttpClient Project. All rights reserved. + * + * Ning licenses this file to you under the Apache License, version 2.0 + * (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + */ +package org.asynchttpclient.example.completable; + +import org.asynchttpclient.AsyncHttpClient; +import org.asynchttpclient.Response; + +import java.io.IOException; + +import static org.asynchttpclient.Dsl.asyncHttpClient; + +public class CompletableFutures { + public static void main(String[] args) throws IOException { + try(AsyncHttpClient asyncHttpClient = asyncHttpClient()) { + asyncHttpClient + .prepareGet("http://www.example.com/") + .execute() + .toCompletableFuture() + .thenApply(Response::getResponseBody) + .thenAccept(System.out::println) + .join(); + } + } +} diff --git a/pom.xml b/pom.xml index f015764f24..3e7a277627 100644 --- a/pom.xml +++ b/pom.xml @@ -215,6 +215,7 @@ netty-bp client extras + example