subscribeOn and publishOn operators in Reactor
August 26, 2018
Reactor
does not enforce a concurrency model, it leaves that to the developer. By default the execution happens in the thread of the caller to subscribe()
.
Let’s consider the following method where we subscribe with 4 consumers to a Flux of Integers which print out the thread name while processing the emitted elements in the Flux.
public static void createSubscribers(Flux<Integer> flux) {
IntStream.range(1, 5).forEach(value ->
flux.subscribe(integer -> System.out.println(value + " consumer processed "
+ integer + " using thread: " + Thread.currentThread().getName())));
}