How can I switch to the default thread pool in Spring Webflux / Reactor?

Viewed 543

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

1 Answers

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.

Related