In Java,Mono
Classes are a core component in the Spring Reactor framework. They are an implementation of the Reactive Streams specification and are mainly used to handle asynchronous sequences containing zero or one element. Mono can represent a single value that may appear at a certain point in the future, or it means there is no value (i.e., a null value). This type of reactive type is very suitable for situations where you expect to return a single result (such as a single entity obtained by querying the database).
Here are some basic methods and examples of using Mono:
Create a Mono instance:
use(T)
Create a Mono with a single given value.
1Mono<String> mono = ("Hello, Mono!");
use()
Create a Mono that contains no value.
1Mono<Void> emptyMono = ();
Use factory methods to create Mono from other asynchronous sources, such as(Callable)
、(CompletionStage)
or(Supplier<MonoSink<T>>)
。
Subscription and Consumption
passsubscribe()
Method subscribes to Mono. When Mono completes, the corresponding callback method will be called.
(value -> ("Received: " + value), error -> ("Error: " + ()), () -> ("Completed"));
Lambda expressions can also be used to simplify errors and completed callbacks.
(::println, Throwable::printStackTrace, () -> ("Finished processing."));
Conversion and mapping
usemap(Function)
Method transforms the values in Mono.
Java
1Mono<String> transformedMono = (s -> ());
Combination Mono
useflatMap(Function)
orthen(Mono)
Connect multiple Mono operations to form chain calls.
Mono<User> userMono = getUserById(userId); Mono<Address> addressMono = (User::getAddress);
filter
usefilter(Predicate)
Filter the values in Mono according to conditions.
Mono<String> filteredMono = (str -> ("H"));
Error handling
useonErrorReturn(T)
、onErrorResume(Function)
ordoOnError(Consumer)
and other methods to deal with exceptions.
Delayed execution and timing
usedelay(Duration)
ordelayElement(Duration)
The value of Mono is emitted after a specified time.
Combined with Flux
Mono can be converted to Flux, for example, usingMono#toFlux()
Convert to Flux containing a single element, or useFlux#next()
orFlux#single()
Get a single Mono from Flux.
Combination operation
Combined with other Mono, such aszipWith(Mono, BiFunction)
、mergeWith(Mono)
wait.
Terminal operation
useblock()
、blockOptional()
ortoFuture()
etc. Force wait for Mono's result and get it.
Note: Since Mono is a Cold Signal, the relevant logic is not executed until the subscription occurs. Therefore, its advantage lies in the ability to build flexible and efficient asynchronous processing pipelines.
Why use the mono class?
Using mono classes can bring the following benefits:
Save resources: Since the mono class only creates one instance, it can save memory and other resources.
Simplified code: Using mono classes can simplify code logic. There is no need to manually create a new instance every time you use a singleton object, just call the getInstance method.
Easy access: Since a singleton object is globally unique, it is convenient to access the object anywhere in the application.
Summarize
This is all about this article about the Mono class in Java. For more related Java Mono class content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!