简体   繁体   中英

Spring Reactor: How to zip two Flux, but with order?

Suppose I have two external services. Let's say we have an item Foo and serviceA returns item A while serviceB returns item B .

What I'd like to get is an handler of the form (A a, B b) where a and b are the corresponding objects for the same queried item.

Flux::zip is the closest thing I've found so far, but it's not quite what I'm looking for as order is not promised. I'm looking for something like CompletableFuture::allOf

I can always cheat by making these two calls synchronous but this takes all the fun from reactive programming. Alternatively, I could manage some cache and emit a record only when the two items has been arrived, but I prefer having things stateless.

If you use Flux.flatMap(f1) or Flux.flatMap(f2) before or during the zip operation, check if these flatMap are executing other asyncronic methods like API endpoint call. In this case maybe you will need to replace them for Flux.flatMapSequential(f1) or Flux.flatMapSequential(f2) to keep the order of the flux elements after the transformations.

Checkout: concatWith . You can explore other concat variants as per your requirement.

Note: Flux.concat(f1, f2) or f1.concatWith(f2) are essentially same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM