gpt4 book ai didi

java - 在 Java 中进行多个异步调用时如何忽略失败的 CompletableFuture 调用?

转载 作者:行者123 更新时间:2023-12-03 12:58:28 32 4
gpt4 key购买 nike

我想在 Java 中使用三个 REST 服务并行调用,我的代码如下:

private CompletableFuture < EmployeesListResponse > makeAsyncCall(Request request) {
return CompletableFuture.supplyAsync(
() -> {
try {
LOGGER.warn("- - - - - Employees Service async call - - - - -");
return serviceObj.findServiceImpl(request);
} catch (Exception e) {
LOGGER.warn("service async call failed...", e);
}
return null;
}, asyncExecutor).handle((res, ex) -> {
LOGGER.warn("Exceptionally...", ex.toString(), res.toString());
return new EmployeesListResponse();
});
}

CompletableFuture < EmployeesListResponse > asyncFirstCall = makeAsyncCall(request);
CompletableFuture < EmployeesListResponse > asyncSecondCall = makeAsyncCall(request);
CompletableFuture < EmployeesListResponse > asyncThirdCall = makeAsyncCall(request);

CompletableFuture.allOf(asyncFirstCall, asyncSecondCall, asyncThirdCall).join();
在上面的代码中,我打了三个电话并使用 CompletableFuture.allOf().join() 加入它们。 .当所有三个调用的服务响应均为 200 OK 时,此代码运行良好。
如果一个调用失败( 500 Internal Server Error or 404 Not Found)而其他两个服务调用都是 200 OK,那么代码将抛出异常并且整个 API 响应因异常而失败。在这种情况下,我想忽略一个有异常的服务调用,并从其他两个调用返回成功响应。
在这种情况下如何处理忽略异常?

最佳答案

所以你试图在完成之前等待所有 3 个 future 完成,但是 allOf当单个失败时,future 立即返回。相反,您可以显式等待每个:

List<CompletableFuture<String>> allFutures = Arrays.asList(asyncFirstCall, asyncSecondCall,
asyncThirdCall);
// await completion of all futures
allFutures.forEach(future -> {
try {
future.join();
} catch (CompletionException ex) {
// handled below.
}
});

if (allFutures.stream().filter(CompletableFuture::isCompletedExceptionally).count() > 2) {
throw new RuntimeException("Multiple failures");
}

// else continue with your business logic...

关于java - 在 Java 中进行多个异步调用时如何忽略失败的 CompletableFuture 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62701250/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com