翻譯自: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
.
合并序列是一種操作,包括監聽來自多個發布者的值并在單個Flux中發出它們。
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.
在第一個練習中,我們將從兩個Flux的元素一到達就合并開始。這里需要注意的是,
來自flux1的值會延遲到達,因此在得到的Flux中,我們首先看到來自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.
但是如果我們想保持來源的順序,我們可以使用concat運算符。Concat將等待flux1完成,
然后才能訂閱flux2,確保flux1中的所有值都已發出,從而保持與源對應的順序。
// 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與多個發布發布者一起使用。例如,您可以將兩個Mono轉換為一個 相同順序的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);
}