Understanding Publisher’s implementations (Reactor Java)

Kuldeep singh
SFD LLP
Published in
4 min readOct 21, 2023

--

Photo by Drew Farwell on Unsplash

Exploring Mono in Reactor Java

In this section, we will dive into the Mono publisher implementation in Reactor Java and understand how it represents a stream with zero or one element. We’ll explore real-time examples where Mono is commonly used and demonstrate its usage in asynchronous or delayed result scenarios.

Example Scenario: Fetching User Details

In this scenario, imagine a web application where we need to fetch details of a user from a remote database. Let’s see how Mono can be used for such an asynchronous operation.

Step 1: Creating a Mono We can create a Mono using the just() method and provide the user ID to fetch the details.

Mono<User> fetchUserDetails(String userId) {
// Simulating asynchronous operation to fetch user details
return Mono.fromCallable(() -> userRepository.findById(userId))
.subscribeOn(Schedulers.parallel());
}

When you use subscribeOn(Schedulers.parallel()), you're indicating that the subscription (the part of the reactive stream where the data producer and data consumer are connected) should run on a scheduler that is designed for parallel execution. This can be useful in scenarios where you have a CPU-bound or computationally intensive task and…

--

--

Kuldeep singh
SFD LLP

Tech enthusiast. Crafting code that blends innovation with functionality. Exploring tech trends, sharing insights.