IsaacLevon
IsaacLevon

Reputation: 2580

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.

Upvotes: 4

Views: 4534

Answers (2)

august0490
august0490

Reputation: 884

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.

Upvotes: 2

priyanksriv
priyanksriv

Reputation: 1154

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

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

Upvotes: 0

Related Questions