Search code examples
reactor

Is it possible to create large amounts of Mono instances in reactor?


I need to send tens of thousands of messages to a client, each message has a delay time, for example, Message1 needs to delay for 10ms before send, Message2 needs to delay for 200ms before send. I am considering to use reactor to do the job, i am thinking about create a Mono for each message and call the delayElement method so that messages will be emitted based on the delay. But the problem is that there will be too many Mono instances. Is this the proper way to use reactor or if there any suggestions?


Solution

  • if delay is dynamic i thin this is the best solution to do

       Flux.just(messages)
                .flatMap(o -> Mono.just(o)
                        .delayElement(Duration.ofSeconds(o.getDelay())))