Reactive Java

Mono.timeout

Fail if no item or completion arrives within the specified duration; cancellation may not stop remote work. Source-reviewed Reactor or WebFlux example, NOT RUN.

Formal Syntax & Structure

java syntaxjava
mono.timeout(Duration.ofSeconds(2))

Working Example

Example Snippetjava
package lab;import java.time.Duration;import org.springframework.http.HttpStatus;import org.springframework.web.reactive.function.client.WebClient;import reactor.core.publisher.Mono;public class CatalogClient {    // Inject a client with fixed trusted base URL, bounded codec size and connection timeout.    private final WebClient client;    public CatalogClient(WebClient client) { this.client = client; }    public Mono<CatalogApplication.Item> read(String sku) {        return client.get().uri("/items/{sku}", sku).exchangeToMono(response -> {            if (response.statusCode().equals(HttpStatus.NOT_FOUND)) return Mono.empty();            if (response.statusCode().is2xxSuccessful())                return response.bodyToMono(CatalogApplication.Item.class);            return response.createException().flatMap(Mono::error);        }).timeout(Duration.ofSeconds(2));    }}
Java Reference