In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to understand Mono and Flux in responsive programming". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Preface
Many students reflect that they are a little confused about the concepts of Flux and Mono in responsive programming. But at present, we have the most contact with these two objects in Java responsive programming, such as Spring WebFlux, RSocket, R2DBC.
two。 Characteristics of response flow
To understand these two concepts, you must talk about the response flow specification. It is the cornerstone of responsive programming. He has the following characteristics:
The response flow must be nonblocking.
The response flow must be a data stream.
It must be executable asynchronously.
And it should also be able to handle back pressure.
Back pressure is an important concept in the reaction flow, which can be understood as that producers can feel the consumption pressure of consumer feedback and dynamically adjust the production rate according to the pressure. The image point can be understood as follows:
Are there two cases of back pressure?
3. Publisher
Because of the nature of the response stream, we can no longer return a simple POJO object to represent the result. You must return a concept similar to Future in Java, notifying consumers of consumption responses when results are available.
In the Reactive Stream specification, this is defined as Publisher,Publisher is a provider that can provide 0 M N sequence elements and push elements according to the requirements of its subscriber Subscriber. A Publisher can support multiple subscribers and can push sequence elements according to the subscriber's logic. The following Excel calculation illustrates some of the characteristics of Publisher.
A1-A9 can be thought of as Publisher and the sequence of elements it provides. A10-A13 is the summation function SUM (A1:A9), the average function AVERAGE (A1:A9), the maximum function MAX (A1:A9), and the minimum function MIN (A1:A9), which can be regarded as the subscriber Subscriber. If we don't have A10-A13, then A1-A9 has no practical meaning, and they don't produce calculations. This is also an important feature of responsiveness: publishers do nothing when there is no subscription.
Both Flux and Mono are implemented in Reactor 3 by Publisher. Publisher provides a subscribe method that allows consumers to spend when results are available. If there is no consumer Publisher will not do anything, he will respond according to consumption. Publisher may return zero or more, or even infinite, and two implementation models, Mono and Flux, have been introduced to express the expected results more clearly.
4. Flux
Flux is a Publisher that emits an asynchronous sequence of (emit) 0murn elements, which can be terminated by an onComplete signal or an onError signal. In the response flow specification, there are three methods called by downstream consumers: onNext, onComplete, and onError. The following figure represents the abstract model of Flux:
Flux
The above explanation is still difficult to understand for the first contact with reactive programming, so there is a step-by-step understanding process.
Some analogies are not very appropriate, but it is helpful for you to understand these new concepts step by step.
Traditional data processing
This is what we usually write:
Public List allUsers () {return Arrays.asList (new ClientUser ("felord.cn", "reactive"), new ClientUser ("Felordcn", "Reactor");}
We get these elements for reprocessing (consumption) by iterating the return value List, which is a bit like the way the chef cooks a lot of dishes, and whether to eat or not depends on the diner. Diners need to take the initiative to eat (pull way), as for what they like to eat, what they do not like to eat, how to eat is also free to eat.
Streaming data processing
In Java 8, we can rewrite the representation of the stream:
Public Stream allUsers () {return Stream.of (new ClientUser ("felord.cn", "reactive"), new ClientUser ("Felordcn", "Reactor");}
It is still the chef who cooks a lot of dishes, but this is more advanced, providing a way to match the dishes (without specific details). Diners can match them according to their own habits according to the instructions, but they will not be returned or exchanged at the beginning, and they will not wait until they are finished.
Reactive data processing
In Reactor, we can rewrite it as Flux:
Public Flux allUsers () {return Flux.just (new ClientUser ("felord.cn", "reactive"), new ClientUser ("Felordcn", "Reactor");}
At this time, diners only need to order meals, which will be presented naturally when they are done, and can be adjusted at any time according to the amount of food they eat. If there are no diners to order, then the cook doesn't have to do anything. Of course, there are more than these features, but this is enough for us to understand.
5. Mono
A Mono is a Publisher that emits (emit) 0-1 elements and can be terminated by an onComplete signal or an onError signal.
Mono
There is no translation here, the whole is similar to Flux, but only 0-1 elements will be emitted here. Which means either there is or there is no. Like Flux, let's look at the evolution of Mono to help understand.
Traditional data processing
Public ClientUser currentUser () {return isAuthenticated? New ClientUser ("felord.cn", "reactive"): null;}
Directly return an object or null that meets the criteria.
The way Optional is handled
Public Optional currentUser () {return isAuthenticated? Optional.of (new ClientUser ("felord.cn", "reactive"): Optional.empty ();}
I think this Optional has the smell of reaction, of course, it is not reaction. When we do not take the specific object from the return value Optional, we do not know whether there is any in it or not, but Optional must exist objectively and there will be no NPE problems.
Reactive data processing
Public Mono currentUser () {return isAuthenticated? Mono.just (new ClientUser ("felord.cn", "reactive"): Mono.empty ();}
The mechanism is somewhat similar to Optional. Of course, Mono is not designed to solve the NPE problem, but exists to deal with a single value (or possibly Void) in the response flow.
That's all for "how to understand Mono and Flux in responsive programming". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.