Show List

Project Reactor Components

The Reactor-Core module is the foundational module of Project Reactor, providing the core building blocks for reactive programming. It defines the Flux and Mono types, which represent reactive streams emitting zero to N elements (Flux) or zero or one element (Mono). Reactor-Core offers a rich set of operators for transforming, filtering, combining, and processing reactive streams, enabling developers to express complex asynchronous data processing logic in a functional and composable manner.

Key Components of Reactor-Core:

  1. Flux:

    • Flux represents a reactive stream emitting zero to N elements.
    • It can emit multiple items over time and supports asynchronous and non-blocking data emissions.
    • Flux offers a wide range of operators for transforming, filtering, combining, and processing data streams.
    • Example operators include map, filter, flatMap, zip, merge, reduce, take, skip, distinct, and many others.
  2. Mono:

    • Mono represents a reactive stream emitting zero or one element.
    • It can emit a single item or complete without emitting any items.
    • Mono is often used to represent asynchronous computations that produce a single result, such as network requests or database queries.
    • Like Flux, Mono also supports a variety of operators for transforming and processing data streams.

Examples of Reactor-Core Implementations:

Here are some examples demonstrating the use of Flux and Mono with operators:

Creating Flux and Mono:


import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public class Main { public static void main(String[] args) { // Creating a Flux emitting a sequence of integers Flux<Integer> numbersFlux = Flux.just(1, 2, 3, 4, 5); // Creating a Mono emitting a single value Mono<String> greetingMono = Mono.just("Hello, World!"); } }

Transforming Flux and Mono:


import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public class Main { public static void main(String[] args) { // Transforming a Flux by multiplying each element by 2 Flux<Integer> doubledFlux = Flux.just(1, 2, 3, 4, 5) .map(num -> num * 2); // Transforming a Mono by converting the value to uppercase Mono<String> upperCaseMono = Mono.just("hello") .map(String::toUpperCase); } }

Filtering Flux and Mono:


import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public class Main { public static void main(String[] args) { // Filtering a Flux by keeping only even numbers Flux<Integer> evenNumbersFlux = Flux.just(1, 2, 3, 4, 5) .filter(num -> num % 2 == 0); // Filtering a Mono by ensuring the value is not null Mono<String> nonNullMono = Mono.justOrEmpty(null) .filter(value -> value != null); } }

Handling Errors with Mono:


import reactor.core.publisher.Mono; public class Main { public static void main(String[] args) { // Handling errors with Mono Mono<Integer> resultMono = Mono.just("not a number") .map(Integer::parseInt) .onErrorResume(e -> Mono.just(-1)); // fallback value in case of error } }

These examples demonstrate some common operations with Flux and Mono provided by Reactor-Core. Reactor-Core's extensive set of operators enables developers to manipulate and process reactive streams efficiently and expressively, making it a powerful tool for building reactive applications in Java.


    Leave a Comment


  • captcha text