CompletableFuture lets a Spring application run work asynchronously and compose the results without blocking a thread to wait for them. In Spring you most often meet it as the return type of an @Async method, as the return type of a controller method that completes later, or as a way to run several service calls in parallel and combine them. It replaces the older Future and the deprecated ListenableFuture.
It is part of the JDK (java.util.concurrent, since Java 8) and Spring Framework integrates with it directly. The main thing to get right is which executor runs the work.
A plain Future can only be polled or blocked on with get(). CompletableFuture implements both Future and CompletionStage, so you can attach callbacks, chain steps, combine futures, handle errors and complete one manually.
| Need | Method |
|---|---|
| Start async work returning a value | supplyAsync |
| Transform the result | thenApply |
| Chain another async call | thenCompose |
| Combine two independent results | thenCombine |
| Wait for many futures | allOf |
| Recover from an error | exceptionally, handle |
| Fail on slowness (Java 9+) | orTimeout, completeOnTimeout |
Spring runs an @Async method on a TaskExecutor, and the caller gets a CompletableFuture immediately. Enable async support once.
The method body runs on a thread from ioExecutor. Returning CompletableFuture.completedFuture(...) just wraps the final value, and the proxy hands the caller a future that completes when the method returns. Two rules apply: the method must be called from another bean (calling it from the same class bypasses the proxy and runs synchronously), and it must not be private or final.
Both calls start at once, so the total time is roughly the slower call instead of the sum. If either fails or the two seconds pass, exceptionally supplies a fallback. For a void @Async method there is no future to carry the error, so configure an AsyncUncaughtExceptionHandler through AsyncConfigurer.
Spring MVC supports asynchronous request processing: a controller method that returns CompletableFuture<T> releases the servlet thread, and the response is written when the future completes.
supplyAsync uses the shared ForkJoinPool.commonPool(), which is wrong for blocking I/O. Pass your own executor, or use the Spring one.@Async method does not share the caller's transaction. Use a context-propagating TaskDecorator where needed.get() or join() inside the async chain, as it blocks a pool thread and can deadlock a small pool.spring.threads.virtual.enabled=true makes Boot use virtual threads for its executors, which suits blocking I/O and reduces the need for hand-tuned pools.Use CompletableFuture to parallelise a few blocking calls in Spring MVC. Choose WebFlux and Reactor when the whole stack should be non-blocking and streaming.
Usually @EnableAsync is missing, or the method is called from within the same class so the proxy is bypassed. The method must also be public on a Spring bean.
Spring deprecated ListenableFuture in version 6.0 in favour of CompletableFuture. Use CompletableFuture in new code.
No. The async method runs on another thread, so it starts its own transaction context if it is @Transactional.
In Spring, CompletableFuture is the standard way to express asynchronous results: return it from @Async methods and controllers, compose it with thenCombine and allOf, and handle failures with exceptionally and timeouts. Always run it on a deliberate executor and remember that thread-bound context does not carry over.