Java Reactive Programming: Why not ?
The ReactiveX website defines the reactive programming as follows:
Reactive programming combines functional programming, the observer pattern and the iterable pattern.
While this definition captures some core elements of the reactive programming, like the observer and the iterator patterns, I found it somewhat incomplete. A more consistent one is provided by Amazon Q:
Reactive programming is a programming paradigm focused on asynchronous data streams and their transformations over time, combining both push and pull models through the observer and iterator patterns, while incorporating functional programming principles. It includes built-in mechanisms for handling backpressure and provides tools for building responsive, resilient, and elastic systems.
As opposed to other JVM based programming languages like Scala and Clojure, Java wasn't initially a native reactive programming language. But this changed since Java 9 which introduced, in 2017, the Flow API.
But using this API explicitly has been proved complicated in the wake of its complexity, verbosity and low level. Several libraries, like RxJava, Reactor or Mutiny, have emerged throughout the years aiming at facilitating the use of the Flow API, by providing lots of ready-to-used operators for transforming, combining and manipulating streams, as well as sophisticated methods for mapping, filtering and error handling.
Quarkus, the well known supersonic, subatomic Java stack, integrates with Mutiny. This library was designed several years later than RxJava or Reactor and, hence, it benefits of a more simplified operator and method set and is more intuitive, avoiding this way to developers to get lost in the map and flatmap "jungle", trying to find their way in the middle of the monads labyrinth.
A monad is a container-like structure that wraps a value, provides methods to transform it and allow chaining operations in a controlled way.
The simplest possible example of a Java monad is the Optional class. Look at the example below:
Optional<String> result = Optional.of("hello")
.map(str -> str.toUpperCase())
.flatMap(str -> Optional.of(str + " WORLD"));
This simple example has:
Writing code in the monadic style shown above doesn't have anything to do with the reactive programming or Mutiny but prepares you to adopt a Java functional approach.
To come back to Mutiny, it provides to Java developers a very convenient way to address the reactive programming, while circumventing the most common obstacles to its adoption. Integrated with Quarkus in a ready-to-use and out-of-the-box way, Mutiny can be used, of course, with vanilla Java.