Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of having HTTP endpoints return Flux/Mono instances instead of DTOs [closed]

I've watched Spring Tips: Functional Reactive Endpoints with Spring Framework 5.0 and read a little about spring reactor but I can't quite understand it.

What are the benefits of having endpoints return Flux/Mono instances (jacksonified) instead of straight up dto objects (jacksonified), given that I've got netty and spring reactor active? I initially assumed that reactive streams would, in http request/response context, work more like websockets wherein the server pushes the data to the receiver with an open channel but this doesn't seem to be the case.

Also what does netty actually do better in reactive programming than tomcat?

I'm sorry if these questions seem stupid but I don't quite understand the purpose of this new framework direction. Why did it come about, how does it work and what problems does it solve?

like image 981
intelectuallychallenged Avatar asked Dec 04 '16 22:12

intelectuallychallenged


People also ask

What is the difference between mono and flux?

A Flux object represents a reactive sequence of 0.. N items, while a Mono object represents a single-value-or-empty (0..1) result. This distinction carries a bit of semantic information into the type, indicating the rough cardinality of the asynchronous processing.

What is the benefit of spring WebFlux?

Spring WebFlux makes it possible to build reactive applications on the HTTP layer. It is a reactive fully non-blocking, annotation-based web framework built on Project Reactor that supports reactive streams back pressure and runs on non-blocking servers such as Netty, Undertow and Servlet 3.1+ containers.

What is mono and flux in spring boot?

Reactor Provides two main types called Flux and Mono. Both of these types implement the Publisher interface provided by Reactive Streams. Flux is used to represent a stream of 0.. N elements and Mono is used to represent a stream of 0..1 element.

What is the use of flux in Java?

Flux: Returns 0… N elements. A Flux can be endless, meaning that it can keep emitting elements forever. Also it can return a sequence of elements and then send a completion notification when it has returned all of its elements.


2 Answers

I highly suggest you watch the recently presented in Devoxx Belgium "Reactive Web Application with Spring 5" by Rossen Stoyanchev.

In there he talks about how the Reactive Web Controller (presented below) on the surface looks like Spring MVC HTTP Servlet Request/Response Controller but it's actually not

@GetMapping("/users/{id}")
public Mono<User> getUser(@PathValiable Long id) {
   return this.userRepository.findById(id);
}

@GetMapping("/users")
public Flux<User> getUsers() {
   return this.userRepository.findAll();
}

he talks about how Servlet 3.1 although non-blocking doesn't truely work for fully reactive and how the glue code connecting the Servlet 3.1 and Reactive Streams is implemented as part of the Spring 5 changes for the Servlet 3.1 compliant web containers (Jetty and Tomcat).

And of course he is touching on fully Reactive non-blocking compliant servers (Netty, Undertow) are supported to run Reactive Streams.

like image 51
dimitrisli Avatar answered Sep 17 '22 22:09

dimitrisli


It's not right to mean that Netty is better than tomcat. The implementation is different. Tomcat uses java NIO to implement servlet 3.1 spec. Meantime, netty uses NIO as well but introduces custom api. If you want to get insight in how does servlet 3.1 implemeted in Netty, watch this video https://youtu.be/uGXsnB2S_vc

like image 26
Yevhenii Melnyk Avatar answered Sep 20 '22 22:09

Yevhenii Melnyk