Project Reactor: Flux Timeout only if first item is not emitted

Viewed 676

I have a flux that should emit an item almost immediately. After this, it may not emit an item for a long period of time. I want it to timeout if no item is initially received. But if I use the timeout(Duration) method, it will timeout every time no item is received in the given period of time.

The code I have now, which does not work for the reason stated above:

messageFlux.timeout(Duration.ofSeconds(30)).doOnError(e -> {
    // handle error
}).subscribe(m -> messageService.consumeMessage(m));

Is there even a way to do this efficiently?

1 Answers

This worked for me. Instead of:

messageFlux.timeout(Duration.ofSeconds(30))

I do:

messageFlux.timeout(Mono.just(0L).delayElement(Duration.ofSeconds(30)))
Related