1

Looking for guidance on reactor schedulers.

I want to run certain IO tasks in background, i.e. send emails to the tech team. To make it asynchronous I use Mono.fromRunnable subscribed to a scheduler.

I have a choice to either use Schedulers.elastic() or Schedulers.newElastic(). I prefer the latter because it allows me to give it a unique name which would help in the logs analysis.

Is it ok to make a static variable e.g.

Scheduler emailSched = Schedulers.newElastic("email");

and subscribeOn my Mono to it versus should I create a new Scheduler instance every time?

I found only What is the difference between Schedulers.newElastic and Schedulers.elastic methods? and that did not help my question much.

1 Answers1

2

should I create a new Scheduler instance every time?

There's no technical reason why you need to if you don't want to. In most instances it probably doesn't matter.

The key differences are:

  • You can give it a different name if you want (trivial)
  • Any individual elastic scheduler will cache and reuse the executors it creates under the hood, with a default timeout of 60 seconds. That caching is not shared between different scheduler instances of the same name, however.
  • You can dispose any individual elastic scheduler without effecting other schedulers of the same name.

In the case you describe, none of those really factor into play.

Separate to the above, note that Schedulers.boundedElastic() is now the preferred option, especially for wrapping blocking IO (which seems to be what you're doing there.)

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • Hi @Michael Berry, is it a good practice to create one instance of scheduler and use it accross multiple flux subscribers? – Deekshith Anand Sep 21 '21 at 09:00
  • Something like, we can have Schedulers.newBoundedElastic(), with custom parameter and make it a spring bean, the same effect as Schedulers.boundedElastic() can be achieved right? – Deekshith Anand Sep 21 '21 at 09:09
  • 1
    @DeekshithAnand As above, I'd only create a new scheduler if there's really a compelling reason to do so - but yes, if you do then sharing that instance would make the most sense (whether that's via a Spring bean or some other mechanism.) – Michael Berry Sep 21 '21 at 11:04