Async Requests
sendAsync and CompletableFuture.
Asynchronous Requests
Blocking on every HTTP call wastes threads. The JDK HTTP client offers sendAsync, which returns a CompletableFuture immediately and performs the work on a background executor.
This lets you fire many requests concurrently and compose their results without manually managing threads.
send vs sendAsync
The signatures differ in what they return:
- send returns
HttpResponse<T>and blocks, declaringIOExceptionandInterruptedException. - sendAsync returns
CompletableFuture<HttpResponse<T>>and never blocks; errors surface through the future.