What advantage is there to using Spring @Async vs. CompleteableFuture directly?

Christian Bongiorno picture Christian Bongiorno · Jun 21, 2017 · Viewed 10.1k times · Source

What's the advantage of using Spring Async vs. Just returning the CompletableFuture on your own?

Answer

Didier L picture Didier L · Jun 23, 2017

There is no “vs.” between the two: these are complementary technologies:

  • CompletableFuture provides a convenient way to chain different stages of asynchronous computation – with more flexibility than Spring's ListenableFuture;
  • @Async provides convenient management of your background tasks and threads, with standard Spring configuration for your executor(s).

But both can be combined (since Spring 4.2). Suppose you want to turn the following method into a background task returning a CompletableFuture:

public String compute() {
    // do something slow
    return "my result";
}

What you have to do:

  • if not already done: configure your application with @EnableAsync and an Executor bean
  • annotate the method with @Async
  • wrap its result into CompletableFuture.completedFuture()
@Async
public CompletableFuture<String> computeAsync() {
    // do something slow - no change to this part
    // note: no need to wrap your code in a lambda/method reference,
    //       no need to bother about executor handling
    return CompletableFuture.completedFuture("my result");
}

As you notice, you don't have to bother about submitting the background task to an executor: Spring takes care of that for you. You only have to wrap the result into into a completed CompletableFuture so that the signature matches what the caller expects.