Reactive Programming with Reactor 3
Reactor
515.5K views
Reactive to Blocking
Sometimes you can only migrate part of your code to be reactive, and you need to reuse reactive sequences in more imperative code.
Thus if you need to block until the value from a Mono
is available, use Mono#block()
method. It will throw an Exception
if the onError
event is triggered.
Note that you should avoid this by favoring having reactive code end-to-end, as much as possible. You MUST avoid this at all cost in the middle of other reactive code, as this has the potential to lock your whole reactive pipeline.
Value from Mono
Similarly, you can block for the first or last value in a Flux
with blockFirst()
/blockLast()
.
You can also transform a Flux
to an Iterable
with toIterable
. Same restrictions as
above still apply.
Flux to iterable
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
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 turn Reactive API to blocking one.
*
* @author Sebastien Deleuze
*/
public class Part10ReactiveToBlocking {
//========================================================================================
// TODO Return the user contained in that Mono
User monoToValue(Mono<User> mono) {
return null;
}
//========================================================================================
// TODO Return the users contained in that Flux
Iterable<User> fluxToValues(Flux<User> flux) {
return null;
}
}