2

Reactor provides possibility to shift execution context to a different Scheduler like following:

Mono.just("test")
        .doOnNext(x -> System.out.println(currentThread().getName() + " " + x))
        .publishOn(MY_SCHEDULER)
        .doOnNext(x -> System.out.println(currentThread().getName() + " " + x))
        .subscribe();
-----------------------
main test
my-scheduler-1 test

How can I switch back to the main thread pool / scheduler that is used by default in Spring Webflux app (usually it appears with "main" in its name)?

fyrkov
  • 2,245
  • 16
  • 41

1 Answers1

0

The short answer is: you don't.

Longer anwser: you should not care about which thread something runs on. You can watch this talk, it is one of the points mentioned: https://www.youtube.com/watch?v=0rnMIueRKNU

IF you are going to run something on a dedicated scheduler (for instance to run some blocking tasks) you should probably choose to go back to parallel or boundedElastic, depending on if your next steps are cpu bound or not.

p.streef
  • 3,652
  • 3
  • 26
  • 50
  • You should be aware which tp things are running and control them because some persistence libs will hijack tp for the rest of the flow beyond its IO requirement and this is not what you want. Same as you don't run Hibernate without knowing what sql it generates. People should stop assuming reactor is magic. – Aubergine May 19 '21 at 09:17
  • I think it's definitely good to know these these things, but these abstractions are there for a reason, that you can also use it without knowing how it works under the hood. I would bet that most developers that use hibernate have no clue about the intricacies of everything it does. More on the subject, I think you should be aware of the scheduler not the thread when using reactor. And you should either use the correct schedulers that come with it out of the box or setup your own correctly. In the end just reading the reference guide will help a lot with the required understanding. – p.streef May 19 '21 at 12:51