Wait for Some Spring Listenablefuture

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.

1

2 Answers

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...)

1

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() {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.