Srikanth Janapati
Srikanth Janapati

Reputation: 443

How to declare and join list of completable future responses in java

The flow goes controller layer -> Service layer

Here I'm calling processLOBTransactions (Async method) method from the controller layer

How can I join all CompletableFuture responses in the controller layer? my requirement is after the execution of the processLOBTransactions for each list element from the controller, in the controller layer I wanna write logs kind of thing

Could anyone please give any suggestions on how to achieve this?

**Controller Layer:**

  class ControllerLayer{
                pktransaction.getLineOfBusinessTransaction().stream().forEach((lob) -> {
                    CompletableFuture<Boolean> futureResponse = flPbtService.processLOBTransactions(lob);
                });

                  ***//HERE How can i join all CompletableFuture Responses,and i wanna print logs like all thread completed*** 

        }

**Service layer:**
class ServiceLayer{
    @Async("threadPoolTaskExecutor")
    public CompletableFuture<Boolean> processLOBTransactions(LineOfBusinessTransaction lobObj) {

             // Doing some business logic and returning the CompletableFuture as Response

        return CompletableFuture.completedFuture(new Boolean(true));

    }
}

Upvotes: 1

Views: 1512

Answers (1)

Thiyanesh
Thiyanesh

Reputation: 2360

  1. All the CompletableFuture<Boolean> futureResponse objects inside the forEach has to be stored in the Collection.
  2. Assuming parallelStream() will not be used, ArrayList can be used to gather these references
  3. Outside the loop, these references can be iterated and get() obtain the result of exception
  4. get() might wait until the actual task completes, so it may be better to use get(timeout, Unit)` to have a deterministic SLA contracts
  5. get can throw exception and be sure handle appropriate actions by catching the exception
  6. if the get with timeout, could not complete within timeout, then you can request cancel if its not a high priority operation assuming the underlying task does not consume the interruption. (it a business logic)
                ArrayList<CompletableFuture<Boolean>> futures = new ArrayList<>();
                IntStream.range(0, 10).forEach((lob) -> {
                    CompletableFuture<Boolean> futureResponse = CompletableFuture.completedFuture(new Boolean(true));
                    futures.add(futureResponse);
                });

                for (CompletableFuture<Boolean> future : futures) {
                    try {
                        System.out.println(future.get());
                        // or future.get(1, TimeUnit.SECONDS)
                    } catch (InterruptedException | ExecutionException e) {
                        System.out.println(e.getMessage());
                        //future.cancel(true); // if need to cancel the underlying task, assuming the task listens
                    }
                }

Upvotes: 2

Related Questions