# Grasping Reactive Programming in Java: A Basic Guide with Reactor

The Reactor is an implementation of the reactive programming paradigm.

> `Reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change. This means that it becomes possible to express static (e.g. arrays) or dynamic (e.g. event emitters) data streams with ease via the employed programming language(s).`

`—` [`https://en.wikipedia.org/wiki/Reactive_programming`](https://en.wikipedia.org/wiki/Reactive_programming)

Over time, a standardization for Java emerged through the Reactive Streams initiative, which is a specification that outlines a set of interfaces and interaction guidelines for reactive libraries on the JVM. These interfaces have been incorporated into Java 9 under the `Flow` class.

In reactive streams, there is a `Publisher-Subscriber` pair. The `Publisher` notifies the `Subscriber` of newly available values as they arrive, with this push aspect being crucial to the reactive nature. Additionally, operations applied to pushed values are expressed declaratively rather than imperatively: the programmer conveys the logic of the computation rather than detailing its precise control flow. We will soon look at the communication between `Publisher-Subscriber`.

### Why do we need an asynchronous reactive library in the first place?

Modern applications often cater to vast numbers of concurrent users, and despite the continuous advancements in modern hardware, software performance remains a critical concern.

There are generally two methods to enhance a program's performance:

1\. **Parallelize** to utilize more threads and additional hardware resources. 2. Pursue **greater efficiency** in the current resource usage.

Usually, Java developers create programs with blocking code. This works fine until there's a performance problem. Then, they add more threads with similar blocking code. But, using more resources can quickly cause conflicts and issues with multiple users.

Additionally, blocking code wastes resources by causing potentially numerous idle threads to wait for data during latency-prone operations like database requests or network calls.

The parallelization approach, although crucial for maximizing hardware potential, can be complex to comprehend and may result in resource wastage due to contention and concurrency issues.

### From Imperative to Reactive Programming

There are two types of coding styles:

* **Imperative**: This involves a sequential series of tasks, with each task running one at a time, following the completion of the previous task. Data is processed in large quantities and cannot be passed on to the next task until the previous task has finished processing the entire dataset.
    
* **Reactive**: In this coding style, a set of tasks is defined to process data, and these tasks can run concurrently. Each task processes subsets of the data, passing it on to the next task in the sequence while continuing to work on another subset of the data.
    

---

### Publisher & Subscriber Communication

**Step 1**: Subscriber seeks to connect

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701985446202/b3fe3221-6e80-4f5a-953f-8a472ce402d1.png align="center")

**Step 2**: The Publisher calls the \``onSubscribe`\` method with an instance of Subscription

<details data-node-type="hn-details-summary"><summary>What is a Publisher?</summary><div data-type="detailsContent">A Publisher is a provider of a potentially unbounded number of sequenced elements, publishing them according to the demand received from its Subscriber(s). A Publisher can serve multiple Subscribers subscribed subscribe(Subscriber) dynamically at various points in time.</div></details><details data-node-type="hn-details-summary"><summary>Behind the scene:</summary><div data-type="detailsContent"><code>onSubscribe</code> method is invoked after calling <code>Publisher.subscribe(Subscriber)</code>. No data will start flowing until<code> Subscription.request(long)</code> is invoked. It is the responsibility of this Subscriber instance to call <code>Subscription.request(long)</code> whenever more data is wanted. The Publisher will send notifications only in response to <code>Subscription.request(long)</code>.</div></details>

**Step 3**: A `Subscription` is established between the `Subscriber` and `Publisher`.

* A `Subscription` represents a one-to-one lifecycle of `Subscriber` subscribing to a `Publisher`. It can only be used once by a single `Subscriber`. It is used to both signal the desire for data and cancel demand (and allow resource cleanup).
    
* Subscription has two methods:
    

| Methods | Description |
| --- | --- |
| `public void request(long n);` | No events will be sent by a `Publisher` until demand is signaled via this method. It can be called however often and whenever needed—but if the outstanding cumulative demand ever becomes Long.MAX\_VALUE or more, it may be treated by the `Publisher` as "<mark>effectively unbounded</mark>". Whatever has been requested can be sent by the `Publisher` so only signal the demand for what can be safely handled. A `Publisher` can send less than is requested if the stream ends but then must emit either `Subscriber.onError(Throwable)` or `Subscriber.onComplete()` |

| `public void cancel();` | **Request the** `Publisher` **to stop sending data and clean up resources. Data may still be sent to meet previously signaled demand after calling cancel** |
| --- | --- |

---

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701986943139/4ae3d86e-6fc9-4040-a418-9d0b3dd4a7dd.png align="center")

**Step 4**: `Publisher` pushes data via the `onNext` method to the Subscriber

<details data-node-type="hn-details-summary"><summary>What does onNext do?</summary><div data-type="detailsContent">Data notification sent by the Publisher in response to requests to <code>Subscription.request(long)</code>. Params: t – the element signaled</div></details>![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701989782003/1989c9b7-082d-4ba4-a5b5-6b71e5b26fef.png align="center")

**Step 5**: Publisher finished supplying elements and calls `onComplete()`

Successful terminal state. No further events will be sent even if the `Subscription.request(long)` is invoked again.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701988221860/2e29ffd3-8b46-4027-a02a-b815bfafe5ec.png align="center")

**Step 6:** If there is an error it will send `onError(Throwable t)`
