I am returning following response to the user
class FinalResponseDTO {
List<Service1ResponseDTO> service1ResponseDTO;
Long totalCount;
List<Service2ResponseDTO> service2ResponseDTO;
}
As of now I am making three sequential calls to compute this FinalResponseDTO
, each of this call can be run independent of others. I tried making three different CompletableFuture
as:
CompletableFuture<List<Service1ResponseDTO> future1 = CompletableFuture.supplyAsync(() -> service1.callMethod1());
CompletableFuture<Long> future2 = CompletableFuture.supplyAsync(() -> service2.callMethod2());
CompletableFuture<Service2ResponseDTO> future3 = CompletableFuture.supplyAsync(() -> service3.callMethod3());
If I do CompletableFuture.allOf(future1, future2, future3).join();
or Should I call CompletableFuture.allOf(future1, future2, future3).get();
? Even If I call either of these join
or get
then how should I construct FinalResponseDTO
from it. I am new to the Java 8 concurrency features such as CompletableFuture
and I am confused, as each return type of each of these future is different, how should I get combined responses of all such futures and then construct my final output?