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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…