Back
Close

Reactive Programming with Reactor 3

Reactor
174.1K views

Mono

Description

A Mono<T> is a Reactive Streams Publisher, also augmented with a lot of operators that can be used to generate, transform, orchestrate Mono sequences.

It is a specialization of Flux that can emit at most 1 <T> element: a Mono is either valued (complete with element), empty (complete without element) or failed (error).

A Mono<Void> can be used in cases where only the completion signal is interesting (the Reactive Streams equivalent of a Runnable task completing).

Like for Flux, the operators can be used to define an asynchronous pipeline which will be materialized anew for each Subscription.

Note that some API that change the sequence's cardinality will return a Flux (and vice-versa, APIs that reduce the cardinality to 1 in a Flux return a Mono).

See the javadoc here.

Marble diagram representation of a Mono

Mono in action:

Mono.just(1)
    .map(integer -> "foo" + integer)
    .or(Mono.delay(Duration.ofMillis(100)))
    .subscribe(System.out::println);

Practice

As for the Flux let's return a empty Mono using the static factory.

static <T> Mono<T> empty()
// Create a Mono that completes without emitting any item.
Empty Mono

Now, we will try to create a Mono which never emits anything. Unlike empty(), it won't even emit an onComplete event.

No Emission

Like Flux, you can create a Mono from an available (unique) value.

Create a Mono from an item

And exactly as we did for the flux, we can propagate exceptions.

Create a Mono that emits an IllegalStateException
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 reactor.core.publisher.Mono;
/**
* Learn how to create Mono instances.
*
* @author Sebastien Deleuze
* @see <a href="https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html">Mono Javadoc</a>
*/
public class Part02Mono {
//========================================================================================
// TODO Return an empty Mono
Mono<String> emptyMono() {
return null;
}
//========================================================================================
// TODO Return a Mono that never emits any signal
Mono<String> monoWithNoSignal() {
return null;
}
//========================================================================================
// TODO Return a Mono that contains a "foo" value
Mono<String> fooMono() {
return null;
}
//========================================================================================
// TODO Create a Mono that emits an IllegalStateException
Mono<String> errorMono() {
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