Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
Wait for some Spring ListenableFuture

There is a service which uses Spring AsyncRestTemplate for sending rest calls. Each call of AsyncRestTemplate.exchange() returns ListenableFuture. Something like this:

    ListenableFuture future1 = new AsyncRestTemplate().exchange(...);
    ListenableFuture future2 = new AsyncRestTemplate().exchange(...);
    ListenableFuture future3 = new AsyncRestTemplate().exchange(...);

Is there a way for create single ListenableFuture which combines all other calls? Something like Futures.allAsList from Guava.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Step #1: Convert each ListenableFuture to CompletableFuture

public CompletableFuture<T> toCompletableFuture(ListenableFuture<ResponseEntity<String>> listenableFuture) {
    final CompletableFuture<T> completableFuture = new CompletableFuture<>();

    listenableFuture.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
        @Override
        public void onFailure(Throwable e) {
            completableFuture.completeExceptionally(e);
            }
        }

        @Override
        public void onSuccess(ResponseEntity<String> result) {
            completableFuture.complete(parseResponse(result));
            }
        }
    });
    return completableFuture;
}

Step #2: call CompletableFuture.allOf(cF1, cF2, cF3...)

about 4 years ago · Santiago Trujillo Report

0

Since Spring 5.0, ListenableFuture implements the method completable() which does exactly what you need:

    /**
     * Expose this {@link ListenableFuture} as a JDK {@link CompletableFuture}.
     * @since 5.0
     */
    default CompletableFuture<T> completable() {
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!