Create a Mono in Java Reactor

In the previous post, we covered the basics of Mono. Here, you will learn how to create a Mono using different methods.

How to Create a Mono?

We can create a Mono in the following ways:

  • Using the Mono.just(T data) method
  • From Supplier interface
  • From Callable interface
  • From Future


Create a Mono with the Mono.just(T data) method

A static method just(T data) accepts a parameter of any type and returns a Mono. The newly created Mono object will hold the value we passed as the method parameter.

Syntax

public static <T> Mono<T> just(T data)


Example

 
import reactor.core.publisher.Mono;
 
class ReactiveJavaTutorial {

  public static void main(String[] args) {

    Mono<String> mono = Mono.just("Hello");

  }
}


We can also create a Mono using the Publisher interface as the type.

import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;

class ReactiveJavaTutorial {

  public static void main(String[] args) {

    Publisher<String> mono = Mono.just("Hello");

  }
}


The justOrEmpty(T data) method returns an empty Mono if the provided data is empty or null.

import reactor.core.publisher.Mono;

class ReactiveJavaTutorial {

  public static void main(String[] args) {

    // will create an empty Mono
    Mono<String> mono = Mono.justOrEmpty(null);

  }
}


Create a Mono from the Supplier interface

We can use the fromSupplier(Supplier<? extends T> supplier) method to create a Mono. This method accepts the Supplier interface as an input.

Syntax

  public static <T> Mono<T> fromSupplier(Supplier<? extends T> supplier)


Example

import reactor.core.publisher.Mono;

class ReactiveJavaTutorial {

  public static void main(String[] args) {

    Mono <String> mono = Mono.fromSupplier(() -> "Hello");

  }
}


The provided Supplier does not accept any input parameters. This method is useful when we need to provide some data that still need to be calculated. If we already have the data that we want to put into a Mono, we should always use the from(T data) method.


From Callable interface

This way of creating a Mono is very similar to creating from the Supplier interface. In this case, we use the fromCallable() that accepts the Callable interface as an input.

Syntax

public static <T> Mono<T> fromCallable(Callable<? extends T> supplier)


Example

public class ReactiveJavaTutorial {

  public static void main(String[] args) {

    Mono<String> mono = Mono.fromCallable(() -> "Hello");

  }
}

This Callable does not accept any input parameters.

Difference between fromSupplier() and fromCallable()

If we use a Callable, we will get a result or face an error if something goes wrong.
With the Supplier, we will always get the result because this interface just supplies a value, and that’s it.

We should always use Callable when working with multiple threads.


Creating a Mono from Future

There is a way to obtain a Mono from the CompletableFuture using the fromFuture() method.

Syntax

  public static <T> Mono<T> fromFuture(CompletableFuture<? extends T> future)


Example

class ReactiveJavaTutorial {

  public static void main(String[] args) {

    CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "some value");
    Mono<String> mono = Mono.fromFuture(completableFuture);

  }
}


There are a few more ways to create a Mono. We covered the most used ones in the Reactive Java world.

Note: Reactive Streams are lazy. That means that nothing will happen with the Mono unless we subscribe to it. 

In the next lesson, you will learn to subscribe to a Mono and consume the data from it.

That’s it!

Leave a Reply

Your email address will not be published. Required fields are marked *