Flux - How it works ?

Pragmatic Code School
A free video tutorial from Pragmatic Code School
Technology Enthusiast, Online Instructor
4.5 instructor rating • 7 courses • 35,580 students

Lecture description

In this video we will learn about how Flux/Mono works at its core and demystifying the internals of it.

Learn more from the full course

Build Reactive RESTFUL APIs using Spring Boot/WebFlux

Learn to write reactive programming in Spring using WebFlux/Reactor and build Reactive RESTFUL APIs.

09:38:13 of on-demand video • Updated February 2021

  • What problems Reactive Programming is trying to solve ?
  • What is Reactive Programming?
  • Reactive Programming using Project Reactor
  • Learn to Write Reactive programming code with DB
  • Learn to Write Reactive Programming with Spring
  • Build a Reactive API from Scratch
  • Learn to build Non-Blocking clients using WebClient
  • Write end to end Automated test cases using JUNIT for the Reactive API
English [Auto] Hi everyone welcome back to the tutorial in the city real. Will explore a boat flux and model and its internals via. My approach to master the reactor types flux and model is by writing the test cases and learning from it. The first thing is we are going to expand this test folder and then expand the Java package and then the com dot learn react to spring package. What I'm going to do I'm going to create a package called Flux and model playground and then click OK. And we are going to create a test case the test case name is going to be flux and model test meaning changes to uppercase and then click Okay. So now I'm going to zoom in a little bit. The next thing is we are going to create a dot test method. That's the next thing. So I'm gonna create a test method called public wide flux test. Let's change this into law guess so we have the test case ready. The next step is we are going to create the flux. So in order test the flux we need the flux right. Let's go ahead and create it. Flux has a handy method called just which takes in the parameters. So what I'm going to do I'm going to pass three strings in order to keep this example thing simple. I'm gonna create a flux of three elements. Spring Spring Boot and reactive spring. So this is a flux of three elements. In reality this might be the flux which is flowing from the database layer for now to keep things simple. We are basically building this flux. I'm gonna give it a name for this flux. So there is a flux of type String and then the name is going to be string flux. So there is a flux semesters. There is a flux of type String. Now we have the flux right. The only way to access the elements from the flux is by subscribing to it without subscribing. There is no point of having a flux and it is meaningless. Now the next step is we are going to use the reference of the Lux which is string flux dot subscript. So when you subscribe that's when you are actively attaching a subscriber which is going to read all the values from the flux when you subscribe. That's when the flux is going to start emitting the values to the subscriber the subscriber has many versions offered many overloaded versions of it. The first thing is we are gonna just get the value and print the value in the console. So what I'm going to do I'm going to use sys dot out colon colon print. So I'm just reading the values from the flux and then printing it. Let's go ahead and run this one so to run the test case in Italy. You have to click this icon and then it gives you the option called 1 flux trust. You have to click on that option. When you do that. It's going to print the values one by one. So as soon as you subscribe why this flux does as it's going to parse the elements to the subscribe which is the subscriber of the flux one by one. So it guard the values one by one and then printed the values in the console. Now the next thing is let's see if there is some kind of error happened in that case. How do we know. So in that case subscribe as a altered version of handling dark so it takes a second parameter to the sub's cremated and then it takes an exception and it can handle the exception in whatever way you want. So in the imperative programming what do we do. We handle the exceptions using the try catch. So in here as part of the subscript method itself you can posit exception and that exception will be handled to keep things simple I'm just gonna print the exception here system dot era dot Ventolin and then the there you go. No I want to attach error to the flux. How do we do it. So Flux has a handy method called can. I'm going to use that congratulate and I'm gonna add that exception. So you have to do flux dot Ed if you do this. Basically you are throwing an exception and using the contact with we are touching that exception to the flux basically to the original flux which is the string flux in here. We will not have the exception as new runtime exception and then parse the error message Error Message us exception. Right now let's add on this example the recall so we got that exception. Also US and even if you remember from the previous slides the data is going to flow to the subscriber as evens. As soon as you call the subscript method subscribe even to send to the flux and then from there it is going to send the Evens using the on extra method and parse element to it. And then when there is an exception it's going to send you there on every word. With the exception it's all in theory right. How do we know is it really happening in order to make sure that we are seeing all those events. What I'm going to do is I'm going to add a log flux as a handy method called Dot log this log is going to log all the evens that happens behind the scenes when you call the subscript center. Let's go out and run this one out on this as soon as you land. Now if you take a look at the library's huge right let's go and explore what's happening there. Let me see whether I can zoom in. There you go. I'm able to zoom in the first steps on Subscribe. That's what this call is and followed by that there is a on request call meaning the request call but unbounded basically the subscribe request for give me whatever data you have the unbounded relates to long dot max value. So that's a value it is requesting the flux to emit as soon as it receives the flux receives the request unbounded call. It's going to send the element one by one. That's why you see that on next on NEXT. And then it pauses element and then we are printing it here. Right. And after that it's gone up by the springboard and not further it's going to pass the on next since we have an error here. That's the reason why it ended it on error in your usual cases. If there is no error then we are going to receive the on complete even know what we will do. We will go ahead. I'm going to come on this and then let's run this example again. So instead of on error we will be having the on complete event and then we are lagging this exception here like our landlord or runtime exception with this exception occurred. So I'm going to just add some more logger here. Acceptance. This is to make sure are exception blog captured dead exception. On this again say if you take a look at it there is no exception because I have commented out this line. That's the reason why you just see the on complete. So this is how the internal working of flux and the subscription works. So as soon as you subscribe it's going to make a subscript call and then the request call to subscribe called goes to the flux and the reckless Call goes to the flux. So basically we receive a subscription but it is not shown in the logger but I'll explain that in a bit. The request call is going to cause for all the data. That's when you receive all the even. Right as on using the next method from the flux and the flux is going to call called on next method in the subscriber and then it's going to parse elements one by one and then on complete. Now I'm gonna come on this one and this again there you go. We have the exception and the exception this exception occurred. Right now let's go ahead and check one more scenario. I'm going to add one more element call contact but this is going to be after the honorary event is just. The use case that I'm going to show here is that after the error does the flux still emits element to the subscriber or not. So let's use flux. Just want to add after error as a string. Let's go and run this and then check. So I'm expecting I'm not expecting the en next event for the operator. Let's go out and run this did we receive that after error. No. The reason being once an error is omitted from flux. That said it is not going to send you any more data after that. So that is something which we have to keep in our mind. So we added this right. Let's check. Do we have this value printed on the console. Copy this and then search it here. There you go. So whenever we have an exception it goes to this block and then it has the message that we added. And then the actual exception itself the subscript method takes in another parameter that parameter is just to have the uncompleted one whenever there is an uncompleted one that gets notified. What I'm going to do I want to use a lambda. And then let's print some message here completed. So let's go and run this one let's search for this one. Do we have competed in the console. No we do not because our flux ended with an error. I'm gonna come on this line. There you go. And then I'm going out on this. There you go. We got this completed message printed on the console. Basically this block gets executed whenever there is a complete even from the flux. So this is to make sure whenever you want to do some kind of activity after the flux dominated meaning the fluxes Flux has no more data to emit than in that case you can use this third parameter as part of the subscript method. So you might be wondering we explored all the internals and how the data flows from the flux to the subscriber right now. We didn't write any. This case is actually test case as basically is. We didn't assert anything on it. Right. So reactor test has a module dedicated for testing these fluxes. So testing the flux is totally different from the way we have been coding so far in Java so I'll be showing you the techniques on how to test a flux in the next material. But this we came to the end of this tutorial. Thank you for watching.