1
votes

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

1 Answers

1
votes

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.)