Reactive Programming with Reactor 3

Reactor
135.4K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

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
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content
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;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX