Back
Close

Reactive Programming with Reactor 3

Reactor
107.8K views

Other Operations

Description

In this section, we'll have a look at a few more useful operators that don't fall into the broad categories we explored earlier. Reactor 3 contains a lot of operators, so don't hesitate to have a look at the Flux and Mono javadocs as well as the reference guide to learn about more of them.

Practice

In the first exercise we'll receive 3 Flux<String>. Their elements could arrive with latency, yet each time the three sequences have all emitted an element, we want to combine these 3 elements and create a new User. This concatenate-and-transform operation is called zip:

Zip Operator

If you have 3 possible Mono sources and you only want to keep the fastest one, you can use the first static method:

Fatest Mono

For Flux, a similar result can be achieved using the firstEmitting static method. In this case it's the flux which emits an initial element first which is selected. Flux aren't mixed.

First Emiting

Sometimes you're not interested in elements of a Flux<T>. If you want to still keep a Flux<T> type, you can use ignoreElements(). But if you really just want the completion, represented as a Mono<Void>, you can use then() instead:

Flux Completion

Reactive Streams does not allow null values in onNext. There's an operator that allow to just emit one value, unless it is null in which case it will revert to an empty Mono. Can you find it?

Null aware user to mono

Similarly, if you want to prevent the empty Mono case by falling back to a different one, you can find an operator that does this switch:

Otherwise if empty
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
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 use various other operators.
*
* @author Sebastien Deleuze
*/
public class Part08OtherOperations {
//========================================================================================
// TODO Create a Flux of user from Flux of username, firstname and lastname.
Flux<User> userFluxFromStringFlux(Flux<String> usernameFlux, Flux<String> firstnameFlux, Flux<String> lastnameFlux) {
return null;
}
//========================================================================================
// TODO Return the mono which returns its value faster
Mono<User> useFastestMono(Mono<User> mono1, Mono<User> mono2) {
return null;
}
//========================================================================================
// TODO Return the flux which returns the first value faster
Flux<User> useFastestFlux(Flux<User> flux1, Flux<User> flux2) {
return null;
}
//========================================================================================
// TODO Convert the input Flux<User> to a Mono<Void> that represents the complete signal of the flux
Mono<Void> fluxCompletion(Flux<User> flux) {
return null;
}
//========================================================================================
// TODO Return a valid Mono of user for null input and non null input user (hint: Reactive Streams do not accept null values)
Mono<User> nullAwareUserToMono(User user) {
return null;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants