简体   繁体   中英

How to iterate over a Tuple using zipWith in Flux?

I want to iterate over a base list that offers referenceIds . Then in the next step, I want to make multiple service calls and aggregate the results in a tuple.

Then as final step, I want to iterate over all my referenceIds , and return each generated JSONObject directly via the event stream.

@GetMapping(value = "/test", produces = TEXT_EVENT_STREAM_VALUE)
public Flux<JSONObject> test(Integer pages) {
    pages = 1;
    return Flux.range(1, pages)
            .map(pageNumber -> Arrays.asList(1 * pageNumber, 2 * pageNumber, 3 * pageNumber)) //referenceIds for testing
            .flatMap(numbers -> Flux.fromIterable(numbers).zipWith(Mono.zip(a(numbers), b(numbers)))) //results based on reference ids
            .map(tuple -> {
                Integer number = tuple.getT1();
                Tuple2<List<String>, List<String>> lookup = tuple.getT2();

                JSONObject json = new JSONObject();
                json.put("number", number);
                json.put("someMore", <fromLookup>);
                return json;
            });
}

For this example, the return types of methods a() and b() do not matter. The important part is:

If I just return Flux.fromIterable(numbers); everything works fine. BUT when aggregating using .zipWith() , I'm only receiving the 1st element of the numbers list. All others are lost. Why?

Sidenote: I need to use .zipWith() to execute those method calls in parallel (some longer running transactions).

All others are lost. Why?

From Flux class documentation:

Zip this {@link Flux} with another {@link Publisher} source, that is to say wait for both to emit one element and combine these elements once into a {@link Tuple2}. The operator will continue doing so until any of the sources completes.

You can do you a() and b() calls, zip the results, and then unwind numbers list to flux and add the result like this:

   .flatMap(numbers -> Mono.zip(a(numbers), b(numbers))
                .flatMapMany(tuple -> Flux.fromIterable(numbers).map(i -> Tuples.of(i,tuple))))

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