Aleksey Kozel
Aleksey Kozel

Reputation: 329

Reactor (Spring Flux)

Im using Spring Flux. I need to assemble an Object from a different sources. How can I be sure that both streams returned required data?

Like:

 public Mono<MyObject> createMyObject() {

        MyObject myObject = new MyObject();

        someService.getSomeData().subscribe(myObject::setData);
        oneMoreService.getMoreData().subscribe(list -> {
            // myObject populate more fields
        });

        // how can I be sure that someData and moreData is populated, before we reach doSomeBusinessStuff method?
        return Mono.just(myObject);
    }

 public Result doSomeBusinessStuff(Mono<MyObject> myObject) {

        // make some other calculations with someData and moreData

    }

Upvotes: 1

Views: 231

Answers (1)

bsamartins
bsamartins

Reputation: 141

You can zip them.

return Mono.zip(someService.getSomeData(), oneMoreService.getMoreData())
        .map(t -> {              
            X data1 = t.getT1();
            Y data2 = t.getT2();

            MyObject myObject = new MyObject();
            //...
            return myObject;
        });

You can find information about it in the documentation. https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#zip-reactor.core.publisher.Mono-reactor.core.publisher.Mono-

Upvotes: 3

Related Questions