Reactive Programming
In this article, the reader will learn about how to take advance of Reactive Programming with Java and Spring Framework.
Join the DZone community and get the full member experience.
Join For FreeBefore diving into Reactive World, let's take a look at some definitions of this mechanism:
Reactive Programming is an asynchronous programming paradigm focused on streams of data.
“Reactive programs also maintain continuous interaction with their environment, but at a speed which is determined by the environment, not the program itself. Interactive programs work at their own pace and mostly deal with communication, while reactive programs only work in response to external demands and mostly deal with accurate interrupt handling. Real-time programs are usually reactive.” — Gerad Berry, French Computer Scientist.
Features of Reactive Programming
Non-Blocking: The concept of using non-blocking is important. In Blocking, the code will stop and wait for more data. Non-blocking, in contrast, will process available data, ask to be notified when more are available, then continue.
Asynchronous: Events are captured asynchronously.
Failures as Messages: Exceptions are processed by a handler function.
Backpressure: Push the data concurrently as soon as it is available. Thus, the client waits less time to receive and process the events.
Data Streams: These are coherent, cohesive collections of digital signals created on a continual or near-continual basis.
Reactive Streams API
Reactive Streams was started in 2013 by engineers from Netflix, Red Hat, Twitter, and Oracle.
The goal was to create a standard for asynchronous stream processing with non-blocking.
The Reactive Streams API was defined with four interfaces :
Publisher Interface
That allows a specific type, and it will take subscribers or allow them to subscribe to it.
Subscriber Interface
This interface implements unsubscribe, onNext, onError, and onComplete.
Subscription
This interface has two methods : request()
and cancel().
Processor
This interface extends the Subscribe and Publisher interfaces.
I know that diving into any Java Framework is the most anticipated step for every programmer, so I will choose the famous Spring Framework for the rest of the article
As you know, the traditional Java Components are blocking, and here we are using a new Stack.
Spring Reactive Types
Spring Framework 5 has introduced two new reactive types :
Mono: a publisher with zero or one element in data streams
Flux: a publisher with zero or many elements in the data streams.
Step 1:Project Creation
For the Rest, we will need the Spring Reactive Web Starter.
Lombok is optional.
Step 2: Create Your Model
Step 3: Create Your Repository and RepositoryImpl
Step 4: Test To See the Result and To Understand the Mechanism
Starting with Mono Operations
I will create a simple test class with two Mono methods:
- Blocking
- Non-Blocking
As you see, the result is the same!
Why?
Here you will not see any difference because the two methods retrieve only the first user. Blocking the rest of the users or not will not give us any results here.
Flux Operations
Here you will see the difference.
Like before, I will create a simple test class with two Flux Methods :
- Blocking
- Non-Blocking
The first method retrieves all users that exist.
But as you see, the result is only the first user!
What happened?
We are saying blockFirst
that method is saying go ahead and block but only wait for the first
element. Don't worry about the rest.
In the second case, we'll do the same thing but with Subscribe
Mechanism (Non-Blocking) :
The result is all elements.
So, that subscriber is going to be executed for every element in the flux.
Finally, to take the lead with Reactive programming, I advise you to learn about Spring Data R2DBC and Spring Web Flux, create your project and try to test different topics.
Spring Web Flux is not compatible with Spring Web MVC, but there are shared programming techniques like using the Controller annotation.
Summary
Reactive Programming focuses on processing streams of data. Traditional applications are still alive and well.
Opinions expressed by DZone contributors are their own.
Comments