如何从异步 Http 客户端请求中获取 CompletableFuture<T>?
How do I get a CompletableFuture<T> from an Async Http Client request?
在 Async Http Client documentation 上,我看到了如何获取 Future<Response>
作为异步 HTTP Get 请求的结果,只需执行以下操作,例如:
AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient();
Future<Response> f = asyncHttpClient
.prepareGet("http://api.football-data.org/v1/soccerseasons/398")
.execute();
Response r = f.get();
但是,为了方便起见,我想得到一个 CompletableFuture<T>
,为此我可以应用一个将结果转换为其他内容的延续,例如将响应内容从 Json 反序列化为Java 对象(例如 SoccerSeason.java)。这就是我想要做的:
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);
根据 Async Http Client documentation ,唯一的方法是通过 AsyncCompletionHandler<T>
对象并使用承诺。所以我为此构建了一个辅助方法:
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;
}
使用这个实用方法,我可以重写前面的示例:
getDataAsync("http://api.football-data.org/v1/soccerseasons/398")
.thenApply(r -> gson.fromJson(r.getResponseBody(), SoccerSeason.class))
.thenAccept(System.out::println);
是否有更好的方法从异步 Http 客户端请求中获取 CompletableFuture<T>
?
使用 AHC2:
CompletableFuture<Response> f = asyncHttpClient
.prepareGet("http://api.football-data.org/v1/soccerseasons/398")
.execute()
.toCompletableFuture();
在 Async Http Client documentation 上,我看到了如何获取 Future<Response>
作为异步 HTTP Get 请求的结果,只需执行以下操作,例如:
AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient();
Future<Response> f = asyncHttpClient
.prepareGet("http://api.football-data.org/v1/soccerseasons/398")
.execute();
Response r = f.get();
但是,为了方便起见,我想得到一个 CompletableFuture<T>
,为此我可以应用一个将结果转换为其他内容的延续,例如将响应内容从 Json 反序列化为Java 对象(例如 SoccerSeason.java)。这就是我想要做的:
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);
根据 Async Http Client documentation ,唯一的方法是通过 AsyncCompletionHandler<T>
对象并使用承诺。所以我为此构建了一个辅助方法:
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;
}
使用这个实用方法,我可以重写前面的示例:
getDataAsync("http://api.football-data.org/v1/soccerseasons/398")
.thenApply(r -> gson.fromJson(r.getResponseBody(), SoccerSeason.class))
.thenAccept(System.out::println);
是否有更好的方法从异步 Http 客户端请求中获取 CompletableFuture<T>
?
使用 AHC2:
CompletableFuture<Response> f = asyncHttpClient
.prepareGet("http://api.football-data.org/v1/soccerseasons/398")
.execute()
.toCompletableFuture();