Reactive Programming with Reactor 3
Reactor
515.5K views
Merge
Merging sequences is the operation consisting of listening for values from several
Publisher
s and emitting them in a single 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.
Simple Merge
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.
Keep the order
You can use concat
with several Publisher
. For example, you can get two Mono
and turn
them into a same-order Flux
:
Create a flux from two mono
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Suggested playgrounds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package io.pivotal.literx;
//generic imports to help with simpler IDEs (ie tech.io)
import java.util.*;
import java.util.function.*;
import java.time.*;
import io.pivotal.literx.domain.User;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* Learn how to merge flux.
*
* @author Sebastien Deleuze
*/
public class Part05Merge {
//========================================================================================
// TODO Merge flux1 and flux2 values with interleave
Flux<User> mergeFluxWithInterleave(Flux<User> flux1, Flux<User> flux2) {
return null;
}
//========================================================================================
// TODO Merge flux1 and flux2 values with no interleave (flux1 values and then flux2 values)
Flux<User> mergeFluxWithNoInterleave(Flux<User> flux1, Flux<User> flux2) {
return null;
}
//========================================================================================
// TODO Create a Flux containing the value of mono1 then the value of mono2
Flux<User> createFluxFromMultipleMono(Mono<User> mono1, Mono<User> mono2) {
return null;
}