Continuous Query With Spring Data Reactive MongoDB

Continuous Query With Spring Data Reactive MongoDB

On this blog post, we will take a look how to implement Continuous Queries in MongoDB. Also, we will have a dash of Spring WebFlux in Action.

Let’s do it, right now!!!

What is Continuous Query???

Is a kind of active query, when the data were arriving in the database, if this piece of data matches accordingly with our query an event will be emitted to our application, with the piece of data. We can think a kind of Event-Driven Programming using the database as an event trigger.

It is a powerful feature and enable us to add interactive behaviors in our application.

Capped Collections and Tailable Cursors

In MongoDB, there is a feature called Capped Collections. This kind of collection has a fixed size and support high-throughput operations and retrieve the documents based on insert order. We will use this kind of collection to store our data to simulate the “continuous query.”

Also, MongoDB has an exciting feature called Tailable Cursors which we are able to use on Capped Collections. This cursor is similar to

in Unix command system, which means we will retrieve the documents in natural order.

Spring Data Reactive MongoDB

Now we know how Capped Collections and Tailable Cursor works. Then is time to use Spring Data Reactive Mongo DB stuff.

Use Case

We will simulate a kind of data collector for IoT components. The main feature of our project is collect components data and provides an API to show max and min temperatures when the temperature reaches the limits configured for a specific device. Then the Frontend Team can build a fantastic reactive page to display the device information.

Our API will have five principal operations:

  • Create Device
  • Input Temperature
  • Device Temperature Stream
  • Device Min Temperature Stream
  • Device Max Temperature Stream

Let’s start with our primary Entity Temperature, the temperature class should be:

As we can see there is nothing special, we have used the Lombok Annotations to remove boilerplate code.

We have the Temperature class created, then we can create our TemperatureRepository, the interface can be declared like this:

Awesome, there is some interesting stuff here. The first one is the @Tailable annotation, which instructs the Spring Frameworks to await the MongoDB Tailable Cursors. When the data arrives in MongoDb with de data matches with a specific query one event will be emitted. In this case, we will watch events from a specific device. Take a look at our return type we always should use the Flux<T> to make application reactive.

Time to create our service class, the implementation should be like this:

On our method init(), we have declared a capped collection. First, we have removed the collection created and then reactively we have created and configured the temperatures collection. Remember this method will call one time. The others methods are so simple the main idea here is to call the Repository Layer to retrieve the documents. The important part here is the return types of methods, always Flux<T> when we expect the events.

In our RestController we will use the Server-Sent Event (SSE), to add more interactive behaviors for our API. The main idea here is to push data to the client using the Half-Duplex HTTP connection.

Our DeviceResource should be like this:

As we can see take a look at the methods declarations we have used the MediaType.APPLICATION_STREAM_JSON_VALUE. It is much important to add Stream Behavior for our application.

Simple like that we have created a couple of classes and lines, to add a powerful feature like “continuous query” in our application. Spring Data Reactive reactive brings reactive characteristics for our application and make them more resilient and cost-effective.

The Full source code can be found at GitHub. There is a client to add some data and make the test easier, the client will send data to interval pre-configured.

The Spring 5.0 By Example Book

Recently I launched the Spring 5.0 By Example Book. This book contains a lot of Spring Concepts and Examples, using the Microservices Architecture and much codes using the Spring Boot 2.

The key features of the book are:

  • Learn reactive programming by implementing a reactive application with Spring Webflux
  • Create a robust and scalable messaging application with Spring messaging support
  • Apply your knowledge to build three real-world projects in Spring

You can find the book on Amazon.

Requirements:

MongoDb instance up and running. I recommend Docker to spin up a MongoDB container.

The MongoDb should be listening at “localhost”, otherwise you can configure using the application.yaml or application.properties.

References:

https://docs.mongodb.com/manual/core/capped-collections/

https://docs.mongodb.com/manual/core/tailable-cursors/

https://en.wikipedia.org/wiki/Server-sent_events

 

See you. BYE.

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.