I have a Project Reactor chain which includes a blocking task (a network call, we need to wait for response). I'd like to run multiple blocking tasks concurrently.
It seems like either ParallelFlux or flatMap() could be used, bare-bone examples:
Flux.just(1)
.repeat(10)
.parallel(3)
.runOn(Schedulers.elastic())
.doOnNext(i -> blockingTask())
.sequential()
.subscribe()
or
Flux.just(1)
.repeat(10)
.flatMap(i -> Mono.fromCallable(() -> {blockingTask(); return i;}).subscribeOn(Schedulers.elastic()), 3)
.subscribe();
What are the merits of the two techniques? Is one to be preferred over the other? Are there any alternatives?
parallel is tailored for parallelization of tasks for performance purposes, and dispatching of work between "rails" or "groups", each of which get their own execution context from the Scheduler you pass to runOn. In short, it will put all your CPU cores to work if you do CPU intensive work. But you're doing I/O bound work...
So in your case, flatMap is a better candidate. That use of flatMap for parallelization is more about orchestration.
These are pretty much the 2 alternatives, if you don't count the slightly different flavor of flatMap that flatMapSequential is (concatMap doesn't really allow for parallelization).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With