翻譯自:https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/Merge
Merge
Merging sequences is the operation consisting of listening for values
from several Publishers
and emitting them in a single Flux
.
合并序列是一種操作,包括監(jiān)聽(tīng)來(lái)自多個(gè)發(fā)布者的值并在單個(gè)Flux中發(fā)出它們。
On this first exercise we will begin by merging elements of two Flux
as soon as they arrive. The caveat here is that values from flux1
arrive
with a delay, so in the resulting Flux
we start seeing values from flux2
first.
在第一個(gè)練習(xí)中,我們將從兩個(gè)Flux的元素一到達(dá)就合并開始。這里需要注意的是,
來(lái)自flux1的值會(huì)延遲到達(dá),因此在得到的Flux中,我們首先看到來(lái)自flux2的值。
// Merge flux1 and flux2 values with interleave
Flux<User> mergeFluxWithInterleave(Flux<User> flux1, Flux<User> flux2) {
return Flux.merge(flux1, flux2);
}
But if we want to keep the order of sources, we can use the concat
operator.
Concat will wait for flux1
to complete before it can subscribe to flux2
,
ensuring that all the values from flux1
have been emitted, thus preserving
an order corresponding to the source.
但是如果我們想保持來(lái)源的順序,我們可以使用concat運(yùn)算符。Concat將等待flux1完成,
然后才能訂閱flux2,確保flux1中的所有值都已發(fā)出,從而保持與源對(duì)應(yīng)的順序。
// Merge flux1 and flux2 values with no interleave (flux1 values and then flux2 values)
Flux<User> mergeFluxWithNoInterleave(Flux<User> flux1, Flux<User> flux2) {
return Flux.concat(flux1, flux2);
}
You can use concat
with several Publisher
. For example, you can get two Mono
and turn them into a same-order Flux
:
您可以將concat與多個(gè)發(fā)布發(fā)布者一起使用。例如,您可以將兩個(gè)Mono轉(zhuǎn)換為一個(gè) 相同順序的Flux:
// Create a Flux containing the value of mono1 then the value of mono2
Flux<User> createFluxFromMultipleMono(Mono<User> mono1, Mono<User> mono2) {
return Flux.concat(mono1, mono2);
}