Introduction
Spring WebFlux, like SpringMVC, provides reactive, async, non-blocking programming support for web applications in an annotated Controller style.
This method is comparable to how Node.js works. Javascript employs an async, non-blocking approach, which contributes to its scalability. It uses a similar architecture, but with several event loops.
Spring WebFlux, a different approach to creating web apps built on reactive programming.. Reactive apps using WebFlux and the HTTP layer. It is a reactive, non-blocking web framework built on Project Reactor that supports reactive streams back pressure and works on non-blocking servers. The event loop architecture is used to create non-blocking servers since it only needs a small number of threads to execute requests. Non-blocking or asynchronous request processing denotes the absence of any waiting threads. In essence, threads process and complete their duty without having to wait for earlier tasks to finish.
Why Use Spring WebFlux?
You can utilise CPU and network resources more effectively with Spring WebFlux, which will also enable you to offer a more scalable architecture and a more responsive user experience.
Reactive streams and Project Reactor
Spring WebFlux has a standardised mechanism for handling asynchronous streams with non-blocking backpressure, the Reactive Stream API. Backpressure is the ability to make data requests when the consumer is ready to process them.
The model for reactive streams is publisher (producer) — subscriber (consumer). An event is released by the publisher, and a subscriber reads it. Four major interfaces make up the Reactive Streams API:
- Publisher – Distributes events to subscribers in response to requests from those subscribers. There is only one way for a publisher to serve many subscribers, and that is to subscribe.
- Subscriber – Receives events that the Publisher emits as a Subscriber. The subscribe has four methods: onSubscribe, onNext, onError, and onComplete to handle events received.
- Subscription – represents the partnership between the subscriber and the publisher. It includes mechanisms for cancelling event demands and obtaining data.
- Processor – Publisher and Subscriber together is a processor.
Dependency for Spring WebFlux
We can integrate our Spring boot application with Webflux by adding the following dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
Comparison between Spring MVC and Spring Webflux
Spring WebFlux: A Functional framework
Additionally to annotated controllers, Spring WebFlux allows functional endpoints. We can now route and handle requests using functions thanks to the functional web framework.
Contrary to the annotation-based paradigm, which uses the Spring MVC framework to search the classpath for @Controller annotated classes and register look up methods that are annotated with @RequestMapping, the functional web framework uses HandlerFunction and RouterFunction.
A Few WebFlux Core Classes
The WebHandler class
An agreement to handle a web request is a WebHandler. It manages a function that is called to handle an exchange from a web server.
The HttpHandler class
Reactive HTTP request handling’s lowest level contract, HttpHandler, acts as a standard across many runtimes.
The HttpWebHandlerAdapter class
The default adapter for WebHandler’s HttpHandler contract is HttpWebHandlerAdapter.
The RouterFunctionMapping Class
A HandlerMapping implementation that supports RouterFunctions is RouterFunctionMapping. If a RouterFunction is not specified when the mapping is being built, it will look at the application context and gather all beans of that type in the correct sequence.
Conclusion
In this blog, we read and understood about Spring Webflux. We went through what it is, Why to use it, Reactive Streams and Project Reactor, Difference between MVC and Webflux and the dependency which allows us to use Webflux in our project. Got a brief understanding about a few core classes.
Stay tuned for more blogs at : https://blog.knoldus.com/