1 回答

TA貢獻(xiàn)1807條經(jīng)驗 獲得超9個贊
感謝您的輸入并沒有直接思考,當(dāng)然只是有多個訂閱者,如下所示:
val flux = Flux.just("MyData1", "MyData2", "MyData3");
flux.doOnNext { println("Subscribing one$it") }.subscribe()
flux.doOnNext { println("Subscribing Two$it") }.subscribe()
將輸出:
Subscribing oneMyData1
Subscribing oneMyData2
Subscribing oneMyData3
Subscribing TwoMyData1
Subscribing TwoMyData2
Subscribing TwoMyData3
正如上面所建議的,有Share,但這個 API 不允許設(shè)置最小訂閱者數(shù)量,因此最好調(diào)用下面的函數(shù),就我而言,我想等到我們有兩個訂閱者。文檔指出
首次訂閱的 Flux 會導(dǎo)致源 Flux 訂閱一次,因此遲到的訂閱者可能會錯過項目。
val flux = Flux.just("MyData1", "MyData2", "MyData3").publish().refCount(2)
這會產(chǎn)生以下輸出,以確保在啟動第二個訂閱者出現(xiàn)延遲時不會丟失消息。
Subscribing oneMyData1
Subscribing TwoMyData1
Subscribing oneMyData2
Subscribing TwoMyData2
Subscribing oneMyData3
Subscribing TwoMyData3
添加回答
舉報