In Spring 5 I just know Spring WebFlux Handler method handles the request and returns Mono or Flux as response.
@Component
public class HelloWorldHandler {
public Mono<ServerResponse> helloWorld(ServerRequest request) {
return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromObject("Hello World"));
}
}
But I have no idea what means Mono and Flux and how it works with the WebFlux Handler.
Can any one simply explain
1.What means Mono and Flux.
2.How it works with the WebFlux Handler.
Thanks in advance.
Spring WebFlux internally uses Project Reactor and its publisher implementations, Flux and Mono. Mono — A publisher that can emit 0 or 1 element. Flux — A publisher that can emit 0.. N elements.
What is Spring WebFlux ? Spring WebFlux is parallel version of Spring MVC and supports fully non-blocking reactive streams. It support the back pressure concept and uses Netty as inbuilt server to run reactive applications. If you are familiar with Spring MVC programming style, you can easily work on webflux also.
both infrastructure will compete for the same job (for example, serving static resources, the mappings, etc) mixing both runtime models within the same container is not a good idea and is likely to perform badly or just not work at all.
One such reactive asynchronous programming model for servers is the event loop model: Above, is an abstract design of an event loop that presents the ideas of reactive asynchronous programming: The event loop runs continuously in a single thread, although we can have as many event loops as the number of available cores.
Webflux is all about reactive programming, which in summary means that business logic is only executed as soon as the data to process it is available (reactive).
This means you no longer can return simple POJO's, but you have to return something else, something that can provide the result when it's available. Within the reactive streams initiative, this is called a Publisher
. A Publisher
has a subcribe()
method that will allow the consumer to get the POJO when it's available.
A Publisher
(for example Publisher<Foo>
) can return zero or multiple, possibly infinite, results. To make it more clear how many results you can expect, Project Reactor (the reactive streams implementation of Pivotal) introduced two implementations of Publisher
:
Mono
, which will complete after emitting a single result. Flux
, which will emit zero or multiple, possibly infinite, results and then completes.So, basically you can see Mono<Foo>
as the reactive counterpart of returning Foo
and Flux<Foo>
as the reactive counterpart of Collection<Foo>
.
For example:
Flux
.just(1, 2, 3, 4)
.map(nr -> nr * 2)
.subscribe(System.out::println);
Even though the numbers are already available (you can see them), you should realize that since it's a Flux
, they're emitted one by one. In other cases, the numbers might come from an external API and in that case they won't be immediately available.
The next phase (the map
operator), will multiply the number as soon as it retrieves one, this means that it also does this mapping one by one and then emit the new value.
Eventually, there's a subscriber (there should always be one, but it could be the Spring framework itself that's subscribing), and in this case it will print each value it obtains and print it to the console, also, one by one.
You should also realize that there's no particular order when processing these items. It could be that the first number has already been printed on the console, while the third item is hasn't been multiplied by two yet.
So, in your case, you have a Mono<ServerResponse>
, which means that as soon as the ServerResponse
is available, the WebFlux framework can utilize it. Since there is only one ServerResponse
expected, it's a Mono
and not a Flux
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With