[Reactor Java #3] How do Mono and Flux behave ?
Reactor is a Java library for creating reactive non-blocking applications on the JVM based on the Reactive Streams Specification.
This article is the third of a series which goal is to guide you through the process of creating, manipulating and managing the execution of the Reactive Streams that offer Reactor through the Mono and Flux classes.
In the first two articles we covered how to create Mono and Flux, and how to apply transformations to the data they hold.
In this third article, I’ll show you how these two classes behave.
Laziness
By definition, every stream is lazy. This means that nothing is executed until you consume the stream. With Mono and Flux, the method to consume them is subscribe(...)
.
The biggest benefits of this approach is that it consumes less memory than doing transformations in an imperative way, and all the operations are Thread safe.
Immutability
Flux and Mono are immutable. This means that an instance of any of them can not be modified at all. Calling any method on them will return a new instance of Flux or Mono.
If you are not familiar with immutability, you can read this article from MIT.
Flux can be infinite
Infinite Flux can be stopped
Flux run sequentially
Conclusion
This third article covered some of the key properties of how Mono and Flux behave.
In the next article, we will cover how to take control over the execution of your Mono and Flux. This will enable you to take full advantage of the power this technology offers.