Introduce the Spring WebFlux – A Practical Guide

Hello guys,

In my first blog post, I mean on this new address I choose the hot topic in java programming, the Spring 5, recently Pivotal had launched the new version of the framework and promoted this version to GA, which means this version is production ready, you can find the full list of features here.

The highlights for me are the full compatibility with Java 9, the kotlin support and finally the most commented feature, the new module called Spring WebFlux which one was built on a reactive foundation.

For this post, I will explain about the Spring WebFlux, which one can help us to write a fully non-blocking application based on event loop concepts.

Before coding, I would like to explain how event-loop works and the difference between this model and a traditional large thread pool with thread-per-request execution model…let’s go.

Event-loop Model

Popularized by nodejs, this style allows us to scale with a small number of threads instead of the thread-pool, the concept is quite simple the request arrives at event loop then it blocking on resource-emitting events and dispatches them to corresponding handlers and callbacks.

Keep in mind one important thing, you should never block the execution otherwise the application will be blocked.

This figure can help us to understand the concept

 

Some considerations before code

The Spring Framework Team chosen the Reactor as a Reactive Streams implementation is a quite similar to RxJava

The most important classes are Mono, which one represents 0 | 1 sequence and Flux which represents 0 to N elements.

Let’s do something amazing…

My choice as a datastore was Cassandra, is a popular noSQL database because Spring has a Reactive Repository for this database. Let’s do the reactive Repository

This is a simple repository, pay attention in one thing you should extend the Reactive version of Cassandra repository.

Let’s go to the service layer, this layer is like we did in before versions of Spring Framework, the only difference is, you should return Mono or Flux in your methods.

The interesting thing here is the @Autowired annotation isn’ t necessary anymore, Spring detects it.

And finally, we will create a REST layer for our applications, this is a quite similar to the last version before 5. Remember we are using the Reactor Netty as a web server this makes our application reactive.

@RequestMapping, @GetMapping, @PostMapping is unchanged, please pay attention to the return types, this needs to be changed to Mono Or Flux.

Conclusions

Spring renews your portfolio one more time, bringing concepts of reactive programming to your projects, you can use your previous knowledge about Spring to create amazing Reactive Applications.

Advice: Keep in mind one thing, the concept of reactive programming uses the declarative programming and this is totally different from imperative programming.

You can find the full code on my GitHub

On the next post, I will explain the Kotlin support on Spring 5, see you there. BYE.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.