Merging three Completeable futures into one with Java

The following 3 methods return CompleteableFuture<JsonObject>, The content I need is on the value property of the JsonObject so ultimately I would need to do something like response.getJsonArray("value")

CompletableFuture<JsonObject> completeableFuture1 = entityService.getProducts();
CompletableFuture<JsonObject> completeableFuture2 = entityService.getCustomers();
CompletableFuture<JsonObject> completeableFuture3 = entityService.getOrders();

Once I successfully retrieve these 3 pieces I would like to continue with the remainder of my code that depends on them. I will need access to all 3 lists: products, customers, orders.

After doing some research I think I need to use allOf(), but having trouble figuring out the correct syntax to piece it all together.

>Solution :

To merge multiple CompletableFuture instances into one, you can use the CompletableFuture.allOf() method. This method takes a varargs of CompletableFuture instances and returns a new CompletableFuture that completes when all the input futures have completed, without returning any result.

To access the results of the input futures, you can use the CompletableFuture.join() method, which returns the result of the future when it completes, or throws an exception if the future completed exceptionally.

Here’s an example of how you could use CompletableFuture.allOf() to merge the three CompletableFuture<JsonObject> instances and extract the JsonArray values:

CompletableFuture<JsonObject> productsFuture = entityService.getProducts();
CompletableFuture<JsonObject> customersFuture = entityService.getCustomers();
CompletableFuture<JsonObject> ordersFuture = entityService.getOrders();

CompletableFuture<Void> allFutures = CompletableFuture.allOf(productsFuture, customersFuture, ordersFuture);

CompletableFuture<List<JsonArray>> mergedFutures = allFutures.thenApply(
  v -> Arrays.asList(

// Continue with the remainder of your code that depends on the merged results
mergedFutures.thenAccept(results -> {
  JsonArray products = results.get(0);
  JsonArray customers = results.get(1);
  JsonArray orders = results.get(2);

  // Do something with the merged results

In this example, we first obtain the three CompletableFuture<JsonObject> instances from the entityService object, which I assume is an instance of some kind of service or API that returns JSON data as CompletableFuture instances.

We then use CompletableFuture.allOf() to merge the three input futures into a new CompletableFuture<Void> instance called allFutures, which completes when all the input futures complete.

Next, we use the thenApply() method of allFutures to create a new CompletableFuture<List<JsonArray>> instance called mergedFutures. This future is completed with a list of the JsonArray values extracted from the input futures using the join() method.

Finally, we use the thenAccept() method of mergedFutures to perform some action on the merged results once they become available. In this case, we extract the JsonArray values from the List and assign them to local variables, which can then be used in the remainder of your code.

Leave a Reply