我怎样才能跳过异常完成的期货
How Can I skip futures which are completing exceptionally
下面的代码片段巩固了可完成的未来。下面的问题是我的一些期货异常完成所以总的来说我的结果异常完成。
来自 java 文档我理解所有 returns 异常当任何未来抛出异常时
"Returns a new CompletableFuture that is completed when all of the given CompletableFutures complete. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause."
但我没有看到任何其他 api 可以帮助我在一切都完成后获得未来
有人可以帮助我或任何线索我怎样才能跳过异常完成的期货。换句话说,我想获得无一例外地完成的期货。
CompletableFuture<List<Pair<ExtensionVO, GetObjectResponse>>> result =
CompletableFuture.allOf(
completableFutures.toArray(new CompletableFuture<?>[completableFutures.size()]))
.thenApply(
v ->
completableFutures
.stream()
.map(CompletableFuture::join)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
首先,您必须使用 handle
链接一个即使在异常情况下也能产生结果的函数,然后,在调用 [=14= 之前使用 .filter(f -> !f.isCompletedExceptionally())
跳过异常完成的期货]:
CompletableFuture<List<Pair<ExtensionVO, GetObjectResponse>>> result =
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture<?>[0]))
.handle((voidResult,throwable) ->
completableFutures
.stream()
.filter(f -> !f.isCompletedExceptionally())
.map(CompletableFuture::join)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
原则上可以使用throwable
来判断是否发生了异常,只在必要时进行isCompletedExceptionally()
检查:
CompletableFuture<List<Pair<ExtensionVO, GetObjectResponse>>> result =
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture<?>[0]))
.handle((voidResult, throwable) ->
(throwable == null?
completableFutures.stream():
completableFutures.stream().filter(f -> !f.isCompletedExceptionally()))
.map(CompletableFuture::join)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
但这可能只对非常大的列表有效,如果有的话。
下面的代码片段巩固了可完成的未来。下面的问题是我的一些期货异常完成所以总的来说我的结果异常完成。
来自 java 文档我理解所有 returns 异常当任何未来抛出异常时 "Returns a new CompletableFuture that is completed when all of the given CompletableFutures complete. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause."
但我没有看到任何其他 api 可以帮助我在一切都完成后获得未来
有人可以帮助我或任何线索我怎样才能跳过异常完成的期货。换句话说,我想获得无一例外地完成的期货。
CompletableFuture<List<Pair<ExtensionVO, GetObjectResponse>>> result =
CompletableFuture.allOf(
completableFutures.toArray(new CompletableFuture<?>[completableFutures.size()]))
.thenApply(
v ->
completableFutures
.stream()
.map(CompletableFuture::join)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
首先,您必须使用 handle
链接一个即使在异常情况下也能产生结果的函数,然后,在调用 [=14= 之前使用 .filter(f -> !f.isCompletedExceptionally())
跳过异常完成的期货]:
CompletableFuture<List<Pair<ExtensionVO, GetObjectResponse>>> result =
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture<?>[0]))
.handle((voidResult,throwable) ->
completableFutures
.stream()
.filter(f -> !f.isCompletedExceptionally())
.map(CompletableFuture::join)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
原则上可以使用throwable
来判断是否发生了异常,只在必要时进行isCompletedExceptionally()
检查:
CompletableFuture<List<Pair<ExtensionVO, GetObjectResponse>>> result =
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture<?>[0]))
.handle((voidResult, throwable) ->
(throwable == null?
completableFutures.stream():
completableFutures.stream().filter(f -> !f.isCompletedExceptionally()))
.map(CompletableFuture::join)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
但这可能只对非常大的列表有效,如果有的话。