Abhijit Sarkar
Abhijit Sarkar

Reputation: 24528

Project Reactor: How to delay emission of (throttle) each element?

Consider the following Flux

Flux.range(1, 5)
  .parallel(10)
  .runOn(Schedulers.parallel())
  .map(i -> "https://www.google.com")
  .flatMap(uri -> Mono.fromCallable(new HttpGetTask(httpClient, uri)))

HttpGetTask is a Callable whose actual implementation is irrelevant in this case, it makes a HTTP GET call to the given URI and returns the content if successful.

Now, I'd like to slow down the emission by introducing an artificial delay, such that up to 10 threads are started simultaneously, but each one doesn't complete as soon as HttpGetTask is done. For example, say no thread must finish before 3 seconds. How do I achieve that?

Upvotes: 13

Views: 16043

Answers (1)

Simon Baslé
Simon Baslé

Reputation: 28301

If the requirement is really "not less than 3s" you could add a delay of 3 seconds to the Mono inside the flatMap by using Mono.fromCallable(...).delayElement(Duration.ofSeconds(3)).

Upvotes: 16

Related Questions