Spring Reactive Full Course | Spring Boot WebFlux | Project Reactor | Reactive MongoDB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this tutorial I'll be talking about reactive programming in Java we will be making use of the springboard framework and start with the absolute basic ideas of reactive programming then gradually we will move on to more complex topic and finish the reactive programming tutorial so let's get started when we talk about reactive programming the first thing we need to clarify is why do we need it why do we need reactive programming when we have the non-reactive traditional programming out there so as you can see here these are the major advantages of using reactive programming we will be talking about each of these points in detail across this tutorial first we will look into the asynchronous and non-blocking advantage of this uh reactive programming approach approach here you can see two images the image on the left side represent the synchronous programming workflow and the image on the right hand side shows the asynchronous workflow I will explain that so here you can see two processes process a and process B whenever process a needs some data from process B it make a request here so this Arrow Mark is requesting for data and then Will Wait so the major difference between synchronous and asynchronous is in this section where there is a weight and there is a continue working method so in the synchronous process a will request for the data and it will wait until the process be finish the data and return it so let us consider the example of a database let's say our program requir some data from the database so uh it will make a request to the database in the form of a database query then it will wait uh in the usual synchronous approach it will wait until the datab base finishes the data fetching and return the data when it comes to asynchronous programming this is entirely different whenever something needs something El something from another process it will make a request then it will continue working on its other task it won't wait for the data so the advantage of advantage of not uh having to wait for the data is that it can continue on its own work so the process a is doing uh useful work when it is waiting for the data from process B again when we consider the example of database fetching we make in the asynchronous programming method we will make the request to database and then the process will continue its own other task once the data is ready from the database side the database will make a call back and the data will be returned to the process a so if you haven't understood this concept yet that is totally fine it will become very clear as we move forward okay so let me show you another image that will that should help you understand it a little bit more so here again in the left hand side we have the synchronous method and in the right hand side we have the asynchronous method uh this is uh this is for showing a working of a web server so it will make a request then it will prepare a response and then send the response so as you can see the flow is like this uh in the synchronous programming it it received a request it prepared the response and then it finished the request response cycle when it come to the asynchronous programming mode it received a request and immediately returned its operation I mean the response is not yet ready when the response is available it is returned at a later time without waiting for it in order to make it even more clear let me show you one more diagram of comparison between synchronous and asynchronous so again here in the left hand side you can see the synchronous operation so this is like the synchronous and asynchronous operation differences in the spring boot so in the presentation layer uh this is like a web server so it receives a request and it will assign a thread a thread to process the request so 90% of the time whenever a web server receives a request it has to do some operation through the database for example in our server if we are storing the user information and we want to get the user information let's say the request one is to get the user information so a Ser thread will be assigned to it then it will go into the database and request for the user data and while the database is making the internal queries and coming up with the data it waits so that the thread will wait until the data is ready in synchronous programming and once the data is ready it will return as a response to the request and then it will go on to processing another request so the main disadvantage of this synchronous approach is it is a blocking operation it has to block until the data is available right so with a single programming as you can see here whenever a request comes it is given into a thread pool I mean a series of threads that is processing the request that is called a thread pool so whenever a request comes it is given to a pool and that pool will make a request to the database then it won't wait for the database to complete its operation instead it will be ready for processing the next request and next request and next request so on so once the database finishes its work and the data is ready for a request it is notified to the uh even Loop the fixer number of thread via a call back method so that's why you can see here there is a event with call back and call back execution so it is like this so let us consider an example uh I want some data and I went to the database in the synchronous approach I ask the database and I wait there until the database G gives me the data but in the as synchr method I go to the database I request for data and I immediately come back and do other work and when the database finishes the cor finishes my request it will notify me yeah the request you made couple of uh seconds ago I am ready with the data here it is and it will give it to me and I can return that to whoever made the request so that is how a synchronous Behavior or as synchronous programming works so uh as you can as you might have understood now the asynchronous and non-blocking goes hand to hand so it is not blocking it just make a request and return and whenever the data is available it will work on top of that so that is the main uh idea of reactive programming the asynchronous behavior regarding the other advantages of reactive methods we will go into that once we get familiarized with the reactive Pro programming the thing is like if I go and explain this resource efficiency back pressure streaming of data right now itself it might be very confusing for you because you don't know what is reactive programming and how it works so what we will do is we will create a sample project we will talk about how this asynchronous and nonblocking behavior is implemented how to program in reactive environment and then we will come back and talk about the efficiency and streaming of data and back pressure technique Etc so uh before starting the coding I know to show you one more important things regarding the reactive programming approach it is the workflow so uh in a reactive programming workflow there are two main actors one is a publisher and one is a subscriber so you can see there is a publisher and subscriber so the publisher is the one who makes the data like it gener Ates the data the publisher will generate the data it is always generating the data and the subscriber is the one who is interested in the data created by the publisher and work on it so uh we I mean just after this slide we will be stting starting coding and you will be seeing this in action very soon so it will become all clear but just bear with me for the moment and uh let us just understand the technical terms here so in the publisher and subscriber method this is how it works when the subscriber wants some data it will go into the publisher and make a subscription so that is the very first step of reactive programming the subscriber subscribe to the publisher okay then once the publisher receives a subscription request from the subscriber it will send a subscription to the subscriber so it is like that so it made a request and the publisher acknowledged that yeah I know that you need the data here is my subscription and and after that when the subscriber needs some data for processing it make a request in the bracket here you can see n that is like how many data I am requesting from the publisher let's say I want 10 data from the publisher then the subscriber subscriber will make a request of 10 entries and the next step on next so when when the publisher receives a request for end data entries it will then immediately uh or as the publisher have the data get the data it will just publish it to the subscriber through the on next command so if there were if the request was for 10 entries then there will be like on 10 on next responses from the publisher then once the publisher gives all the necessary data to the subscriber the publisher will let the subscriber know that everything is complete I am done uh uh uh the subscription is complete your request is complete and if there were any error if some error errors happened then the on error will be called and the subscriber can do other things like retrying or trying some something else when an error happens now let us create a new project and start working on reactive programming so we will be implementing the implementing this concept we have just talked about so let's start it let's let us create a new project with a reactive module support so I'll be using intellig idea the good thing about intellig idea is that it has a built-in generator for spring boot projects so here as you can see there is a spring initializer and here I can fill in the details and generate a spring boot project right away so that makes things a little bit more easier so here let me name the project as learn reactive programming and the group ID uh com. genuine coder that is fine for Java we will go with the LTS release Java 17 and uh let us click on next and here we need to choose the dependency and the spring boot version so current most recent version is 3.2.2 as I am create as of creating this tutorial and uh for the dependency we need to add a special reactive web dependency for reactives application so here you can see spring reactive web this is the module we need usually when we create a new spring board project we go with the spring web so that is the non-reactive version and for reactive programming we have the spring reactive web so that is the only dependency that we need at the moment as we create as we are creating the project so I'll just choose that and click on create now uh it will take a little bit of time until the gradel build is complete and the project is ready so in the meantime let me go into the build. Gradle and check here you can see that this is the uh special dependency we have added springboard starter webflex so for reactive programming we use web spring webflex framework so that is why springboard starter webflex is added our project is ready uh here we have the learn reactive programming application uh let us just start the application and make sure that it is starting uh just just for a testing so here it is uh it started the uh server on 8080 Port we haven't done anything so even if we load the browser nothing will happen okay so let me close the application and create a new class so here I will create a new class and let us name it as reactive tutorial so in this class we will be familiarizing with the reactive programming project reactor module so for doing the reactive programming spring boot make use of a library called project reactor I will show you that so if we go into the browser and search for Project reactor you can see a a project called project reactor the reactor library is automatically added when we add the spring boot starter web Flex so the springboard web Flex live dependency make use of the project reactor to provide the reactive programming interface so let us go ahead and write our first function using reactive programming so here I have a reactive tutorial class and I will create a main class uh psvm then uh just create an instant reactive tutorial new reactive tutorial in reactive programming there are basically two important classes the first one is mono and the second one is flux so first let us familiarize with the mono class let me write a function here so write private mono then I will uh I can this is a generic class so I can provide any kind of type any type or any class here so here I will provide mono string so that means it will provide one string as as the return type so uh let us write the name as test pH so let me show you so uh usually usually to return a string what do we do we return uh let's say I want to return Java then usually this is how we do it we uh Pro provide the return keyword and provide the value when in reactive programming or when it comes to reactive programming you have to wrap it inside mono so mono do J so mono doj Java will return the Java string as an asynchronous object here so mono string test mono in order to use this let me show you the publisher subscription uh image we have discussed so here this is the publisher so we just created the publisher as mono so there are two publisher classes mainly the first one is mono and the second one one is uh Plex and this is the first one so we created the publisher and we said that the data uh I mean the work it will only work when the subscriber make a subscription and make a request and so on and so forth so let us just confirm that so if I just call reactive tutorial. test monono actually nothing will happen that is because the subscription is not yet made when I run this program it exited immediately with exit Code Zero and here you can see a warning message from the indell and it is say that value is never used as a publisher so the publisher is making data but there is no subscriber so without subscription it will not work so in order to subscribe in order to do the part the subscribers part what we have to do is to call subscribe method so when we do the subscription this data will be used if we run the program right now you won't see anything much here internally it is taking this data but we are not doing anything with the subscriber data like uh maybe uh printing it in the into the console or anything so inside the Subscribe function we can use the return data from here and uh displayed so here I can add it like subscribe the data so uh each uh so the data inside the mono will come here so the data will be Java in this case and I can just print it so system. out. print and data now when I run this program again it should display me Java so here it is the Java so that is coming through the subscription now the reactive programming or the project reactor provides a very useful function called log so I can add a log function here here and see how the data is going through the subscription processor interfaces so I just added the log log function here and I am running the code now and here you can see what is happening exactly so just like we theorized the subscriber first should send a subscription request so you can see on subscribe so here when we call Tes mono and call the sub CRI function it made a subscription into the publisher which is our mono then it sent a request so the next process after the subscription it will request for a data and then it will return all the data it has so unbounded means send me all your data so here for mono there is only one data and only one on next is called and that is Java and it completed so once all the data is processed it will call on complete so the subscriber will know that yeah the processing is completed so that is how it happened so just like we theorized it worked exactly like that so mono do just like that now let us look into some other interesting features of the mono so if we look into other ways of mono preparation we have mono doist that will accept just or empty so the just will not accept null values if I call Mono do just null and do it like this it will it will actually show an error because mono cannot have null so if your program want to return a null value include mono just or empty so the value can be empty so uh let let me run the program again and here you can see that since the mono do not have any data that means it only has a null nothing really happened but it it didn't crash and another functions for mono is like mono do zip and some other functions so what if you want to just give an empty mono I mean mono do just or empty then passing null is not recommended so if you want to return a empty value use mono do Mt and let us run the program again and here again you can see that there is no data in the mono so nothing really happened but the program worked with the empty data for the mono so mono is for just one data what if we have more than one data in that case we have a publisher called Flex so let us talk about Flex next so I will write a function private flex and here again I can provide any data type so for tutorial I am going going to give string so we have a flux of string and then test flux so let us test a flux so previously for mono we said we have mono doj and we gave Java here and uh it displayed the Java uh in the main function similarly to create a flux we have multiple ways the first and easy way is flex. just so it will accept just one data or more than one data so so here as an example let me give Java uh CPP rust maybe Dart as well so I gave four entries and this is a flux publisher so of course uh in order to use that value we have to subscribe it so we will use this test flx here inside the instead of the mono so reactive tutorial. test Flex then we will subscribe and we'll print that into the console so here there is a warning and this warning is a suggestion for converting into a Lambda based method so uh this can be even more easy to easy to read so I will just put it like this replace Lambda with a reference method so it is if you're are not familiar with Lambda this is exactly what this is so here there is only one argument that is data so and that argument is directly passed to the function so in the Lambda method you can put it like this okay so uh I have linked the test Flex into this one so we have subscribed for it here let us run the app so when we run it you can see here that these four entries Java CPP press dot they they they are now printed here from the flex and when we look into the flex there are more than more other ways you can uh create Flex so uh just is just one method you can create flux from arrays normal Java arrays from iterables from streams and you can create custom Publishers as well so uh in order to test some other method let me go with from iterable so Java Collections are iterable so if I have a list let's say array list or something like that let me create one so here instead of using flux. justest let me me show you uh a list of string so list of elements so this is a list of uh list of string so list of programming languages languages so I have a list of normal Java list and I can convert this list into a flux using flex. from iterable and just return it like this return fleux from iterable and it will work exactly the same way let us test that see Java CPP rest aray and this works for an for array as well so we have from array so if you give an array it will be converted into a flex you can convert a stream Java stream into a flex as well so it works with existing Java Collections and sequences so that is a good thing so that's about the Flex so for single data we have mono for multi- data we have Flex these are the two main publishes now let us talk about the special functions we have that that helps us manipulate data in these flexes and monos so one important thing is called mapping so let let us create a function for map so let's say I have Flex string and let me write the name as map so I I will use this one this iterable uh like this uh list of programming languages then I have a flex here Flex string uh Flex or uh to reduce the number of lines of code let me just use the just function so flex. just like this so I have a flex now let's say I want to make I want to apply an operation to each elements in this flex and return it and that operation can be anything for example let's say I want to make them all capital letters so I can do that by returning Flex do map and here in the function this is each entry so here I I have every entry in the flex and apply any operation so I will just use S do2 root uppercase so when I do the mapping each of of these entries will be applied into the math function and the result will be returned as a flux so let us test that test map test map and let me run it Java CPP R do so it it got applied into each of these elements now similar to map there is another function called flat map so let us look into the flat map let me create another function so F string test flat map and let me copy paste this function this function the test map function here and change this map to flat map and now it doesn't accept it it doesn't accept the uh operation we applied into the map so here if we look into that it is using a function that accept a string and return a publisher so what are the publisher mono is a publisher Flex is a publisher but here what we are returning is a string value so it needs a publisher so the uh a lot of people are usually confused between the map and Flat map so the difference is simple if you are returning a normal value that is not a uh sub publisher then you can use maap function if if you are returning a publisher then you have to you use flat map so this code yes do to root uppercase should become mono dot so that is a publisher now mono dot just s dot to uppercase so now you might be asking why is it mono why is it is not Flex that is because here we are applying the operation to only one element at a time at a time we are only applying this to we are applying to all the elements in the flex but at a time within the flat map we are applying to only one item at a time so that's why it is mono so mono do just s do2 root uppercase so that is how we apply the flat map so now the obvious question is why just why am I complicating the things by using a reactive publisher here instead of just using the normal normal method that thing is this is just an example in real world scenario there will be a lot of cases where your data mapping requires some asynchronous operations let's say uh for each s entry I want to do a database query at that time we won't be able to use the normal operation like a normal synchronous operation so at that time when there is a blocking thing or any other cases like that where you have to use a publisher or reactive way you will have to use the flat map now to confirm the working let let us run the test flat map so test flat map uh it should display all these entries in the uppercase so yeah it is Java CPP rest and Dot so so we have finished covering the map and Flat map topic now let us look into another interesting function called skip so let me create another function let me copy this uh Flex Flex string so test flat map so let me write a function called test skip here I have a flex here and uh uh the skip function allow me to skip certain entities from the flex uh let us look for Skip and you can see there are a lot of overrided function overloaded functions here so we have skip with a long value skip with a duration skip last Etc so let us first use the easiest skip option that is the long skip so let's I have four entries here right like four programming languages if I want to skip the first two entries I can give it like this flux. skip uh that is skipped so and let us run the program so test skip run and you can see rust and dart is there so rust is there dot is there that is the third and fourth one the first and second one is skipped what if I give like maybe a six value that is more than the number of available entries in the flux in that case it will not print anything it will just completes because obviously the fleux do not have that many elements so that is the very basic skip function if we look into another form of the skip function you can see there is a duration so the duration is useful when there is a delay in the flux so let me show you so to show you that first I need to show you uh first I need to make a or emulate a delay in the publisher so uh let me make it simple for you let me just return the flex like this uh return Flex Dot and here I can provide a delay to each element through a delay function so here there's a function called delay elements so duration do off seconds one what this mean is after each element there will be a delay of 1 second I have provided one second here so let us try that if I run this program right now it will be there will be a problem uh nothing is printed that is because the program starts it makes the subscription then there is nothing else to do so uh because this is not a blocking operation so the program just finishes immediately to solve this I need to put a thread dot sleep here because this way I can make the thread wait until every until the whole thing is completed in the uh asynchronous part so let me do that like 10 second here so uh this will take only maximum 4 seconds uh 4 to 5 seconds so 10 second is more than enough uh let me run the program now and now you should be able to see see Java CPP rest Dart after 1 second delay and uh let me make it clear to you uh by providing a log here so do log and let's see what is happening exactly inside those functions so first it made a subscription it waited for 1 second and printed Java then it waited for 1 second and uh called on next then it waited for one second and called on next so let me take the image again so I made a subscription then after 1 second the made a request sorry I made a subscription made a request then after one second the First Data came then after 1 second the next data came and so so on and so forth so uh let me confirm that again let me run the program again made the subscription then the first item came after 1 second then one second like that okay I hope it is clear to you so uh this is just simulating the Behavior Uh simulating the delay using delay elements let uh now let us come back to the skip operation so now I my flux has a delay of 1 second between each items okay so let me make it like this delay elements now if I want to skip elements based on the time period let's say I want to skip data for the first two seconds and give me the rest of the data okay in that case I can do it like Flex dot Skip and there is a duration so duration of seconds uh do I have seconds yeah so let let let me say it like this for the first two seconds I don't I am not interested in the data uh after that give me the data let let us run the program see if that is working so first two seconds nothing is happening then CPP came then rest came and then dot came now you might be asking like you said first SEC uh two seconds uh but why CPP is coming because Java and CPP are supposed to come that is because of minute millisecond differences in the delay elements uh so if I make this 2.0 01 second then it should not be a problem so let me make it like off seconds uh of mes so let me give just about two seconds because this CPP is supposed to come at the after 2 seconds right or just at the 2 second time period so here duration of Mill is two uh so 2 second means 2,000 so let me make it like 2010 so just about 2 seconds I want to skip and then get the data so let me run it now you can see CPP did not come uh because uh it is skipping 2 seconds and 10 millisecond and taking the data so only rest andard G so that's about the skipping with duration so we talked about skipping with count so it was like this Flex do skip just give the count uh skip the first two elements like that then we uh looked into skip with the duration now let us make it more interesting let us consider another cases of uh skip available so again Flex do skip what what else we have so you can see skip last so as the name suggest if I provide two then the last two elements in the flux will be skipped rest standard okay and I have some other things like skip until skip while Etc so that accept a predicate so uh before looking into that let me show you how the skip last work just to confirm that it works as I explained so I am I want to skip the last two entries so Java and CPP should be printed and rest and that should not be printed okay yeah so rest rest and that did not print it the program is not complete because of the 10sec timer I have provided here okay so that is the basic skipping function now look into a little bit more complex function so let me write another function test skip test complex skip so let me tell you how to apply the complex skip operation so uh there is another way to create Flex here so uh so far we have been talking about string of flex entries if you want to create an integer Flex there is an easy way called Flex dot uh range so here I can give a start value so I if I want to create 20 elements uh uh integer elements I can give a starting position here let let me give one as a starting and then how many elements I want in this uh in the flux so I will give 20 so total 20 element will be there in the flux right let me return this flex and show you how it works so flux uh let us write wire this function test complex Skip and let me run the program and you can see it print printed from 1 to 20 all right 1 to 20 is printed so flex. range works good now let us consider the case of skipping so fleux do Skip and we have uh two important functions so skip until and Skip while so let us try with the skip while so in the skip while I can give a predit k i can give a condition so skip while integer less than 10 so that means uh it will go from 1 to 20 right 1 2 3 4 so one one is less than 10 so up to 10 it will skip up to 9 it will Skip and after the this condition gets false it will stop skipping so skip while integer integer less than equal to 10 means until this condition turns false it will continue skipping the entries in the flux so let me run it you can see 10 from 10 it started working because in integer less than 10 is true until 9 1 2 3 4 5 6 7 8 up to 9 it is true so it was skipped the reverse is also possible skip unle so we have the function called Skip unle and uh the skip unle works like this until the condition is met it will skip so we can say like until the integer becomes 10 skip so previously the condition was like until it comes false should it Should Skip and here it is the reverse until the integer satisfy until this condition becomes true skip it so let us try that so uh uh when running the program you can see here that uh it started it skipped until 10 and it stopped skipping once it reaches the true condition which is integer equal to equal to 10 and and I can set this to another always false function right like integer equal to 30 this is not going to happen right because the the mo highest element is 20 so if I run this program this will not have any output that is because this condition never came true so the skipping happened until the end of the list sorry until the end of the flux and if if you look into the documentation you can see that is what they are saying skips values from the flx until a predicate returns true for the value the resulting flux will include the emit include and emit the matching one so until the condition is true it is skipped skipped once the condition accepted or once the condition became true then it started uh sending all those events into the subscriber so that is about the skip so we have covered all the important uh functions in the skip function and the next one is an interesting thing that is called the concatenation so let us look into that so private uh let me just copy this right so test let us use test concat so we discussed that we can generate flux using the range function let's say we have two fluxes and we want to merge these flux into a resultant a single flux so that can be done like this so I will name this as Flex one that has values from 1 to 20 and I have another Flex flex two that starts from 100 and and ends at let let me make it as 101 so uh one to 20 and 101 to 20 Okay then if I want to merge them I can make use of the concat operator so return Flex one. concat sorry I can use flex. conat so that will be Flex one then Flex 2 so what will happen here is this Flex will be sent to the subscriber first 1 to 20 and then the uh once it is completed all the elements from the flux 2 will be concatenated and sent to the uh subscriber so let us try that so test conc test concat run see uh it printed from one to uh 1 to 20 and then from 101 to 120 let's say I want the second flux to come first and the first flux to come as second then I can just swap these values then send it so as you can see it happened like this first the second flex and then the first flex like that happened so the the good thing about flex. conat is it can accept more sources right see it has a v AR function so if I have another third Flex that starts from 10,1 then I can include it as well so Flex 3 Flex 2 Flex one and all those entries will be shown in the end so uh th1 220 then 101 like that so that's about the concat oper so it will help us concat uh multiple fluxes together okay and uh let me check fleux one. conat is that function available no okay okay the the next thing we need to look into uh is the merge function so let's see how we can merge to flexes so let me write it here test merge so uh for easiness let us keep two so we have Flex one and flex two and inside the flex class we have another function called merge flex. merge and here let me merge Flex one and Flex 2 together Flex 2 okay and here the resulting is any uh flex and let me run the program test merge to show you how it looks so here you can see just like that it merged one two three uh sorry the first flex and then the second flux and the interesting thing will happen when we introduce some delay so you might be asking like why why do we need flux you just showed me the concat that worked just like this what is the significance of merge when we have conat that's a very good question that's a pretty good question I will show you why it is important so just like before let me add a delay into this one okay so here I uh the flux one has a delay so I will add a duration so duration of seconds and I will put one so uh each element will take one second uh after the other so there is a one second delay so I have added one second delay for each of these plexes and let me run the program now now you can see the yeah now you can see it is not uh showing any output because just like before we have to add a sleeve function here so thread do sleep so how how many items do we have we have 20 items right so so let us make it faster so off mes so let me make it half a second okay to execute this faster so off mes so there is a half second gap between each entries and for sleep let me put a 12 second sleep then run the program now you can see an interesting Behavior so uh they are printed together so first uh the second flexus element is displayed then one92 is displayed then one2 3 so they are actually merged together right so these elements from these the elements from these two flexes are coming in a mer away if I use conat here the one we just learned before you can see it is first displaying elements from the first flux and only after the first flux is fully completed the second flux is started see 1,1 sorry 101 102 it exited at here because 12 second after 12 second the program exited so let let me make it 30 so that is the difference between conat and merge when there is a delay they behave very differently so if you look into the documentation concatenate all sources provided as V AR forwarding elements emitted by the sources Downstream so basically it will wait until the first flex is complete this is this arrow is the first flex and only after only once it is complete the second Flex will be started whereas in case of merge both flexes are subscribed together there you can see this is the first flex this is the second Flex both are merged into a single Flex so that is a very important thing and one thing I need to tell you guys is that when you do reactive programming these kind of merge conat and map operation these are one of the most important user functions you will come across them all the time when you are doing reactive programming so make sure that you understand the concept test some code on your own and continue okay then we have another thing called merge so we have conat we have merge then we have a thing called siip so let us see how we can make use of siip function so flex. siip is available similar to uh let me show you so here uh just inside the class flux we have the static function called sip but it doesn't provide flux of integers why let us look into the response type so here you can see it is returning a tle value tle two value a tle two value means when you sip them one element will be taken from each flex and then it will be G uh given to the uh resultant Flex so uh in it will be like this uh with the zip function the first element from the first flux will be taken that is one and the second ele sorry the first element from the second flux will be taken that is 101 then the second element from the first flex that is two second element from the second Flex that is one2 so zp is used to zip or combine multiple flexes together to produce a single like a combined output for each one so let me show you this in an output so this should become a tle two of integers integers right so triple two integers of integers we uh Zed it now let us run the code test SI see 1 1 0 1 so the first element uh from the first flux is taken then the first element from the second flux is taken like that now the obvious question is like what if one flux do not have the same elements as that of the other that's a good question so here let let us make this like let us say the first flex has only 10 elements and the second flux has 20 elements and run the code right so this is okay up to 10 it will be okay but after the 10 there will not be any more output that is because as soon as one publisher finishes the whole thing will be ended whole thing will be completed so that is why H that is why it happened here here this flux has only 10 elements so even though this has 20 it will be ended or completed as soon as the first flex is completed right and the test test has some other interesting things like if you ask me like what will happen if I combine three flexes let's say I have a third Flex called Flex 3 right Flex three that starts from 10,1 go for 20 items uh then if I sip it what will be the value so flux three then it shows an error here because the return value doesn't match so in that case it will become a triple three and a triple three has three Val sorry a triple three has three values uh what's the problem here that is fine Tri three right test sa yeah the function is already test complex sa so here you can see uh it became a tle three and a tle three will have three values so let me uh run the test complex zp test complex Z run the function see you can see element three elements and as soon as any of the flux or publisher finishes it will the entire resultant flux will be uh completed right so uh now the question is like what but if there is four there is four fluxes that I want to combine then there is thing called the triple four and it can provide four values there is triple five Triple 8 I believe triple 9 is not available so you can sip up to eight Publishers or uh here in this case eight flexes to create a sied eight Tuple up to eight tole publisher okay I hope it is clear now if you ask me like can I sip a flex and a mono together let's try so let's say I have a mono of integer with mono mono doj right mono do just one just one value for ease of use let us make it as like one flex and One Mono so I have a flex and mono so I provide a flex here I provide a mono here yes here it will become two dle two and as you can see it is possible so the problem here is the mono will be completed after one item so even though this flux has 10 items it will be over just after the just after the first item see one one so that is possible but uh I'm not sure how much useful it is going to be now let us see how to make use of collect function to collect items from Flex into a normal Java list so let me create a new function so test test collection or maybe test collect and uh I only need a Syle Flex uh just one Flex so 110 and here I can collect the flex into a list using collect list so here you can see it is a mono of list of integers so why it is a mono so mono is always for returning one item right uh even though the flex contains multiple items when we combine them into a list that list is going to be a single object right only one item so that's why it is mono of list of integers so flex. collect list and here uh I can I need to change this so this is going to be mono list of integers right mono list of integers and I can just return it like this and let us run the code uh let us test collect and uh uh uh subscribe it and the thread do sleep is not longer needed here because I have removed the duration of mes or the delay between the elements and let me run the prod program so here you can see it got converted into 1 2 3 4 five six just a normal Java list now the question is like I don't want a mono list of integers I want a list of integers how do I do that for that we have a function called block so uh instead of subscribing in order to get it uh synchronously I can call blog here so what will happen here is like a conversion from reactive programming to normal non-reactive programming so when we call the block function the nonblocking uh publisher here this non-blocking publisher will get converted into a blocking synchronous uh code flow so here the output will be list of integers output see list of integers output and I can just print them so the problem here is uh when we run the program right away that is not going to show any issues it will just show 1 2 3 4 5 6 7 8 N9 10 like that but when when there is a delay delay elements like maybe duration of M of mes maybe 1 second delay then the problem is like uh this Flex will take 10 second to complete right as a result this function and this thread will be blocked up uh up to 10 seconds so let me show you that so let me run this and here when we uh we just started the program the output is not coming because for the fleux to complete it will take 10 seconds and until that 10 seconds or till that 10 seconds our program is hanging it is hanging in this blocking call called block so it is not recommended so avoid these calls to block as much as possible now let us look into another function called buffer uh to do so let me create a function test a buffer so buffer as the name suggest is used to collect a a set of items from the publisher and send them together it is just like other kind of buffering so in the YouTube video when the video is buffering it will buffer or download video for couple of seconds and then once that buffer empties it will download the rest of the video for another couple of seconds or minutes similarly the buffer has the same meaning here as well so to test a buffer let us use this Flex and the flex has 10 items starting with one and here uh we can use the buffer like this so flex. buffer and the buffer has a lot of overloaded functions so here uh we have uh a buffer without any items so let us see how it behaves so flex. buffer and you can see that here it provides a list of integers that means it will collect all incoming values into a list that will be then it will be uh emitted as a flex so let me provide it like that so it is going to be a flux so uh collect is like combining everything into a single list whereas buffer is collecting the items together into more than one list okay so let me call the test buffer here test buffer and then subscribe so I have to subscribe here I am not doing any block course so I should I have to uh uh uh subscribe so here let me put cast expression so this return call is no longer valid okay I I have to sleep as well because uh this is a asynchronous call so to make the program alive until this buffering is complete I have to put the Sleep call now let me run the program and it will take 10 second for the whole Flex to uh I mean the publisher to finish it's publishing it will take 10 seconds so all the items are put into the one single buffer here one uh list of integers now we can customize the buffer instead of buffering the whole element into a single list we can provide some customization for the buffer so let us see how we can do that and here you can see there's a max siiz value so if I give a Max size value to maybe three let us see what will happen so I gave the buffer SI S three so here you can see it it is buffering three items at a time so it is waiting until three items are received from the publisher uh and then it is sending that as a list of integers so it created four list of integers first one with three second one with three another one with three and the last one and after 10 it is completed so that is how the max size uh works we can specify the size of the buffer there is also a Time duration based buffery so this one for this one we can provide how much time do you need to wait for the buffer let's say I need to wait for 3 seconds maximum and get me the data from that time period right so let me give the duration of seconds as three and then run the program so in this case it should uh give me I three items per buffer so here it is coming as two or three so now you might be thinking you said three why it is only coming as two that is because the duration of mes is 1,00 millisecond or 1 second so uh 3 second means base there will be some Nan nanc or millisecond variations just like we have seen in one of the above case where we have to adjust the time so what we need to do is increase the duration of uh increase the buffer duration from 3 second to a little bit more so for that let me put duration of mes so instead of 3 second let me put 3.1 second so 3.1 second 3,100 that millisecond means 3.1 second so 3.1 millisecond will make sure that I will get three items since the items are separated in one second interval so here you can see 1 2 3 4 5 6 7 8 9 and 10 so that's about the buffering so with the buffer we can combine multiple inputs together and then process them in a step by-step fashion so that is really that is really helpful to load balance and all that we will talk in the at a later section of this tutorial another interesting thing we can do is called collect map so we have a publisher a flex with a set of items we saw how to collect the uh Flex into a list now let us see how we can convert the flex into a map with a key value so map mean map means there will be a key and a value so let us see how we can do that let me write another function test test M map collection so let me name it like that test map collection and I have a flex here of 1 to 10 as usual and here let me do it like this flex. collect so we saw how to work with the collect list then there is this option called collect map it takes two parameters the first parameter is the key so uh as you know uh the map has a key and a value so what should be the key in in this case what I want is like this I want an output of uh of an of for each entry I want to have a value like a and then a squared so for example uh for entry one entry five I need 5 into 5 25 for six I need 6 into 6 36 like that so I need uh the number and its uh square square value so that we can do like this so for for each key I can use the key uh for each integer I can use it as the key itself and as the value I can use the square function so integer into integer or you can do it like this math. power integer does it accept integer I'm not sure so here what is the output so power yeah it it will become double so I don't want to use Ma or power function I will just use integer into integer now there is a return type error so if you look into the error you can see that it is returning a map of integer integer this is a flex so it is going to be a mono because there is only one map output so that is a mono response and within the mono I have a map of integer integer like this right then I can call test map collection here and subscribe for it uh I don't need the thread dot sleep at the moment because there is no delay elements added so this should work without any problem and here it is uh you can see that for one we have one two we have four like that and we got the output let us look into a set of interesting functions that start with the name do uh there are couple of functions that are very useful uh in reactive programming so let me create a new function to explain it working so private I need a flux flux of integer just like we used before uh test do functions so let us understand how how these do functions work uh as usual let me create a flex and let us start writing the code so return Flex Dot and when you add like uh when you type do you can see all the functions that start with do so let us first understand do on each right and in the do on is uh you can see that the consumer is a signal so signal means if you look into the image we discussed before all these are signals like the subscription request on next on next all these are signals between the subscriber and the publisher so if we want to do something based on the signal we can do that do on each function so here let me write a uh function uh here so so signal uh just a signal and let me just print it s out signal let me just print that signal and do nothing else so uh let me attach this test do functions into the main uh that's it and and let me run the program and here you can see what is happening so these things are coming from here do on each uh function so for every item in the flux or in the publisher it is doing this processing so uh it is calling on next on next on next and there is on complete as well okay uh and uh let me update this like this so uh signal signal plus signal so we can see uh actually these 8 9 10 these outputs are coming from the subscription so let me remove the subscription for the time being and run it like this and now you can see uh the first one on next was called on next on next on next and once everything was once everything is complete it called the on complete function so that is the purpose of purpose of do on each thing so you can do task based on the signal and you can get the signal if you want to do some like uh special processing you can do it like this if signal and see here you can see what kind of type it is signal type so I can say like if signal type is complete on complete do something uh maybe I will write uh I'm done like right I'm done otherwise I would just say s out signal get value so I can I can do it like this so this way what will happen is like for every on next or every normal signals it will put like the value uh the integer value 1 2 3 4 5 like that and once it is complete it will put the message I am done it should work like that let us see so yeah uh 1 2 3 4 done and once it is done it is calling I am done so that is very useful and there are more functions so that is about the do on each let us look into more functions so uh let me create another function uh test do functions to because I want to keep the other one there for reference so test do functions to and let us look for other do functions okay so flux. do on complete do on complete right so this one so here what we have done is like we are explicitly checking for the signal type and doing the uh message output or write printing the message I am done based on the signal type this we can do very easily using the do on complete so as the name suggest when the flex is complete it will execute this function so let me write it again here so let me write the same functionality as of this one here so here I am writing I am complete I am complete right and to print each item I can put a entry like this system.out dot system.out dot print line right and if I execute the function now it should provide the same behavior see the 1 to 10 is printed from here and and the I am complete is done from the do on complete function so that is the do on complete function now let us create another function and see what else we have there do there is a thing called do first do on next now what if we want to uh what if we want to process each individual signal so here for signal. getet uh so here for printing each entry I'm calling signal. getet that that is giving me the integer what if I don't want to use that instead instead of getting signal for each one I want to get the actual item right so that can be done using the on next function so the on next function will give me each entry alone in the fleux so I can do it like this s out integer and it will print each item so if I run this program right now it will Pro it will print each item twice so the first time it is printing from do on next and the second time it is printing from the subscription subscribe method so 1 one 2 2 3 3 like that for each entry so that's about the do on next what else do we have there is a thing called do on subscribe so when someone subscribed to me if I want to do some operations let's say uh this is a subscription right reactive tutorial test function that is a subscription so whenever some subscriber subscribe to this Flex publisher uh if I want to do something I can do so in the do own subscribe function so let me put a message here s out subscribed right subscribed subscri uh I miss L buing yeah subscribed and to con to confirm it's working let me call it twice so uh I have one subscription there and the subscription here uh so actually this should be called twice uh because I am subscribing twice let me run the program you can see it subscribed first and printed all the 10 entries then then at the lane 128 another subscription happened and it printed all the 10 items like that so this is very useful when you want to track the subscriptions and do something with it Etc okay another thing so let's say I want to do something when cancelled yeah so there is a thing called do on cancel so uh so far we have been talking about the uh the the ideal cases like uh we create a flex the subscriber fully uses it and everything goes well what happens what happens when someone cancels a subscription so here you can see that when you make a subscription it returns a thing called disposable okay so there is a thing called disposable using this dispose I can actually cancel the publisher cancel the Publisher's connection to the subscriber and actually cancel the processing itself so if I call disposable. dispose uh it will be prematurely cancelled so let's see how how how to make use of it if I do it like this let me call S out cancel sorry cancelled and let me run the program there will be a problem right now as you can see even though I cancelled it actually printed all and items that is because this Flex do not have any delay and before executing this statement all of these are like quickly executed and uh the disposing didn't really have any effect on the flex but as soon as the flex is not instantaneous uh as soon as it has a delay this dispose will start working so let me tell you like this so let me show you uh let me add a delay so I want a delay of like a delay sorry duration of seconds I want one second duration as usual and I want to wait for like 3 seconds maximum so I will wait for 3 seconds then I will cancel the uh subscription then let's see what will happen so let me make the sleep to 3,500 so 3.5 seconds so uh we will get at least three items from the flex let us run it like this do on cancel and you can see one printed two printed three printed and cancelled so what happened here is the subscription happened uh the flex started giving data uh while the main thre waited for three 3 and a half seconds and it disposed once we call dispose no more items from the flex will receive the will be reached at the subscription or subscriber and it will be cancelled so the do on cancel uh is very useful in a real world scenario where we can't always process every entry we have time limitations so uh we want to do something when someone cancels the subscription so that can be done from the do own cancel function let us look for other functions right so there is an important thing called do on error so this is another set of uh special handling for error occurred in the streams so let me create a new function because error handling is a very major and important thing in the reactive programming so let us talk about the error handling let me name the function as test error handle let me show you so let let me uh give you a symbol flux that has a delay uh I think delay is not needed for showing the error handling let me use a map function so map so the map function will be executed for each of these items and I want to simulate an error condition here so let me write a full function here uh map if integer becomes five so when the number becomes five I want to throw an error to simulate the exception so uh when the number reaches five I will throw an exception called Throw new runtime exception error qu uh unexpected number maybe unexpected number unexpected number uh intellig is showing this error uh because uh we are not really supposed to throw errors but this is just for simulation this is just for simulation okay so uh when the integer becomes five we will throw an exception otherwise for all other cases we are okay we will just return the number uh in the map now uh let's let us call this function test error handling and let's see what happens so let me remove this dispose function we added and uh disposable yeah we don't need the Disposable okay now let me run the code let's see what happens so one printed no problem 1 2 3 4 and when the five reached the problem happened and uh the exception was thrown and unexpected number happened and the entire rest of the flex was dropped like once an error happens the rest of the entries in the flex will not be executed we can make them executable I mean we can recover from the error we have function for that we will see how to do that next so here when the error happened up to five it worked properly but when when the error happened everything stopped and everything went into flames right so to recover let's look for functions do on uh it is not do on on error continue so we have the function called on error continue and it has different uh overloaded options uh let's us go with the e EST option so it has this option where we can handle the error inside this function and then the rest of the entries in the flux will be executed normally so let's see how we can do that so when the error happens I will just write like s out don't worry about uh the object so here the object is going to be five so on error continue has two parameters the first parameter throwable is the error that is thrown and the second one is like the integer entry that had the problem or that caused the exception and we are saying on error uh just handle it here and continue uh let us run the program and see if it is uh behaving as expected so here it is 1 2 3 4 and for the fifth item the error happened and the on error continue function got invoked and we print the don't worry about message and then the six 7 8 and N so the rest of the stream uh rest of the flux uh got executed and uh it completed normally so this is one way to handle it there are more ways to handle errors in the reactive programming so let us create another function and see what else we can do when this kind of error happens so let me name the functions function as test error handling two I will put it here and let's see so on error continue we have SE it will continue what about on error return so on error return allows us to uh provide an option to return a value when an error happens let's say when uh we know that when the integer becomes five the error happens so uh when the error happens I want to stop the flex or publisher or cancel the subscription with a minus one value right so this way when it uh when the subscriber uh gets the minus one value it can do some special operations like maybe uh retrying it or do do some specific operations Etc so uh the on error return option allow us to return a special value when the error happens so here if you look into the documentation it says simply emit a captured fallback value when any any error happens so 1 two 3 4 will go when it comes to five it will return the special parameter we have given into the output and stop the processing so let us run this let us run the program and you can see 1 2 3 4 and when the error happened it returned the special minus one value and stop the processing uh this is uh really helpful as we discussed for uh subscriber to handle exceptions and uh this has like three overloaded options uh in one option you can give like a special value for special exceptions so in our case we only have runtime exception do class so here I have given minus one for the runtime exception I can add more so like if I have like arithmetic exception then I can provide a separate value like maybe minus 2 and do the processing uh like that so uh that's about the on error return option let us see what else we can do here so on error so we talked about on error continue and uh uh now we have the option on error resume so this is an interesting function so what the on error resume allows us to do is let me take this one so let's say our Flex failed so it it uh executed 1 2 3 4 and for fifth entry it failed failed then we can provide another Flex or mono that will be used when this error happens so let us try that uh let me write another flux so I have another flux of integers like a special error Handler flux and let me create a flux of range like maybe uh 100 starting from 100 and send five five more items right so the way on error assume works is let's say uh the error happened the error happened switch to special error Handler flux like that or I can specify it just like this this flux range like that and now let's see what happens so it should print 1 2 3 4 and instead of printing five since five had an error it will immediately switch to the new flex and continue or resume oper on that Flex that's why we have the uh word resume in the function name I hope you understood the concept and this doesn't have to be a fleux you can send a mono as well here so you can when an error happen you can just say like return uh my mono like mono 5 and process it see 1 2 3 4 five so uh this is like recovering from the error so uh we have already talked about three to four different ways to recover from error so the reactive programming provides a comprehensive way to handle all types of exceptions that may arise in the uh in the in your program so let me uh wrap up this function and let me look for other functions if we have any so on error assume sorry on error eror and there is one last thing so that is called on error map so this on error map is useful for mapping one type of exception to another type of exception let's say uh we are internally we are using uh the runtime exception but when we throw an error we have to be like a standard error uh like a special error so we can map one type of exception to another type so let me uh write the map here I can convert the runtime exception into any other kind of exception maybe unsupported operation exception unsupported operation exception and here I can maybe give the message from throwable to uh yeah throwable doget message so what is happening here is uh whatever the exception that comes within the flex it will be converted into unsupported operation exception and then will be given to the subscriber so let us confirm this test error handling three and let me run the code and you can see uh unsupported operation exception unexpected number see uh previously it was showing runtime exception so this is useful for like standardizing the exceptions you are throwing to the outside modules so this way uh no matter what kind of error happens within the publisher you can ch change it you can change it to your own standard exception so that's about it that's about the special error handling for uh reactive programming so far we have been doing reactive programming using the project reactor module so we learned how to create a publisher how to subscribe it handle all the cases now let us come back to the spring module and see how to create a web page or show at least a home page in our application when we open the browser so let me uh go into the main class learn reactive programming so this is where the spring module starts uh so far we have been uh working on this file uh for learning the mono and flex now let us uh start this file so this is the main file uh we can also start it like this uh so let me run that and when we do so it started so it shows that the server is started at 8080 Port so let me open it uh open the Local Host 8080 module and the uh this is the page this is the only page that is reachable and it is showing white label error page the application has no configured error view uh so you are seeing this as a fallback so we haven't configured any page so next let us see how to load a page page static HTML page using the spring reactive web module right so in order to do this we need a special module called time Leaf so time Leaf is a template engine I think uh you you could be familiar I think uh you might have heard of this time LIF module it is a template engine which is used to generate HTML dynamically uh for our learning purpose we will just add this dependency and uh configure a static web page a simple HTML content and see how to hook up a page into our homepage okay so uh in order to add time leavea uh we need to uh add the dependency so the dependency is or spring spring framework. boot spring boot stter time Leaf okay so let me copy this paste into our gradel so we have this implementation uh uh 3.2.2 yes let me build the application close it okay now the time Leaf is added now what we need is a controller a controller to link a page into the uh homepage right so let me create a controller here so Java class uh let me name it as main controller and let me name uh let me put The annotation as controller this is all Standard Spring stuff and then I need to write a function so public and uh usually what we do is like we will do it like this public string and then handle main then we simply return the name of the HTML file so let's say my HTML page name is home so this is how we usually do in normal spring since this is reactive spring we have to provide it like this mono of string and the home should be within mono do just just just like what we have learned in the previous chapters so uh handle Main and we have to provide an annotation at get mapping and provide like the path so when we load the Local Host 080 it it will come here and it will know that it should load home. HTML and it will load it let us run the program and see uh what will happen it will not work at the moment obviously because we haven't configured the home HTML page so let me do it like this so now the error is little bit different it is saying that it is showing an exception here saying that error resolving template home template might not exist or might not be accessible blah blah blah so what that means is we haven't added the HTML page so using chat GPT let me create a sample HTML page for this application so let me request your jpt uh create a sample see a uh uh sample uh HTML homepage for my spring reactive tutorial that I'm going to put on YouTube and uh let us wait for it to complete uh so chat GPT is gener generous enough to create an HTML for me very nice let me copy the code and place it inside the resources so here I will create a new file and that is going to be home. HTML because I specified the name here so home. HTML and let me paste the code here do a Gradle build build build okay so uh let us wait for the build to complete Assemble okay now let us run the program and see what happens so I have the page here let us wait for the server to start and end an error okay so we have an error and it is saying error template template might not exist so uh we have to make some adjustment here so let me do the debugging uh here we have added the spring board started time Leaf so that is enough then uh we we have added the controller that is good then we have the home. HTML I think we have to put that in a templates directory so there should be a page called templates then let me just copy this and paste that into the templates fair enough let me start the application again okay and here it is is so the homepage should be placed inside the templates directory uh that was a mistake on my side and once it is placed inside that the page is now properly loaded so uh whenever I load my server uh it will come into this main controller look for the page name and that page will be resolved from the templates directory now we have a pretty good homepage next thing is like uh we have to learn how to use use database in reactive programming so when we talked when we discussed the introduction we said that the main purpose of the reactive programming is for asynchronous non-blocking Behavior and the most common blocking element in any program is database because database will take some time to provide the data because it has to manage a lot of data lot of things has to be managed so we will see how to connect a mongodb database into the reactive application add how to add some data how to retrieve them all using reactive programming so let's see how to do that uh to work with a database I'll be using a mongodb database the mongodb is already installed in my system uh if you have other databases installed then the process will be almost same you will have to just add a different dependency I mean if it is SQL you will have to add a reactive uh SQL dependency uh for the sake of this tutorial I am not going to explain how to install mongodb and mongodb's own properties because that is the scope of a different tutorial uh if you interested let me know I will create a tutorial for that so for now uh let us see how to connect to a mongodb and work on top of that okay so uh first in order to add mongodb we need to add spring spring board starter data mongod be reactive so for non-reactive editions it is like spring board starter data mongod TB but for reactive programming it is a separate thing so let me copy this and put it into the uh build Gradle here that should give me the support for uh mongod DP okay that is good and the next thing is like I need to check my database so uh this this is a mongodb compass application uh this is to view the database contents and all this is provided by the mongodb developers themselves so I will connect to my database I do not have any username or password configured so it will take a while to connect and I have some uh DB admin config and local so the database is there uh so I should be able to connect to my database so let me create a new class to manage the mongodb database configuration so uh database configuration and then I need to add some dependencies here so in order to configure the mongodb database connection to the reactive application there are very basic simple steps so first I need to add enable mongod DB enable reactive sorry reactive repositories enable reactive repositories so this this annotation needs to be hand added and then we need to extend this class with the abstract reactive configuration class so this is an abstract class that has some buil-in methods uh that provides the necessary client template uh classes and then I need to add a bean here so that bean is going to be client so I need to create a client so the client is a class that helps our program to communicate with the mongodb database so or connect to the database so uh let me name it as mongod DB client and return this client dot uh clients do create cs. create so that is one thing and that is not enough I need to provide a database name so if you look into my database I have couple of databases already so I need a new database here so I will name it as uh maybe genuine coder genuine coder itself right uh let us drop the name like this database name I need one more Bean this Bean is required for uh making ques to the database that we will see just in a moment so uh let me create another Bean at Bean public reactive template and reactive template tempate and here I have to create the template new sorry new reactive template and this template requires a client that is already there and a database name so I will just call the get database so that's it uh with just this class and the dependency we should be able to connect to our mongod TB database okay uh so let me run the program and see if the output has anything new in it and here we go so you can see there are some special logs here MoGo client with metadata connected uh so yeah so the MoGo DB is detected it should work now now let us talk about our goal so what we are trying to achieve is like uh create two database classes one a customer and one an order so we will create a couple of customers uh via the uh reactive API and we will create some orders as well and then we process these uh customers and orders using the flex concept we have just learned to get a real world understanding of how to do that okay and we will also learn how to save to database in a reactive way how to get data from the database in a reactive way Etc as well so for that let me create a new Java class and uh let me name it as data Control Data controller and here I will make it as rest controller so uh I hope you are familiar with the difference between rest controller and the uh at controller if you are not I will just explain uh so the at controller says that what these response values are for the HTML HTM for looking up the HTML so here when I return the home it should understand that there should be a home. HTML and load it but when it come to a data the data will be in a Json format like when I am returning a list of customers or list of orders that is actually data not the web page name so for the those purposes we will put at rest controller to denote that the functions in this class are going to be rest apis okay okay then let me write a function so at post mapping and uh I need to provide uh here so first I need to create some customers so customer. create and here I need to write a function so public mono customer yeah I have to create the class right uh just a moment so let me write the function create customer and a request body request body customer customer this will all make sense in a moment let me uh fill it so here uh what I'm trying to do is I will receive a customer uh from the user uh from the web page or from a postman or something like that and then save it to database here save it to database and return the saved customer back to the person who request module or back to the client so for that I need to create some classes so let me create package model and create a customer class so let me create a customer class and the customer will have three Fields so private string ID a unique ID private string name name of the customer and maybe private string job whatever their job is and we need a Constructor so name and job uh needs to be in the Constructor the ID is usually automatically generated not provided by the uh end user so I will just use a random uu ID so this will create a unique ID every time whenever I call it it create a unique ID that I can use to find it in the database and then I need to provide some get and Setter so I will provide some gets and sets uh generated uh for this generator I am using alt insert so alt and insert button in the intellig will provide this dialogue for get transet and here we have to denote that this is not a normal class but a database class right so for that I need to provide a document annotation so this document comes from the mongodb library so I have to denote it as a document then I have to provide this as like ID so the ID indicates that this is a unique ID primary key in the database that is used to uniquely identify this customer so that's about the customer model so we will receive a customer model from the user we need to save it to the database to save it to the database we need the database reactive template so I will just autowire it here so at autowire private reactive template so I will get the template here and using that template I can save the customer to the database okay so uh if you look into the save output you can see that it is a mono because since we added the reactive mongodb Library all the responses from the database Library will be in a reactive way so we don't have to do anything extra it is already in a reactive way so it will be returned after the save okay so we are saving it to the database now we need to test this right uh let me check if I have to explain any other things so you might be wondering why this is not mono why the customer is not the response is mono the processing is mono why this is not mono that is because internally before calling this function it is handled by the uh spring web Flex uh Library so spring webflex will receive the customer as a mono and then it will convert that into a normal object and provide to us so that we can easily processes it process it okay so I believe that is clear now let us run the program okay and let us create our first customer okay so let me open Postman create Postman and let me create a new HTTP request so this is Postman I can make request to our server so I want to make a post request because I provided a post mapping so that is database uh post mapping and it is going to be Local Host 8080 SL customer SLC create so that is our end point right customer SL create then in the post request I want to provide a post body with a form data uh I need a raw data Json data so I will I I need to provide value for this customer like ID will be autogenerated but name and job should be provided so that will be given like this in a Json format so I will specify name as maybe name should I provide qu maybe then job right I need to provide a comma for the Json schema now if I execute this it should be it should get saved into database let us try send and there is an error so uh uh the response contains an error indal server error that means the server has crashed so let us see what happened so it is saying type definition error customer customer create exception and HTTP post codex exception a no default Constructor okay so the problem it is saying is I have provided a Constructor with two arguments I didn't that means I didn't provide a Constructor with no arguments right so I have to provide that so public customer just like this so I'm just explaining so what is happening here is when you add a your own Constructor the default Constructor will be removed so if you provide no Constructors then there is a hidden default Constructor that accepts new argument but as soon as you provide a uh Constructor custom Constructor the hidden one is removed so you have to add the uh no argument Constructor by yourself that is mandatory that is basic Java so I will just uh call this run this again and run the program once more and here you can see the name uh genuine coder job the ID was automatically provided to me and if I go into the database now I should be able to see a new database here a new table for uh so there is a new database test and there is the new new one called customer and uh the name is genuine coder and the job programmer so so that is automatically added right so genuine Cod and programmer that is properly added uh so that that's about the uh saving to the database so we have successfully saved a first reactive object into a database and uh fed it uh I mean the result was returned in a react active way as well so we created the customer now let's create some more customer so that is one customer genuine coder another customer is maybe Ana something like that Ana then maybe businessman maybe something else maybe pilot sorry okay so uh we have given the data and let us check the database and the customer now should have more than one I mean should have three objects in it and we have the three customers in the database now let us see how to improve this so we said uh we will create a customer now let's say we want to find a customer by their ID so let's see how to do those kind of of ques so uh at instead of post mapping let us create a get mapping so my goal is to create a controller function uh rest API that will receive ID of a customer and return the details of the customer okay so the response uh is going to be mono customer so find customer by ID and customer find by ID so that is going to be the endo and here instead of request body since this is not a post mapping uh post request I will create a request parameter function and the request parameter function will be will receive the customer ID as the request parameter H so that is going to be customer ID so I will receive the customer ID here and I want to find the customer from the database using the ID and return it to the client so for that let me write a function here private mono customer so I'll be finding a customer find by ID find customer by ID and then here I will have a customer ID as the argument custom so here we need to write a quy a mongodb query to find it so the way we write quy is using a criteria so we have a criteria uh that is like IIA where what we want to match we want to match a customer's ID with the given customer ID so I will provide the ID here custom criteria where ID is customer ID that is uh usual mongodb uh Cory format and we have to prepare a Cory for this so Cory Cory equals Cory quy criteria so we converted the criteria into a database quy Now using the reactive template I can find it like this so I I I can give the quy here and I want to identify the class so what is the class here the class of the customer is customers. class so it will return it and when I check this here it is saying that I provided a mono but FX is expected that is because the fine method will return more than one result if it is available but but ID is a unique field like a uu ID unique to each customer so we can go with a find one so what that means is it will only find One customer and it has an error again uh that is because the find customer by ID function is already used so let me change it to get customer by ID now coming back here we have the customer ID here and we wrote the function to find the customer so we can simply use it like this return get customer by ID customer ID that's it okay now we want to test it right so to test it we need some valid ID so let us take an ID from maybe this customer 65 BF something like that is it copyable yeah that is copyable so I am trying to find Ana by her customer ID right so let us go to postman create a new request so this is going to be a get request so that is fine Local Host 8080 customer find by ID and here I I have to provide a argument as the customer ID sorry the parameter the Cory parameter so I will give it as like that and make a send request and here you can see we gave the ID and it correctly found Ana HR and the uh the ID so the whole customer object is properly returned so that is how we do reactive quy okay now one interesting thing is we learned how to log right we learned that if we put a log function we can find how the uh subscription and uh events are happening with between the subscriber and the publisher in the reactive programming so we can apply the same here so if I restart the server with a log here you can actually see how the uh data is fetched from the database that is one of the biggest advantages of using reactive uh you can actually see every step using the log function so let me execute the quy and here you can see on subscribe so it subscribed requested for the uh data and it returned a customer then once the customer was returned the subscription was completed so on complete complete so subscribe next uh request next and on complete so here the customer do not have a two string function that is why it is showing like that so if I add a two string alt insert to string and I will create a two string function I don't like that alt insert do we have a better two string function string cat uh string bu string Builder string Builder maybe no that is universe so uh let me just use a standard two string function okay okay now if I run the program again and make the send request you can see uh subscribed request sent the customer was properly identified and returned then it was returned to the postman the client who made the request so that's it guys uh that's how we do the quy in the mongod react active mongodb now let us uh go one step higher and see how to do complex operations on this so for that I need one more model that is going to be named as order order so in the order similar to customer I need an ID for the order but I need a customer ID as well so every order will be assigned to a customer then I need a money field that is is double maybe use double total so every order has a total and I need a discount as well so discount amount so this will be a unique ID generated for every order this will be the customer ID and every order will have a total and a discount value we need a Constructor right just like before we need a full flood Constructor we need a no argument Constructor we need a getter and Setter and we need a two string function yep so we have created the model one thing we forgot we need to denote this as document so that mongodb can find this and you can see the mongodb icon came here as soon as I added The annotation the class is identified as a mongod DB entity and here I have to provide ID as a unique uh primary key for this table so the order model is created and coming back to data controller we need a create function for order as well so that we can create new orders so order create and in the mono customer I will provide mono order then instead of create customer I will call it create order instead of receiving a customer object I will receive a order like this then I will save that order into the reactive template I think that is clear enough because we did the same for the customer now let us go into the uh post uh just a moment yeah uh into the post man then write the function so post Local Host 8080 instead of customer. create it is going to be order create and then I need to specify the body ra body Json body and I will provide the Json value here so if I go into the order ID will be automatically generated so I don't want the ID here like this it is going to be uu ID random U id. to string so it will create a unique ID okay so I need a customer ID so the customer ID I need to take from the database so first for genuine coder I will create some orders for that I need order ID customer ID from genuine coder then let me create total value so sorry let me add a total total so how much we want maybe I will add a 50 50 uh as the total and a discount maybe five for the first entry let me send a save request uh I I haven't restarted the server I need to restart the server to get the new changes and now let me send the request so I send a request it completed it it as assigned an ID for the order and the customer ID uh and the rest of them are like the data I have provided if I re uh refresh the MB Compass I should see the new collection uh new table that is called order yeah I can see that and it should have one entry yeah that is there so uh let us add some more uh orders into uh the genuine Corder customer uh so maybe a 100 more with a discount zero second order and maybe one more order 150 with a discount of 10 so for genuine Corder three rows are created now for Ana let us create another so I copied Ana's ID then I will put total as 100 then maybe discount as zero first row then maybe I will add 1,250 with 100 EUR as discount right okay so that is enough data I guess uh three customers and uh each two customers have orders we can work with that okay so these data are generated now now let us come back to the data controller and let me tell you what I want as an output so my requirement is uh I want a get mapping function uh sales summary so when I call this one I need a map I need a map with the customer name and the to total amount they have uh uh total or order total so genuine Corder for genuine Corder customer we have added three orders so I want the map string uh the first string as the name of the customer that is genuine Corder and the sum of all the order totals okay so sum of the all sum of the order totals so for the second Anya customer we have two orders so we have to add the order total for Ana as well and for the Third customer there are no orders so it should display as zero so uh let me name it as calculate summary like that so let me write the requirement here uh as a comment so the expected output is expected output genuine coder would have like sum of order total so uh I I don't don't remember the exact total so I will just write it like that then for Ana we need some of order total then for the third one that was Bruce right so Bruce Zero because no no orders this is the expected output so let's see how to do that so the first thing is for uh the output requires each customer so first we need to find every customer in the database that can be done using find all method so this will give me all the customers in the database by providing customer. class so at this step after the find all output I will get a flux of customers that is all the customers in the database that is fine so uh I will use map I will use the map function uh just a moment data controller right map so I will use the map function so now the requirement is for each customer I want to calculate the order total so I have to write a function here so let me write a function to calculate order total for each customer so private and it is going to be a double value the sum so mono double calculate order sum so when I give a customer ID I want to get all the orders for that customer and calculate the sum of the orders so that also can be done like this so return reactive template find and we have to provide a quy so we have to find all the orders belonging to this customer so criteria criteria equals criteria where customer ID right because in the order table we are talking about the order table we can find all the orders belonging to one customer by checking the customer ID so customer ID is customer ID right customer that is my criteria so I can write a Cory here Cory quy of criteria and then here I have to provide the class so the class is going to be order. class because I am searching in the order class so what will be the output so that will be the output of all these like all the orders for that customer will be fed now I want to calculate the sum of all the total values for that for those orders so that can be done like this so I will provide mapping so order dot get total so the order objects are converted into double objects so at this level we will find all the orders and then all their totals and here we have to calculate the sum so how do we calculate a flux of double to it sum so for that we have a thing called called reduce function so in the reduce function you can provide how to sum or aggregate a couple of values so here initial value I will give zero and then I can provide like double dot sum let's see if that works so what happened here uh order. total I can uh uh I can use Lambda function here Lambda method reference z d yeah and here using the reduce so what happens here is all uh at this point what we have is like a flux of double you can see here flux of double that is we have a flux of order totals and I have to add all these flux values together into a single value a mono value that is done in the reduce function so this function will return sum of the order sum of all the orders for this customer so we have a map function here and can we use map here we talked about this when to use map and Flat map I cannot use map function here because this is a mono publisher so this is a publisher not a default not a standard uh synchronous Java return return value right so if I uh I if I do it like that then I will not be able to process it so if I do it like this customer. get ID I got the customer ID and what is the problem calculate calculate order sum yeah calculate order sum so here it won't say any pro any errors but the problem is the output of the uh input of The Next Step will be a mono of double but what we need is a map a map with the string string customer name so at this level I need both the customer and the order sum so for this we have to use the Sip operation we have learned in the uh previous chapters so uh if if you if you go back into reactive tutorial we talked about sip function so using sep function we can combain two data into one stream so I'll be using that and it will get clear as I write it so I need to use a sip function here sip with uh I I will have to use flat map yes and I have to use flat map so here the map doesn't work because the output is not going to be a synchronous value it is a reactive value so I will use flat map then I will use mono doip and I have two values required in the next line that is one is customer so mono do just customer is required so I will get the customer I I also Al need the sum for the customer uh sum of order total for customer so calculate order sum then customer do get ID this way in the next line I I will get all the required data that is the customer details and their order details now I want to convert this uh data into a map so for that we have collect map function collect map function we also talked about the collect map function in the previous chapters so let's see how we can do that so we said that when we zip something we will get a tuple Tuple two so for here we have like duple two and Tuple two dot get T1 so what is the T1 what is the output of the first sip sip argument that is the customer so from the first tole entry we will get I name of the customer so get name from the second Tuple uh entry so that will be that is going to be second value so again tle two Tuple two. get T2 that is going to be the order total so we got both the necessary data here so uh you might be still confused like why did we take the customer into a sip from here that is because we need both of them in the next step to create the map so we have created the data we can just return the value like this and uh calculate summary right so this this is not a reactive value right this is a standard value so that has to be converted into mono map string double and here it says I can use a Lambda function so replace with Lambda and that should be that should be all let us run the program and see if everything comes together as we expected so let me go into the postman and create a new request so that is going to be a get request right that is because it is a get mapping so Local Host 8080 sales summary and I will make a send request and here you can see Bruce we did not create any orders for Bruce so that is zero Ana had two orders that adds to 2250 genuine Corder had three orders and if you add all of them together that will become 300 and that's how we process data in a synchronous programming so my so if you ask me like isn't this complicated yes it is complicated than the usual programming I mean the synchronous programming but the advantage usually outweighs the extra complexity so this is hard to debug I'll will be very straightforward reactive programs are hard to debug hard to write as well but but you will save a lot on your server resources I mean if you're paying for a server the additional development time will outweigh the development uh the will be beneficial sorry I mean the additional development effort will be justified by the reduction in the server cost so that is there so we have learned how to ATT connect to mongodb how to save to mongodb how to retrieve from mongodb be and how to process the data in a multi-step fashion so that's about this part in the beginning of this tutorial we talked about the need for reactive programming we discuss about the asynchronous and nonblocking behavior in detail we have seen the code in action and uh you should be now very familiar with the asynchronous and non-blocking concept the next most important thing in the reactive programming is back pressure so let us see what BR pressure is and how it improves the reactive programming so let me take uh the back pressure slide so what back pressure is is is a fail safe mechanism for reactive programming as you know every server has a limit a limit on the number of request it it can process so this is dependent on a lot of parameters like network capacity the CPU the memory availability Etc so everything has a limit what happens when the number of request that is coming to the server is more than that it can handle at any time so here is an example so there is a publisher that generates the events and it is sending 10,000 events every second and we have a consumer or a subscriber that consumes or process it and the consumer can only access accept 7.5k or 7,500 events maximum at any time so what do we do in usual case or in a normal case the consumer just fails it is overwhelmed by the amount of request it is getting and uh it cannot process and it will go into an exception state with reactive programming and the back pressure concept we have the ability to control the amount of flow and and we have total control over how much data the consumer can get so we can limit it and there are many ways to handle this kind of extra pressure or extra request coming from the publisher side to give you just an overall idea when there are 10,000 events coming and the consumer can only process 7.5k events at at any time then what are the options we have so one one option is we can drop the extra 2,500 request this way the server won't crash the another thing is like we can keep the extra request that cannot be processed at the moment into a buffer and then process it later so uh those are some of the options so thanks to the reactive programming framework we have all these buil in to the framework so we'll be seeing how to do that we'll be simulating a overflow experience for the consumer using a sample code and uh we will just go through these points as well so uh you can see it provides flow control allow the system to control the pace of data flows based on consumer capacity preventing overwhelmed consumers ensures that the producer doesn't send more data than the Consumer can handle so even if we don't have the control over the producer we can still handle it at the consumer level and it will prevent overload so like we said uh when a consumer or when a server receives more data than it can handle it will automatically crash but we can prevent it we can we have preventive mechanism we have the ability to handle the extra request coming gracefully so let's see how we can do that so coming back to the code let me uh take you to our project we have been working on I have created just a new class it is called black pressure tutorial there is nothing just a main class first what we have to do is let's consider a normal case where there is no uh there is no overflow of data coming uh let me copy a Syle function I have prepared so I have a function create no overflow flux so let me just walk you through this function so we have a flex of range it will generate value from one and create uh integer max value this is a very big value as you can see 2 to 31 minus 1 so it will create a lot of values it will log it and it will apply an operation so conat map means uh this is just a simulating a uh delay on processing it so uh let's say in real world scenario when we receive a data we have to apply some operation on it these operations could take time here for uh showing you the behavior behavior of data offlow we can't provide a really complex operation since it complicates things so this will just simulate like a 100 millisecond so what this means is every Flex entry every entry in the flex will have to take will take 100 millisecond to process so uh it is just to simulate the process processing take time now let us uh call the function tutorial create no overflow Flex and I will just use the function block last here so block last means it will block until the flex is complete I'm not adding a thread dot s function here as the block last will provide the same experience as before so let me run the program when when I am running the program let me stop it so what is happening here is there is no overflow because the data is coming only after I make a request so uh the subscriber uh since it is subscribed here uh block glass will automatically subscribe you don't have to provide extra subscription subscription here because this is a blocking call so uh it will uh create a subscription so you can see the on subscribe it will request for one data and get the data make another request and get the data and so on and so forth so there is a delay of 100 MC because we added this line so this is the ideal case where we have control over for the producer and the producer provides data only when we make a request so no overflow here now let me show you a case where there is an overflow of flex so I have prepared the function here let me just paste it here and show you so consider this function and let us try to understand the difference between these two so here Flex do interval duration of millisecond so here what I am saying is in every 1 millisecond create a new entry into the flux okay so uh instead of uh requesting sending a request and then generating the data we are providing a duration value here that means in every one duration that is given here which is 1 millisecond a data will be generated and as just as like we discussed Above This each of these will require 100 millisecond to complete or process so there is a problem here every 1 second a new data is coming but sorry every 1 millisecond we are getting a new data and 100 Mill second is required to process that data so naturally we'll go into an overflow State okay so let me run this one create overflow flux okay uh then let me run the program now you can see it started it started the subscriber started made one request it received the First Data but before uh receiving the next one it wasn't able to complete the first one itself so it is saying could not emit dick one due to the lack of request interval doesn't support small down screen request like that so that is an overflow so every 1 millisecond we are getting one request but we need at least 100 millisecond to complete one request so what we do we have to make use of back pressure technique so the first back pressure Technique we have is dropping so uh here what we can do is uh in 100 millisecond we are getting like 100 100 request so one option is like drop the 99 request and process the only one so if there is any offlow of data just drop it and don't care about it okay so let us see how we can do that and for that we have a function here that is called on back pressure drop it so this is very similar to this function off for flux we have the flux interval that means it is creating one request in every millisecond every request will take 100 100 millisecond to complete then return a means I want to show which of these entries are getting processed and which are getting lost so that is why then return a a means uh every the every integer will come here and then uh it will be shown here okay uh sorry instead of integer it is long value so let let me show you how this runs so if I add create drop on back pressure flux and run it you can see some interesting patterns here let me stop the program and show you so uh element kept by consumer so the uh first element was Zero it was executed element kept by consumer means it is executed and it is propagated to the subscriber so do on next it is coming to the do on next after zero Only The 100 19th element were able to be processed by the consumer all the other requests that means like 118 requests were lost or dropped because the consumer was just overwhelmed with the request so this is one option we have uh for back back pressure whenever the consumer is overwhelmed or cannot process the request just drop it so the problem with the dropping the request is it is not all always ideal what if if you are making request to a server and it just drop the request that is really bad if the uh request itself cannot be repeated or we have some constraints on that so dropping the request is always not a feasible solution so what we can do so what we can do in that scenario is like whenever there is a uh overflow of request store the extra overflow request into a buffer and and process it from that buffer when the number of requests I mean when the uh consumer is free to process that request so that is possible very easily with just one option so let me write that function for you here so create drop on uh create drop on back pressure so create buffer so let me create a buffer function so uh here uh on back pressure if you look into the on back pressure option we talked about the drop option that's fine now we have a thing called buffer so in the inside the buffer we can specify the size of the buffer like uh if I specify 50 then what will happen is uh if the consumer is not able to process a request it will go into the buffer and until the buffer is full all the request will be going to the buffer or will be going to the uh I mean it will be processed by the consumer so 50 is not enough for our case because in every second itself uh I mean in every 100 millisecond we have like 99 Ros extra coming so let's see what happens let's see uh what happens if we run it with a uh buffer right let me just run it and you can see uh elements are getting uh uh kept by the consumer and eventually it got overrun that is because the buffer size is small and the buffer says is not enough to fully buffer the extra request that is coming from the producer side uh it ran okay for a while but then it failed because the buffer is just too small so you can increase the buffer size but that doesn't solve the problem fully it solved the problem for a while but that is not a real solution that we can provide so what we can do uh to give you an idea we have one more parameter we can give here and that is buffer overflow strategy strategy so the buffer overflow strategy provide a provides us with a way to do to do something when the buffer gets full okay so let me look for the function buffer overflow strategy sorry buffer overflow strategy class and we have these options one is error so whenever the buffer gets full just throw an error that is what happened now so let us try the option drop oldest so what will happen is whenever the buffer gets full the first request that came into the buffer will be dropped so if you want to drop the latest ones like uh when the buffer gets full the new request will not be added into the buffer and simply dropped you can use the drop latest okay now let us run the program again so let me set the buffer back to 50 so we can see if we are uh if we are getting the exception so it is processing like uh 31 35 39 45 see after the 50 after this one it dropped all the request until 128 then after 128 it dropped all the request until 237 that is because even with the buffer our consumer is not able to fully process the request but still it is better than the full back pressure drop because we are able to process some uh request using the back pressure buffer and here the situation is very much tight because for all the requests we are taking 100 milliseconds usually uh in real world scenarios some request will take some more time some request will be executed very fast so in that way when we use a back pressure buffer it will even out eventually the request from the buffer buffer will be able to uh be executed by the uh consumer so that will be sold like that so these are the most important concepts of the back pressure so the back pressure feature let us make sure that the consumer will not be overwhelmed by the amount of request it is getting from the producer and this helps us to keep the server stable scalable and much more secure so that's about the topic so far we have talked about the asynchronous and non-blocking behavior and the back pressure feature for the reactive programming uh those are the two most important things now let us talk about the resource efficiency and better streaming support for data uh since you are familiar with the concepts of the reactive programming so let me go into the slide for the resource efficiency and by resource efficiency what we mean is using reactive programming there is no blocking weight that means the threads are free to do more CPU intensive task than the traditional non-reactive programming ways so again to recollect the idea in traditional non-reactive programming we make a request wait for the data to be ready and then return the data using the thread with reactive programming there is no weight that means the threads or the CPU thread are not blocked waiting for data it is always ready to do more processing and more uh more processing so the resource efficiency comes from the lower thread usage so that is one of the points the number of thread that is blocked or working at a time or Alive at a time is very less compared to non-reactive approach so this makes because every thread has its own it's on requirements like every thread requires some I I am not sure about the exact megabytes but it requires certain megabytes to be created by itself so those kind of overheads are lost so it is much more efficient in that way and it is optimized for system resources so it consumes very much less memory and CPU resources because it is not blocking and it allows the application to run uh it allows running applications more on the same Hardware so let's say you have a 2GB RAM and a two core CPU and you are putting your application in there if you make use of the reactive programming properly you will be able to serve more number of customers or clients in the hardware than compared to the non-reactive programming approach and this is enhancer for cloud and container environment so nowadays uh if you look into AWS orle Google cloud services and all uh the payment is made per request or the resources we use uh specifically the RAM and CPU so with with being more efficient than the traditional non-reactive programming Paradigm or method uh this is much more Optimizer for the uh cloud and container I think this is much more suitable for the cloud and continent environments and there is a concept called microservices so so if you are familiar with microservices this reactive programming method provides support for functional programming which is really really helpful for the microservices so if you're planning to do a microservice project or if your project requires microservices then it will be a wise idea to go with a reactive programming approach because this forces you to do functional programming so that's about the resource efficiency we talked about the back pressure and and better for streaming data so better for streaming data in the sense it is built in a reactive way so uh you have seen the flex and the mono and you have seen the infinite streams we discussed with back pressure so the reactive programming is developed or is developed in a way by considering that the data will be coming in a stream stream way so one after the other and how do we process the Stream and it has very good integration with the existing streaming libraries so uh when you create a mono or Flex you can actually create them from existing reactive or asynchronous libraries like RX Java or Java's own completable future objects so it is tightly integrated you can use directly with those and it is efficient for handling large and infinite data streams and realtime data processing is a very important part because realtime data comes continuously and it should be processed in a very small amount of time so that is because it is coming continuously and uh there is no DeLay So for these kind of things since the reactive programming is developed for the streaming uh requirements this is much much better to handle those kind of use cases compared to the traditional approach and I think that's about it so uh if you guys have any suggestion for new tutorial topics please let me know in the comments and uh if you would like to learn more about reactive programming about some specific use cases like uh we haven't covered how to read and write files in uh reactive programming those kind of things so if you interested in specific topics do let me know in the comments as always thank you for watching this video see you later
Info
Channel: Genuine Coder
Views: 4,449
Rating: undefined out of 5
Keywords:
Id: y3ySZkSgWik
Channel Id: undefined
Length: 150min 17sec (9017 seconds)
Published: Sat Feb 10 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.