Reactive Programming in Java: Project Reactor and RxJava Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Reactive Programming in Java: Project Reactor and RxJava



Reactive programming is a development paradigm focused on processing asynchronous data streams and events. In Java, this paradigm has become widespread thanks to libraries implementing the Reactive Streams specification. The two most popular implementations are Project Reactor (the foundation of Spring WebFlux) and RxJava (Reactive Extensions for Java).



Unlike the traditional imperative approach, reactive programming allows you to efficiently work with large volumes of data without blocking execution threads. This is especially important for modern high-load web applications, microservices, and real-time systems.



Basic Concepts of Reactive Programming



The reactive approach is based on three key concepts:



  • Observable (publisher) — a data source that can generate events (elements, errors, completion).
  • Observer (subscriber) — a data consumer that reacts to events.
  • Operators — stream transformation functions (map, filter, flatMap, combineLatest, etc.).


Reactive streams follow the principle of backpressure: the subscriber can tell the publisher how many elements it is ready to process, which prevents memory overflow.



Project Reactor: Flux and Mono



Project Reactor is a reactive library from Pivotal (now VMware) that underlies Spring WebFlux. It provides two main types:



  • Flux<T> — a stream that can contain 0, 1, or many elements (asynchronous sequence).
  • Mono<T> — a stream that contains 0 or 1 element (analogous to Optional, but asynchronous).


Example: Creating and Processing a Flux



import reactor.core.publisher.Flux;import reactor.core.publisher.Mono;

public class ReactorExample { public static void main(String[] args) { // Create a stream from a list Flux<String> names = Flux.just("Alice", "Bob", "Charlie", "Diana");

// Apply operators names.filter(name -> name.length() > 3) .map(String::toUpperCase) .subscribe( result -> System.out.println("Received: " + result), error -> System.err.println("Error: " + error), () -> System.out.println("Stream completed") );

// Mono — for a single element Mono<String> greeting = Mono.just("Hello, Reactive!"); greeting.subscribe(System.out::println); }}


Execution result:

Received: ALICEReceived: CHARLIEReceived: DIANAStream completedHello, Reactive!


RxJava: Observable, Flowable and Single



RxJava is a Java port of Reactive Extensions (Rx). In version 3.x (RxJava 3), the following main types are available:



  • Observable<T> — a stream with 0..N elements, without backpressure support.
  • Flowable<T> — a stream with 0..N elements, with backpressure support.
  • Single<T> — a stream with exactly one element or an error.
  • Maybe<T> — a stream with 0 or 1 element.
  • Completable — a stream with no data, only a completion or error signal.


Example: Working with Observable and Operators



import io.reactivex.rxjava3.core.Observable;import io.reactivex.rxjava3.core.Single;

public class RxJavaExample { public static void main(String[] args) { // Create an Observable from a range of numbers Observable<Integer> numbers = Observable

Recommendations