我如何获得一个CompletableFuture< T>从一个异步HTTP客户端的请求? [英] How do I get a CompletableFuture<T> from an Async Http Client request?

查看:390
本文介绍了我如何获得一个CompletableFuture< T>从一个异步HTTP客户端的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

异步HTTP客户端文档我看看如何获​​得未来<应变及GT; 作为一个异步HTTP的结果Get请求简单地做,例如:

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();

不过,为了方便起见,我想获得一个 CompletableFuture&LT; T&GT; 相反,我可以申请一个,其结果转换成别的东西延续,比如反序列化从Json的响应内容到Java对象(如 SoccerSeason.java )。这是我想做些什么:

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);

根据异步HTTP客户端文档做,这是通过一个唯一的办法 AsyncCompletionHandler&LT; T&GT; 对象,并使用一个承诺。所以我建href=\"https://gist.github.com/fmcarvalho/fcfe91effc495bae5fe1110b13b5c26d\" rel=\"nofollow\">辅助手段为此一个

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;
}

通过这个工具方法,我可以改写previous例子只是在做:

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);

有没有得到什么更好的方式 CompletableFuture&LT; T&GT; 从一个异步HTTP客户端请求

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

推荐答案

通过AHC2:

CompletableFuture<Response> f = asyncHttpClient
     .prepareGet("http://api.football-data.org/v1/soccerseasons/398")
     .execute()
     .toCompletableFuture();

这篇关于我如何获得一个CompletableFuture&LT; T&GT;从一个异步HTTP客户端的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆