The most efficient way to split a Flux to multiple Fluxes in Reactor 3

后端 未结 1 1827
北恋
北恋 2021-01-14 20:00

In Reactor 3, what\'s the most efficient way to split a heterogeneous flux to multiple fluxes by pattern matching? (And subsequent operations on each flux may be very differ

相关标签:
1条回答
  • 2021-01-14 20:15

    If the number of groups is fairly low, then you can use Flux.groupBy referenced in the project reactor docs

    For example:

    Flux<String> flux = Flux.just("a1", "b1", "c1", "a2", "b2", "c2")
            .groupBy(s -> s.charAt(0))
            .concatMap(groupedFlux -> groupedFlux
                    .startWith("Group " + groupedFlux.key()));
    
    StepVerifier.create(flux)
            .expectNext("Group a", "a1", "a2")
            .expectNext("Group b", "b1", "b2")
            .expectNext("Group c", "c1", "c2")
            .verifyComplete();
    

    You can use groupedFlux.key() to vary the operations performed for each group.

    0 讨论(0)
提交回复
热议问题