`subscribeOn()` tells Project Reactor which scheduler should handle a subscription. It does not start the reactive flow: `subscribe()` does that. Use `subscribeOn(Schedulers.parallel())` when subscriptions should run on workers from a fixed-size pool. section 11.3.3
Transform names in parallel
Start with a `Flux` of names. For each name, `flatMap()` creates a `Mono`, maps the name to a `Player`, and applies `subscribeOn(Schedulers.parallel())` to that inner `Mono`. The individual mappings can then run asynchronously and in parallel, and `flatMap()` combines their results into one `Flux<Player>`. Unlike the synchronous `map()` version, do not rely on the players arriving in source order. section 11.3.3
@Test
public void flatMap() {
Flux<Player> playerFlux = Flux
.just("Michael Jordan", "Scottie Pippen", "Steve Kerr")
.flatMap(n -> Mono.just(n)
.map(p -> {
String[] split = p.split("\\s");
return new Player(split[0], split[1]);
})
.subscribeOn(Schedulers.parallel())
);
List<Player> playerList = Arrays.asList(
new Player("Michael", "Jordan"),
new Player("Scottie", "Pippen"),
new Player("Steve", "Kerr"));
StepVerifier.create(playerFlux)
.expectNextMatches(p -> playerList.contains(p))
.expectNextMatches(p -> playerList.contains(p))
.expectNextMatches(p -> playerList.contains(p))
.verifyComplete();
}
The test shows where to place `subscribeOn()` in each inner `Mono` and verifies the resulting players without requiring a fixed order. section 11.3.3
Table 11.1 Concurrency models for Schedulers (view table figure)
Compare `parallel()` with the other available scheduler choices before selecting how subscriptions should run. section 11.3.3
