Async

3 posts in this section

CompletableFuture: Async Pipelines and Non-Blocking Composition

The Problem with Future<T> Fetching a product page on an e-commerce site requires at least three data sources: product details, live pricing, and inventory availability. Done sequentially, a 200 ms call to each service adds up to 600 ms. Done in parallel with a properly composed CompletableFuture pipeline, the wall-clock time drops to roughly the slowest of the three — around 200 ms. This article shows exactly how to build that pipeline and everything else you need to write robust async code in Java 8.

Continue reading »

HTTP Client API (JEP 321): HTTP/2, Async, and Authentication

Why a New HTTP Client? HttpURLConnection — Java’s HTTP API since Java 1.1 — has deep design problems: Mutable shared state makes it error-prone in multithreaded code No built-in HTTP/2 support No built-in async; non-blocking requires manual thread management Clunky API: setDoOutput(true), getOutputStream(), connect() in sequence No support for reactive streams JEP 321 (Java 11) standardised the HTTP Client API that was incubating since Java 9. The new API lives in java.

Continue reading »

Async Processing with @Async and Virtual Threads

Not every operation needs to complete before the response returns. Sending an email, generating a report, publishing an event — these can run in the background. Async processing keeps request latency low while the work continues. @Async — Fire and Forget @SpringBootApplication @EnableAsync public class OrderServiceApplication { } @Service @Slf4j public class NotificationService { @Async // runs in a separate thread public void sendOrderConfirmation(Order order) { log.info("Sending confirmation for order {}", order.

Continue reading »