.allOf(resultB, resultC) 返回的 future 仅在 resultB 和 resultC 都完成时才完成,因此,依赖函数 ignoredVoid -> combine(resultA.join(), resultB.getNow(fallbackB), resultC.getNow(fallbackC) 仅在 resultB 和 resultC 都完成并提供时才会被评估回退完全没有效果。
通常不可能对这些函数中的get() 调用做出反应。考虑到在不同的时间和不同的超时时间可以有任意数量的get() 调用,这应该是显而易见的,但是传递给thenApply 的函数只被评估一次。
在getFuture() 中处理消费者指定超时的唯一方法是将其更改为返回一个接收超时的函数:
interface FutureFunc<R> {
R get(long time, TimeUnit u) throws ExecutionException;
}
public FutureFunc<Result> getFuture() {
CompletableFuture<A> resultA = serviceA.call();
CompletableFuture<B> resultB = resultA.thenCompose(a -> serviceB.call(a));
CompletableFuture<C> resultC = resultA.thenCompose(a -> serviceC.call(a));
CompletableFuture<Result> optimistic = CompletableFuture.allOf(resultB, resultC)
.thenApply(ignoredVoid -> combine(resultA.join(), resultB.join(), resultC.join()));
return (t,u) -> {
try {
return optimistic.get(t, u);
} catch (InterruptedException | TimeoutException ex) {
return combine(resultA.join(), resultB.getNow(fallbackB),
resultC.getNow(fallbackC));
}
};
}
public Result extractFuture(FutureFunc<Result> future) {
Result result;
try {
result = future.get(timeOut, MILLISECONDS);
} catch (ExecutionException ex) {
...
}
return result;
}
现在,可以进行具有不同超时的不同调用,只要 B 或 C 尚未完成,就可能产生不同的结果。并不是说combine 方法有一些歧义,这也可能需要一些时间。
你可以把函数改成一个
return (t,u) -> {
try {
if(resultB.isDone() && resultC.isDone()) return optimistic.get();
return optimistic.get(t, u);
} catch (InterruptedException | TimeoutException ex) {
return combine(resultA.join(), resultB.getNow(fallbackB),
resultC.getNow(fallbackC));
}
};
等待可能已经运行的combine 完成。在任何一种情况下,都不能保证在指定时间内交付结果,因为即使使用了 B 和 C 的后备值,也会执行 combine,这可能需要任意时间。
如果您想要取消类似的行为,即所有结果查询返回相同的结果,即使它是使用第一个查询的后备值计算的,您也可以使用
public FutureFunc<Result> getFuture() {
CompletableFuture<A> resultA = serviceA.call();
CompletableFuture<B> resultB = resultA.thenCompose(a -> serviceB.call(a));
CompletableFuture<C> resultC = resultA.thenCompose(a -> serviceC.call(a));
CompletableFuture<Void> bAndC = CompletableFuture.allOf(resultB, resultC);
CompletableFuture<Result> result = bAndC
.thenApply(ignoredVoid -> combine(resultA.join(), resultB.join(),
resultC.join()));
return (t,u) -> {
try {
bAndC.get(t, u);
} catch (InterruptedException|TimeoutException ex) {
resultB.complete(fallbackB);
resultC.complete(fallbackC);
}
try {
return result.get();
} catch (InterruptedException ex) {
throw new ExecutionException(ex);
}
};
}
这样,单个FutureFunc 上的所有查询将始终返回相同的结果,即使由于第一次超时而基于回退值。此变体还始终将combine 的执行排除在超时之外。
当然,如果根本不打算使用不同的超时,您可以重构 getFuture() 以提前获得所需的超时,例如作为参数。这将大大简化实现,并且可以再次返回未来:
public CompletableFuture<Result> getFuture(long timeOut, TimeUnit u) {
CompletableFuture<A> resultA = serviceA.call();
CompletableFuture<B> resultB = resultA.thenCompose(a -> serviceB.call(a));
CompletableFuture<C> resultC = resultA.thenCompose(a -> serviceC.call(a));
ScheduledExecutorService e = Executors.newSingleThreadScheduledExecutor();
e.schedule(() -> resultB.complete(fallbackB), timeOut, u);
e.schedule(() -> resultC.complete(fallbackC), timeOut, u);
CompletableFuture<Void> bAndC = CompletableFuture.allOf(resultB, resultC);
bAndC.thenRun(e::shutdown);
return bAndC.thenApply(ignoredVoid ->
combine(resultA.join(), resultB.join(), resultC.join()));
}