6

I want to implement a hybrid of CompletableFuture.allOf() and CompletableFuture.anyOf() where the returned future completes successfully as soon as all of the elements complete successfully, or it completes exceptionally (with the same exception) as soon as any of the elements complete exceptionally. In the case of multiple elements failing, returning the exception of any of them is sufficient.

Use-case

I have a task that needs to aggregate sub-results returned by a list of CompletableFutures, but that task should stop waiting as soon as any of them fails. I understand that the sub-tasks will keep on running and that's okay.

Related questions

I found Waiting on a list of Future which initially seems like a duplicate question but the accepted answer uses CompletionService which requires Callable or Runnable as input. I am looking for a solution that takes already-running CompletionStages as input.

Gili
  • 86,244
  • 97
  • 390
  • 689

2 Answers2

9

This question is actually very similar to Replace Futures.successfulAsList with Java 8 CompletableFuture?

Although the question is not exactly the same, the same answer (from myself) should satisfy your needs.

You can implement this with a combination of allOf() and chaining each input future with an exceptionally() that would make the future returned by allOf() immediately fail:

CompletableFuture<String> a = …, b = …, c = …;
CompletableFuture<Void> allWithFailFast = CompletableFuture.allOf(a, b, c);
Stream.of(a, b, c)
    .forEach(f -> f.exceptionally(e -> {
        allWithFailFast.completeExceptionally(e);
        return null;
    }));
Didier L
  • 18,905
  • 10
  • 61
  • 103
  • i see that even if one of them fails (say a from ur example) - the other 2 are still picked up the executor service. are tasks marked as failure not automatically removed from the threadpool ?? – redzedi Dec 14 '19 at 10:31
  • for a true fail-fast u will have to cancel each of the tasks in ```f.exceptionally``` – redzedi Dec 14 '19 at 10:47
  • 1
    Indeed, `CompletableFuture` does not provide any mechanism to cancel the task behind it. This is due to the fact that it does not hold a reference to that task, and there might actually be no such concrete task since you can create a `CompletableFuture` programmatically. – Didier L Dec 14 '19 at 13:57
-1

I believe this will do the job:

/**
 * @param arrayOfFutures an array of futures to wait on
 * @return a {@code CompletableFuture} that completes successfully once all elements have completed successfully, or completes
 * exceptionally after any of the elements has done the same
 * <br>{@code @throws NullPointerException} if {@code arrayOfFutures} is null
 */
public CompletableFuture<Void> waitForAllButAbortOnFirstException(CompletableFuture<?>... arrayOfFutures)
{
    if (arrayOfFutures == null)
      return CompletableFuture.failedFuture(new NullPointerException("arrayOfFutures may not be null"));
    if (arrayOfFutures.length == 0)
        return CompletableFuture.completedFuture(null);
    return CompletableFuture.anyOf(arrayOfFutures).
        thenApply(unused ->
        {
            // Remove any futures that completed normally and try again
            return Arrays.stream(arrayOfFutures).
                filter(element -> !element.isDone() || element.isCompletedExceptionally()).
                toArray(CompletableFuture[]::new);
        }).
        thenCompose(remainingFutures -> waitForAllButAbortOnFirstException(remainingFutures));
}
Gili
  • 86,244
  • 97
  • 390
  • 689