DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Reactive Programming in Java: Using the WebClient Class
  • Embracing Reactive Programming With Spring WebFlux
  • Comparing ModelMapper and MapStruct in Java: The Power of Automatic Mappers
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS

Trending

  • REST vs. Message Brokers: Choosing the Right Communication
  • Deploy Like a Pro: Mastering the Best Practices for Code Deployment
  • Auditing Spring Boot Using JPA, Hibernate, and Spring Data JPA
  • Mastering Persistence: Why the Persistence Layer Is Crucial for Modern Java Applications
  1. DZone
  2. Coding
  3. Frameworks
  4. Reactive Programming

Reactive Programming

In this article, the reader will learn about how to take advance of Reactive Programming with Java and Spring Framework.

Elyes Ben Trad user avatar by
Elyes Ben Trad
·
May. 31, 23 · Tutorial
Like (2)
Save
Tweet
Share
4.11K Views

Join the DZone community and get the full member experience.

Join For Free

Before 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. 

Reactive Streams API

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.

Spring Reactive Web Starter

Step 2: Create Your Model 

Create Your Model

Step 3: Create Your Repository and RepositoryImpl 

Create Your Repository and RepositoryImpl

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 

Starting with Mono Operations

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

Flux Operations

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) :

second case

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.

Reactive programming Spring Framework Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Reactive Programming in Java: Using the WebClient Class
  • Embracing Reactive Programming With Spring WebFlux
  • Comparing ModelMapper and MapStruct in Java: The Power of Automatic Mappers
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: