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
CompletableFutureand several methods among which areorTimeoutandcompleteOnTimeOutthat provide built-in support for dealing with timeouts."(Source: sources/2023-07-25-zalando-all-you-need-to-know-about-timeouts)CompletableFuture .supplyAsync(orderService.placeOrder(...)) .thenApply(paymentService.updateBalance(...)) .orTimeout(1, TimeUnit.SECONDS);
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 aTimeoutException. 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¶
- sources/2023-07-25-zalando-all-you-need-to-know-about-timeouts — canonical Zalando citation as the JDK-native implementation of the chained-call time-limiter pattern.
Related¶
- concepts/request-timeout — what
orTimeoutimplements at the language-standard-library level. - patterns/time-limiter-wrapping-chained-calls — the
pattern
orTimeoutrealises. - patterns/explicit-timeout-on-remote-calls — the house- style rule this enables without external dependencies.
- systems/resilience4j — the library-level alternative with more composable primitives.