7

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?

Stan Ostrovskii
  • 528
  • 5
  • 19

1 Answers1

3

This worked for me. Instead of:

messageFlux.timeout(Duration.ofSeconds(30))

I do:

messageFlux.timeout(Mono.just(0L).delayElement(Duration.ofSeconds(30)))
Stan Ostrovskii
  • 528
  • 5
  • 19