反应堆(Spring 通量)

Reactor (Spring Flux)

我正在使用 Spring 助焊剂。 我需要 assemble 来自不同来源的对象。 我如何确定两个流都返回了所需的数据?

喜欢:

 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

    }

您可以压缩它们。

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

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

您可以在文档中找到有关它的信息。 https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#zip-reactor.core.publisher.Mono-reactor.core.publisher.Mono-