Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

347
Visualizações
How to run multiple service calls in parallel using CompletableFuture?

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?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

From the Javadocs of CompletableFuture.allOf():

Returns a new CompletableFuture that is completed when all of the given CompletableFutures complete. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause. Otherwise, the results, if any, of the given CompletableFutures are not reflected in the returned CompletableFuture, but may be obtained by inspecting them individually.

So when the combining CompletableFuture completes, you can inspect the values and construct the final response object using a simple constructor by applying a function that constructs the object:

CompletableFuture.allOf(future1, future2, future3).thenApply(v -> 
    new FinalResponseDTO(future1.join(), future2.join(), future3.join())
);
over 4 years ago · Santiago Trujillo Relatório

0

    CompletableFuture<List<Service1ResponseDTO> future1 = 
        CompletableFuture.supplyAsync(() -> service1.callMethod1());
    CompletableFuture<Long> future2 = 
        CompletableFuture.supplyAsync(() -> service2.callMethod2());
    CompletableFuture<List<Service2ResponseDTO>> future3 = 
        CompletableFuture.supplyAsync(() -> service3.callMethod3());

    CompletableFuture.allOf(future1, future2, future3).get();

    return new FinalResponseDTO(future1.join(), future2.join(), future3.join());

Note: supplyAsync runs on ForkJoinPool.commonPool() so it would be a good choice to provide your own executor e.g. Executors.newCachedThreadPool():

CompletableFuture.supplyAsync(() -> action, executor);
over 4 years ago · Santiago Trujillo Relatório

0

Instead of using CompletableFuture.allOf, you could fall back to the somewhat hidden Applicative of the CompletableFuture:

static <T, R> CompletableFuture<R> alsoApply(CompletableFuture<T> future,     CompletableFuture<Function<T, R>> f) {
    return f.thenCompose(future::thenApply);
}

With this helper function you can execute futures in parallel threads:

CompletableFuture<String> future = alsoApply(
    CompletableFuture.supplyAsync(() -> "a"),
    CompletableFuture.supplyAsync(() -> "b")
.thenApply(b -> a -> a + b));

assertEquals("ab", future.get());

See this question and answer for where this is coming from and why and how it works.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda