Spring Boot WebFlux | Project setup & understanding Mono & Flux Internal Workflow | JavaTechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] [Music] [Applause] [Music] hi everyone welcome to java techy in this tutorial we'll be exploring about mono and plugs with its internal workflow okay also we'll verify the steps which we discuss in reactive stream workflow using this mono and phlox basically we'll be creating one small project to demonstrate these flux and mono and its internal flow so let's quickly create one spring boot reactive project go to your favorite id i will be using intellij id then click on file click new click on project click spring assistant you should have this plugin in your id now click on next then change the group id here so i'll add com.javateki then change the artifact id i'll name it springboot webflux demo then change your java version i will use it let me copy the artifact id so that i will specify same as my project name then change the package here com.javateki dot webflox now click on next now you need to add the reactive dependency so let me type here spring wave if you observe there is two spring wave dependency one is spring wave and another is spring reactive wave okay since we are doing the reactive programming i need to choose this spring reactive now click on next finish then go inside your project now if you will open your palm.xml you could see the latest version of springboot we are using 2.4.4 and we added only the dependency spring boot starter wavefox okay and this is the default dependency for test and there is a dependence called reactor test and this is what the maven plugin right so only one dependency which you added that is the springboot starter webflox now i'm not going to write any api here i'll just write one test case to show you the workflow of mono and flux now go inside your src test then i'll create one class i'll name it mono flux test so we'll be begin with mono so what i'll do i'll write one method public void then test mono okay so there is multiple way you can create object of mono so what i will do i will just type mono dot just this is how you can create object of this mono now as we already understood mono can only handle one element so i will add here any kind of data type i will add abc if i want i can type one it's up to you which kind of data type we want to use okay so for this example i will use something called string javateki now the return type will be mono of string okay i'll name it mono string fine this is how you can create object of mono who can handle only one object fine now if you will go inside your mono class so let me go to this implementation if you observe the packet structure reactor core publisher it means this mono and flux will act as a publisher right so to access any publisher first subscriber need to call subscribe method of publisher so here my publisher is mono string now subscriber need to call mono string is my publisher dot subscribe right so if you remember this is what the workflow we understand right first subscriber need to call subscribe method of publisher that's what we are doing here this is my publisher and we are invoking the subscribe method then if you observe the subscribe method is one overloaded method there is a multiple subscribe method okay you can observe here so what i want to do here i just want to print the published event so i will directly use method reference system dot out println fine now i just annotate this with other tests let's run this piece of code run this monoflox test yeah so we are seeing the event here right once we call publisher.subscribe method then immediately publisher start emitting the event that's how we can see the result now what i need to verify i just want to verify whether this mono and flux is really following the reactive stream workflow or not so for that reason i will add one method called log now each of execution will be printed in console now let's rerun this piece of code yeah so if you observe the output there is a unsubscribe method call then there is a request method call from the subscription interface then on next method call from the subscriber interface then on complete event right so let's go to the diagram once again the first step we just need to subscribe to the publisher by calling this subscribe method then publisher need to send the subscription event to subscriber then if you observe the third request third need to be the request method from the subscription right so subscriber is invoking this request n method to inform to the publisher give me n number of data that's how the method argument is n and then the fourth step will be on next right and if there is n number of event we will get n number of on next method call as you are trying with the mono i cannot show you this n number of event once you will try with the flux i can show you this output then attend you can observe we are receiving the uncomplete event now if you go back to the console the first method is unsubscribe then there is a request method from the subscription interface with n number of argument then once we once publisher called this on next method we are receiving the event then as everything is okay there is no error we are getting on complete event now if you go to the diagram if there is any failure we should receive on error event right now let's verify the this on error event so what i will forcefully try to throw some exception from this mono so let me minimize this then what we'll do we'll just add here then i just want to mono dot error new something called runtime exception just pass the message exception or code something like that okay just change this to type generic fine because this will return the object and we are mapping here the string so now forcefully we are throwing some error while processing the mono now let's verify whether it return on error event or not let me run this piece of code we're getting unsubscribe then request then on error there is no on complete event if you observe there is no request on next event as well okay now we are not handling this exception so what we can do as this subscribe is overloaded method i will try to print the exception sees out i'll just print this message now let's rerun this it called unsubscribe request on error there is no on next call and on complete call okay so the error handling scenario is something different in reactive programming so we'll cover a separate session for error handling this is just i was trying to show you the flow of on complete and on error okay now we will just verify if publisher will publishing n number of event whether we are getting any number of unnext call or not so what we can do i will go with the flocks so i'll write one another test case public void test blocks then i just need to create the flux object flux dot create i will pass your n number of elements or i can just name it to flux dot just okay so here i will give something called a spring spring boot then i'll add hibernate some random value okay so i'll add another string let's say microservice fine so define the local variable flock string fine now this flux string will be act as my publisher so to consume this what i need to do i just need to call the subscribe method of my publisher so just call it once you call this subscribe method on flux it will immediately start emitting the event it will it will emit the four event okay so what i'll do i will just print it so i'll copy this directly fine now let me run this piece of code so we need to annotate at the right test let's run this we can see the result right spring spring boot hibernate and microservices each of the message or each of the data published as a separate or next call so to verify that what we can do we'll just add one log method now let's run this code not enter test case because on the first one we are getting the exception right i'll run this yeah so if you observe the first call is unsubscribe then request n number of object from the subscription interface then if you observe on the first on next call we are receiving one event on the second or next call we are receiving another event like we have four so here four on next event right under 10 we are getting on complete because there is no error now in middle if i want to add something let's say i'll just add dot concur with value i'll add something called aws now my publisher will fire five event so there should be five on next call now let me run this yeah so if you observe there is pipe on next call now let's verify the error scenario so what i want to do i just want to forcefully send some error okay so i can write concurrent with flux dot error i'll return the same exception object so i will add the message exception occurred in flux fine now if i will run this we are not handling the exception so let me add this we are not handling we are just printing the message so this will be second argument of subscriber now let's run this so if you observe here there is unsubscribe call request call then 5 on next event then at finally we are receiving on error event right as there is error we are getting on error if there is everything succeed we should receive on complete event now what we just need to verify after this error if i will add some value to this existing flux whether it will do the next call or not so what i will do i will add something called here cloud so many random string now let me run this so if you observe this output we don't have this cloud on next event right still you can see only five on next and at end we are getting an error so this is how it follow the reactive stream workflow once again if you see this diagram the first we need to subscribe then publisher need to send the subscription event then there will be a request n call so let me remove this yeah so there will be request n method call from the subscriber from the subscription interface then there will be on next event will be published from the publisher to the subscriber and the number of record you have you will find that many and on next event then at end you will find on complete or on error event this is what we understand with our code right so i believe this concept is clear for you in our upcoming tutorial we will understand how this asynchronous and non-blocking works in reactive programming with live example okay so once again don't ignore this theory session just try to brush up once again so that it will help you to understand the next tutorial that's all about this particular video guys thanks for watching this video meet you soon with a new concept
Info
Channel: Java Techie
Views: 110,357
Rating: undefined out of 5
Keywords: spring reactive programming, spring reactive, javatechie, Mono, Flux, spring boot, spring webflux
Id: ckfqcfzCg3w
Channel Id: undefined
Length: 15min 26sec (926 seconds)
Published: Thu Apr 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.