data-driven-docs

Selft training repo


Project maintained by ggranados Hosted on GitHub Pages — Theme by mattgraham

Reactor Core


Table of Contents


Reactor is a fourth-generation reactive library, based on the Reactive Streams specification, for building non-blocking applications on the JVM

Reactor 3 requires Java 8 or + to run

Back to top

Dependencies

Maven

<dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-core</artifactId>
    <version>3.5.9</version>
</dependency>

Back to top

Gradle

dependencies {
    compile "io.projectreactor:reactor-core:3.5.9"
}

Back to top

Reactive Data Types

Reactive Core gives us two data types that enable us to produce a stream of data, Flux and Mono.

Both, Flux and Mono are implementations of the Reactive Streams Publisher interface. Both classes are compliant with the specification, and we could use this interface in their place:

Publisher<String> just = Mono.just("foo");

Back to top

Flux

Back to top

Mono

Back to top

Producing a Stream of Data

In order for an application to be reactive, the first thing it must be able to do is to produce a stream of data.

Without this data, we wouldn’t have anything to react to

Back to top

Using Flux

Back to top

Back to top

Back to top

Back to top

Back to top

Using Mono

Mono is typically used for producing a stream of zero or one element in Project Reactor. While it might not seem as common as using Flux, there are still scenarios where you would use Mono to represent a single result or outcome.

Back to top

Back to top

Back to top

Back to top

Back to top

Back to top

Why Not Only Flux?

You might wonder why you shouldn’t just use Flux for all cases. After all, Flux can represent zero, one, or multiple elements, which seems to cover all possibilities. However, there are reasons to use Mono in certain situations:

While Flux is more versatile and can handle a wide range of scenarios, using Mono when appropriate can lead to clearer and more maintainable code.

Back to top

Subscribing to a Stream

Collecting Elements

The Flow of Elements

Comparison to Java 8 Streams

Backpressure

Operating on a Stream

Mapping Data in a Stream

Combining Two Streams

Hot Streams

Creating a ConnectableFlux

Throttling

Concurrency

Ref.