问题描述
我一直在使用 CompletableFuture.allOf(...)
助手来创建汇总期货,这些期货只有在其复合期货被标记为完成时才会完成,即:
I have been using the CompletableFuture.allOf(...)
helper to create aggregate futures that will only become "done" when their composite futures are marked as complete, i.e:
CompletableFuture<?> future1 = new CompletableFuture<>();
CompletableFuture<?> future2 = new CompletableFuture<>();
CompletableFuture<?> future3 = new CompletableFuture<>();
CompletableFuture<?> future = CompletableFuture.allOf(future1, future2, future3);
我想对此功能稍作改动,在以下情况下,总的未来市场是完整的:
I would like a slight variation on this functionality, where the aggregate future is market as complete when:
- 所有期货均已成功完成 OR
- 任何一项期货均已完成不成功
在后一种情况下,总期货应立即(例外)完成,而不必等待其他期货完成,例如 fail-fast 。
In the latter case, the aggregate future should complete (exceptionally) immediately, and not have to wait for the other futures to complete, i.e. to fail-fast.
为了说明这一点,与 CompletableFuture.allOf(...)
考虑一下:
To illustrate this in contrast to CompletableFuture.allOf(...)
consider this:
// First future completed, gotta wait for the rest of them...
future1.complete(null);
System.out.println("Future1 Complete, aggregate status: " + future.isDone());
// Second feature was erroneous! I'd like the aggregate to now be completed with failure
future2.completeExceptionally(new Exception());
System.out.println("Future2 Complete, aggregate status: " + future.isDone());
// Finally complete the third future, that will mark the aggregate as done
future3.complete(null);
System.out.println("Future3 Complete, aggregate status: " + future.isDone());
使用 allOf(...)
,此代码产生:
Future1 Complete, aggregate status: false
Future2 Complete, aggregate status: false
Future3 Complete, aggregate status: true
而Feature2完成后,我的替代汇总实现将返回 true
Whereas my alternative aggregate implementation would return "true" after Feature2 was completed, given it was an exceptional.
我在Java标准库中找不到能够帮助我实现这一目标的工具,
I cannot find any utils in the Java standard library that will help me achieve this, which feels strange... given it's a relatively vanilla use-case.
看着 CompletableFuture.allOf(...)这些场景背后的逻辑非常复杂。我不愿意自己写这个,我想知道是否还有其他选择?
Looking at the implementation of
CompletableFuture.allOf(...)
it's fairly obvious that the logic behind these scenarios is fairly complex. I'd loathe to have to write this myself, I was wondering if there are any alternatives?
推荐答案
尽管在语法上不那么甜美作为
CompletableFuture.allOf(...)
方法,看来 thenCompose(...)
可以提供解决方案:
Although not as syntactically sweet as the
CompletableFuture.allOf(...)
method, it appears that thenCompose(...)
may offer the solution:
CompletableFuture<?> future = future1.thenCompose((f) -> future2).thenCompose((f) -> future3);
这将产生所需的内容:
Future1 Complete, aggregate status: false
Future2 Complete, aggregate status: true
Future3 Complete, aggregate status: true
可以将其包装在帮助程序方法中,该方法可以为调用方提供一些语法上的技巧:
This could be wrapped up in a helper method which would offer some syntactic niceties to the caller:
private static CompletableFuture<?> composed(CompletableFuture<?> ... futures) {
// Complete when ALL the underlying futures are completed
CompletableFuture<?> allComplete = CompletableFuture.allOf(futures);
// Complete when ANY of the underlying futures are exceptional
CompletableFuture<?> anyException = new CompletableFuture<>();
for (CompletableFuture<?> completableFuture : futures) {
completableFuture.exceptionally((t) -> {
anyException.completeExceptionally(t);
return null;
});
}
// Complete when either of the above are satisfied
return CompletableFuture.anyOf(allComplete, anyException);
}
允许:
CompletableFuture<?> future = composed(future1, future2, future3);
这篇关于CompletableFuture-汇总未来的快速失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!