Skip to content

SYSTEM Cited by 1 source

Java CompletableFuture

What it is

java.util.concurrent.CompletableFuture is the standard Java async composition primitive, introduced in Java 8. It represents a future result that can be composed (thenApply, thenCompose, thenCombine), chained (supplyAsync(...).thenApply(...)), and terminated on deadline (orTimeout, completeOnTimeOut).

Why it shows up in timeout guidance

Zalando's timeouts post cites CompletableFuture.orTimeout as the standard-library implementation of the outer time- limiter wrap for chained calls:

"In Java, you could use CompletableFuture and several methods among which are orTimeout and completeOnTimeOut that provide built-in support for dealing with timeouts."

CompletableFuture
    .supplyAsync(orderService.placeOrder(...))
    .thenApply(paymentService.updateBalance(...))
    .orTimeout(1, TimeUnit.SECONDS);
(Source: sources/2023-07-25-zalando-all-you-need-to-know-about-timeouts)

This lets a Java service implement the chained-call time- limiter pattern without pulling in an external library — though Resilience4j's TimeLimiter module offers a more composable shape once other resilience primitives (circuit breaker, bulkhead, retry) are also in play.

The two timeout methods

  • orTimeout(long timeout, TimeUnit unit) — if the future has not completed by the deadline, it completes exceptionally with a TimeoutException. This is the "request timeout" shape.
  • completeOnTimeOut(T value, long timeout, TimeUnit unit) — if the future has not completed by the deadline, it completes normally with the supplied fallback value. This is the "request timeout + fallback" shape.

Seen in

Last updated · 550 distilled / 1,221 read