📜 ⬆️ ⬇️

Reactive programming with Spring Boot 2. Part 1


Not so long ago a new version of the most popular Java framework was released: Spring Framework 5 . The new version has brought a lot of new. One of the biggest innovations is the reactive programming model. Spring Boot 2 will be released very soon, which will significantly simplify the creation of microservices with this approach.

If you, like me, want to find out in more detail what it is and how it is used, then welcome under cat. The article is divided into two parts - theoretical and practical. Now we will try to figure out what it means to be reactive. After that, we will try to use this knowledge to write our own microservice ( part 2 ).

What is reactivity?


To begin, consider the concept of reactivity. And here it is necessary to make a clear distinction in the definitions immediately.

Reactive system


A reactive system is an architectural pattern that satisfies a certain set of rules ( reactive manifesto ). This monifest was developed in 2013 to eliminate uncertainty. The fact is that at that time in Europe and the United States the term “reactive” was too excessive. Everyone understood in his own way what system could be called reactive. This gave rise to enormous confusion, and eventually a manifesto was created that establishes clear criteria for the reactive system.
')
Let's look at the picture from the manifest and analyze in more detail what each item means:

image


Reactive programming


According to Wikipedia, reactive programming is a programming paradigm focused on data streams. Very soon we will look at how this works in practice. But first, let's see what this paradigm is based on.

The basic concept of reactive programming is based on non-blocking input / output. Usually when accessing a certain resource (database, file on disk, remote server, etc.) we get the result immediately (often in the same line). In case of non-blocking access to a resource, our thread does not stop accessing and continues execution. The result we get later and if necessary.

Practice


Fine! Now we will start implementation of reactive programming in Java . The only thing to note is that we will use Spring WebFlux . This is a new framework for reactive programming. The question arises, why did the Spring team not use Spring Web MVC for this purpose? The fact is that not all modules in this framework can be used for working in reactive mode. There are a lot of code and third-party libraries, for example, Tomcat , which are based on declarative programming and threads.

In the process of working on the framework, a small specification was developed for asynchronous work. Later this specification was decided to be included in Java 9. However, I will use Java 8 and Spring Boot 2 for simplicity.

Basic concepts


In the new approach, we have two main classes for working in jet mode:


All this is very simple and efficient in terms of resources. Imagine what can be gained by combining calls to stream methods.

In this article, we examined the concept of the reactive system and reactive programming . In addition, we understand how these concepts are connected. In the next part we will go further and try to build our service based on the knowledge gained.

PS I propose to disassemble the messaging system from mail.ru. Do you think this application can be called a message system according to the manifest? Write your thoughts in the comments. Very interesting.

Source: https://habr.com/ru/post/342498/


All Articles