In this short article, we will discuss reactive streams in Java. This tutorial is the first of a series on reactive programming in Java and Spring Framework. It will lay down the basis that is required for the following tutorials. I am a big advocate of reactive streams, and I find them fun to program as they require a shift from the imperative way of thinking we are all used to.
Reactive Programming, Reactive systems, and Reactive streams.
Reactive Programming is a declarative programming paradigm concerned with data streams and change propagation. It is fundamentally asynchronous and event/message-driven. When a request is made to a resource, the resource use does not block while waiting for the response. It can move to something else while listening to events or messages that will tell it the state of execution of its request.
Now, reactive systems, as per the reactive manifesto, are systems built to make applications more resilient and scalable as they handle billions of users and petabytes of data. The principal characteristics of these systems are that they are: responsive, resilient, elastic, and message-driven. You can read the short reactive manifesto to have a better understanding of these characteristics.
Finally, Reactive Streams is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure (A very important concept we will discuss later). The main goal of Reactive Streams is to govern the exchange of stream data across an asynchronous boundary (like from one thread to another) while ensuring that the receiving side is not forced to buffer arbitrary amounts of data. Again, you can have a look at the Reactive Streams website to fully understand its scope and the problem it is aimed at solving.
Basic Components of Communication in Reactive Streams: Publishers, Subscribers, and Subscription.
We will make this part simple and straight to the point. A Publisher is a component that emits elements. The Subscriber is the component that receives the elements. The Subscriber subscribes to the Publisher, and the publisher creates a Subscription which is given to the subscriber.
The items generated by the publisher can either be stored in the publisher itself or in the subscription. It is two-way communication. The subscriber cannot only blindly receive items but can restrict the number of items it can receive by making a request using the subscription object. This is called back pressure (A concept I previously mentioned). It is essential in preventing the overloading of the subscriber.
It is to be noted that if a subscriber requests for example for 5 items when the publisher has only 3 at the moment, it will be given the 3, then when the publisher will have more items, the subscriber will receive the remaining 2.
NB: A Processor is an entity that is both a subscriber and publisher.
Hope this tutorial was helpful for you. In the next one, we will further discuss how to create publishers and subscribers in Java.