Java Flux and Mono
Introduction
In reactive programming, Flux and Mono are two important classes provided by the Reactor library in Java. These classes are used to represent streams of data and enable asynchronous and non-blocking processing. In this article, we will explore the concepts of Flux and Mono, and how to use them with code examples.
Flux
Flux is a reactive stream that emits zero or more elements. It can be considered as a publisher that produces data asynchronously. The Flux
class is part of the Reactor library and provides various operators to transform and process data.
To create a Flux, we can use the Flux.just()
method that accepts multiple values or an array of values. Here is an example:
import reactor.core.publisher.Flux;
public class FluxExample {
public static void main(String[] args) {
Flux<String> flux = Flux.just("Apple", "Banana", "Orange");
flux.subscribe(System.out::println);
}
}
In the above example, we create a Flux that emits three strings: "Apple", "Banana", and "Orange". We then subscribe to the Flux and print each emitted value using a lambda expression.
Flux provides many operators to manipulate the data stream. For example, we can use the map()
operator to transform each emitted value:
Flux<String> flux = Flux.just("Apple", "Banana", "Orange")
.map(String::toUpperCase);
flux.subscribe(System.out::println);
In this example, we convert each string to uppercase using the map()
operator before printing it.
Mono
Mono, as the name suggests, represents a reactive stream that emits at most one element. It can emit either a single value or no value at all. Mono is useful when dealing with asynchronous tasks that produce only one result.
Similar to Flux, Mono provides various operators to manipulate the data stream. To create a Mono, we can use the Mono.just()
method:
import reactor.core.publisher.Mono;
public class MonoExample {
public static void main(String[] args) {
Mono<String> mono = Mono.just("Hello Reactive World!");
mono.subscribe(System.out::println);
}
}
In this example, we create a Mono that emits a single string and subscribe to it to print the value.
Mono can also be used for error handling. We can use the onErrorResume()
operator to handle exceptions and return a default value:
Mono<String> mono = Mono.just("Apple")
.map(name -> {
if (name.equals("Banana")) {
throw new RuntimeException("Invalid fruit");
}
return name;
})
.onErrorResume(e -> Mono.just("Default"));
mono.subscribe(System.out::println);
In this example, if the emitted value is "Banana", we throw a RuntimeException
. However, we catch the exception using the onErrorResume()
operator and return a default value "Default". This ensures that the Mono always emits a value and doesn't terminate abruptly.
Conclusion
Flux and Mono are powerful classes provided by the Reactor library in Java for reactive programming. They enable asynchronous and non-blocking processing of data streams. In this article, we explored the concepts of Flux and Mono and saw how to create and manipulate them using code examples. By leveraging the capabilities of Flux and Mono, developers can build highly efficient and responsive applications.