Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a CompletableFuture<T> from an Async Http Client request?

On Async Http Client documentation I see how to get a Future<Response> as the result of an asynchronous HTTP Get request simply doing, for example:

AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient();
Future<Response> f = asyncHttpClient
      .prepareGet("http://api.football-data.org/v1/soccerseasons/398")
      .execute();
Response r = f.get();

However, for convenience I would like to get a CompletableFuture<T> instead, for which I could apply a continuation that converts the result in something else, for instance deserializing the response content from Json into a Java object (e.g. SoccerSeason.java). This is what I would like to do:

AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient();
CompletableFuture<Response> f = asyncHttpClient
     .prepareGet("http://api.football-data.org/v1/soccerseasons/398")
     .execute();
f
     .thenApply(r -> gson.fromJson(r.getResponseBody(), SoccerSeason.class))
     .thenAccept(System.out::println);

According to Async Http Client documentation the only way to do this is through an AsyncCompletionHandler<T> object and using a promise. So I built an auxiliary method to that end:

CompletableFuture<Response> getDataAsync(String path){
    CompletableFuture<Response> promise = new CompletableFuture<>();
    asyncHttpClient
            .prepareGet(path)
            .execute(new AsyncCompletionHandler<Response>() {
                @Override
                public Response onCompleted(Response response) throws Exception {
                    promise.complete(response);
                    return response;
                }
                @Override
                public void onThrowable(Throwable t) {
                    promise.completeExceptionally(t);
                }
            });
    return promise;
}

With this utility method I can rewrite the previous example just doing:

getDataAsync("http://api.football-data.org/v1/soccerseasons/398")
    .thenApply(r -> gson.fromJson(r.getResponseBody(), SoccerSeason.class))
    .thenAccept(System.out::println);

Is there any better way of getting a CompletableFuture<T> from an Async Http Client request?

like image 307
Miguel Gamboa Avatar asked May 25 '16 10:05

Miguel Gamboa


People also ask

What is asynchronous HTTP client?

Overview. AsyncHttpClient (AHC) is a library build on top of Netty, with the purpose of easily executing HTTP requests and processing responses asynchronously. In this article, we'll present how to configure and use the HTTP client, how to execute a request and process the response using AHC.

How does async HTTP request work?

Asynchronous request. If you use an asynchronous XMLHttpRequest , you receive a callback when the data has been received. This lets the browser continue to work as normal while your request is being handled.

What is asynchronous request in Java?

In short, an asynchronous servlet enables an application to process incoming requests in an asynchronous fashion: A given HTTP worker thread handles an incoming request and then passes the request to another background thread which in turn will be responsible for processing the request and send the response back to the ...


Video Answer


1 Answers

With AHC2:

CompletableFuture<Response> f = asyncHttpClient
     .prepareGet("http://api.football-data.org/v1/soccerseasons/398")
     .execute()
     .toCompletableFuture();
like image 194
Stephane Landelle Avatar answered Sep 20 '22 19:09

Stephane Landelle