Spring Events Masterclass | Code Async Listeners in Spring #2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back so in this lesson we'll continue our spring events uh the lesson that we have learned in our last session so this is going to be a kind of advanced session so if you have completely skipped our last session and you are continuing with this one i assume that you already know a little bit about events because here in this session we're gonna talk about a lot of advanced things and i'm really sorry if my voice is echoing a lot uh because you know i'm in a different uh setup and i'm traveling and i'm in a hotel room right now so if you are finding my voice a little weird so excuse me for that all right so in this lesson let me just quickly tell you like you know what are the course contained uh sorry what are the lesson content that you'll be learning here okay so that you can take a call like you know whether to watch this particular video or whether to skip it but let me tell you that this particular video is going to be um it's going to be the prerequisite for my spring security advanced course because you know spring security we'll have a lot of different events and we will be listening to those events so if you are um if you are just starting spring framework uh you can take a call whether to watch this video or not of course if you already know spring core as i said in the last video you can get started with this one okay so let me tell you quickly the course content so that you can take a call so first of all we'll create few uh you know listeners and we'll create few events and we'll be listening to a few events and then we will be uh also learning about how to create multiple listeners okay so basically i i have told you in the last class that we can create one listener at a time in a particular class right so how can we create multiple listener in a one single file and also we'll be learning like you know how can you create a method which can listen to multiple events like you know you'll be creating one listener and that listener will be listening to multiple events and also you'll be learning like you know how to create uh asynchronous listeners it's very important you you have to um you know care about the asynchronous stuff a little because of course you don't want your listeners that you will be creating uh should execute one after another rather what you want is that you know you can create multiple listeners and all those listeners will be working in a multi-threaded application and will be also learning like you know how to make them you know multi-threaded right so a whole lot of different things also will be learning like you know how to order the listeners and blah blah blah so a lot of different things will be learning a lot of advanced things so we'll go step by step again and i won't be confusing you guys so if you are pretty new to spring events then again i'll recommend you go back to the last lesson and watch that but if you have a little bit of i mean knowledge and event then you can continue with that i mean you can continue with this or you know if you are if you are already working for last six years and you you have a little bit exposure to spring framework you can get started with this particular video so a whole lot of advanced things are waiting for you so let's get started all right so let's get started just tell me first uh that have you guys practiced yesterday and is there any confusion because i'm gonna do a quick um i'll be developing a quick listener and a quick event okay um using spring just to you know just to refresh your memory but have you guys practiced like you know whatever we have done yesterday can someone confirm yes okay everything is working fine yeah it's working i'm going [Music] one listener can can use multiple event listeners yes one listener also can listen to multiple events of and that's the thing i'll be discussing today but yes you are correct there is a way where we can define multiple listener i mean one listener and that listener can listen to multiple events and that's that's possible okay all right any other questions all right so let me get started with the new thing not new thing um let's do some hands-on and let's we'll just go to the spring default events and we'll go for a little bit advanced thing today just like synchronous and asynchronous events and then once you're okay with this then you know we will go for some spring security events and we'll just start learning about them as well okay so maybe this is the thing which is gonna again be only be focused on the core side of the spring and i won't be doing any kind of web request or i mean we can also create events for our web request right whenever a request coming we can trigger events and we will will be doing that particular things in spring mvc and in springboard obviously will be will be able to create the like you know whenever your spring boot application is starting we can also able to define events at the time right so if my container is starting do something if my container is getting stopped do something right we can do a whole lot of things right there but right now first of all you know i'll just try to recap everything and i'll ask you questions right now and i won't be doing the things today you will be making me do this particular thing so i've just copied your project okay and i have deleted all the classes that i have created yesterday in this project so this is your last project i'll give you this particular file if you in case if you need this and today what i'm going to do let's create a scenario okay for an example imagine like you are sending money to someone uh using google pay or phone pay or you are doing a bank transfer and the transaction fail and if the transaction failed you want to trigger an event okay and that event should be listened by um you know different listeners just like your you know sms listener or email listener and sms listener will send an sms to the user email listener will send an email to the user how will be implementing that okay first of all let's make it very very simple and let me quickly do that within 10 minutes so first of all i'll go to let's say i'll be going to the publisher and i'll be creating a new class and the class name is gonna be let's say gp right and gpa what i mean by that is google pay and it's i mean if some i mean any one of you is listening to this recording and do not understand gps gpa is something i think everyone should be aware about this it's just a medium to transfer money from one person to another okay and i'm just creating a class here called gpa and let's say that you know inside this gpa we have a method let's say public void send money and here in the send money method let's say um okay i will ask someone to give the money that you want to transfer let's say double uh amount okay and also i want to know what is the user name because i'm creating a domi method i'm in a domain class so let me minimize this and here i'll be asking the user what's your name so string name double amount okay so what i want to do here let's say i i don't want to do anything else let's log something just right here let's say hi then plus then the name okay whatever the name he gives let's assist out and one versus out let's say sending amount thing i'll just create a log here by concatenating this amount and let me say um is successful all right so simple send money method we have in gpa and gpa i will make it a component so this class will be there inside the container and i'll be going into the you know i'll be going into the app.java and here let me create an application context quickly so i'll be creating application context application context context is equal to new annotation config application context and yesterday we have created a config class which does a component scan to all the packages starts from com and obviously it's a configuration file i'm going to copy this off config goes to the app.java okay and i'm going to define my you know configuration class name just right here and there we go all right and from the container right now from this container i'm gonna get the bean context dot get bean of gp so g pay the class name is starts from the capital g so the b name will be by depaul small g and i'm gonna say gp dot class okay there you go and this is gonna get me a gpa instance and i can call that method let's say bean dot send money let's say my name is villash and amount is let's keep some amount let's say 200.00 control s okay so now if i'm gonna run this application okay so let's see what it got i mean it's printed higher velocity and sending amount 200 is successful now think like you know whenever you know the transaction is getting failed let's create some condition and make it hard code and let's make the transaction get i mean how can i make the transaction get failed like you know let's say i'll have an if clause okay and i'll make some condition which will trigger automatically let's say one equal to one and i'll throw let's say throw new or run time exception okay runtime exception which takes a string message let's say transaction felt okay there we go um so if this condition matches one equality equal to one then obviously this particular runtime exception will trigger and it's throw it and it'll throw an exception called a runtime exception called transaction failed so now what i want you to do whenever the transaction fail let me let me first you know test this application or test this particular thing maybe what i can do here instead of doing one equal to one i'll make a condition okay and this condition i'll just control from here okay and i'll make it a boolean condition okay there we go so now let's say someone if i'll go to the after java let's say if i'll say condition equal to false then obviously you know this if will not be executed because this will be false this will be skipped and this code will be executed if i'm gonna run this now it is gonna say hi velaz sending money 200 is successful now if i'll go to update java and if i'm gonna make the condition true then it's gonna trigger that exception sorry that i'm doing this but you know i'm just trying to show you that okay imagine that transaction is failed and we are getting a runtime exception okay now whenever this runtime exception occurs i want i want that even i want one event to publish okay so that you know the listener the the people who will be listening to this particular event for an example maybe a sms listener or a email listener will be listening to this exception and will be keep doing their work okay for an example whenever the transaction failed i want an sms to trigger to the user and i want an email to trigger to the user as well okay and i don't want to do do some hard coding right here inside the gp class okay this should be an independent class but only when then an exception is occurring it should you know uh it should it should notify the listener okay so how will be doing it okay so first of all um what i'll be doing there is the if condition here and i will also make sure to put that inside the tribe block second i put it inside a try block okay put everything inside a try block okay cast exception okay and the exception whenever some exception will be thrown i'll just do e dot print stack trace and also i want to publish an event okay so let's say whenever this condition will fail it will throw an exception the flow will come to the cache block and when the flow is here we know that the exception has occurred and i want to publish an event okay so how can i publish an event i need an event publisher and for that i will be doing private event public what is that application event publisher application event publisher let's say event publisher okay and whenever the exception occurred i'll be doing event publisher dot publish and i will be publishing an event okay so well i don't have any event so far so i have to create an event so anybody anybody knows how to create an event in spring any any answer to that how to create an event which class willy yeah we can create a simple project that's fine and also there is an interface what is that interface name if you if i want to create an event uh application event perfect application even so you can see there are a couple of cloud a couple of methods here either you can publish by passing in a normal object what he said a simple pojo and also it can pass in an event which extends to application event and that's that's another way to create events in spring and in older application and even in even in spring framework if you if you're gonna go and see uh the the framework code you will be seeing a lot of classes is extending uh to the application event for now i'll be creating i'll be creating a simple event let's say new i don't have an event yet but let me just just right type it here first then i'll be creating the class let's say new transaction failure um hello event and let's say i will be creating a new transaction failure event and to this event i'll be passing in two things let's say the amount or the name let's say name whatever the name i got from here that name i'll be passing to this and also i'll be passing the amount and of course you know don't worry i don't have this event yet so i'll be creating a new class so create class and i'll be putting this inside the package called event where is that even package there we go event and i'll do a finish okay i don't want to create a i don't want to create a you know event by extending to the application event we'll do that later okay not needed as i said in the latest version of spring we don't need that so this is going to be a simple puzzle so maybe we'll have a couple of variables here private string let's say username and private string private double uh you know amount okay i have this uh i have this event class maybe i'll just create the gator and setter for this uh and i'll create the getter instead of for amount as well actually you know i don't want to create a setter to initialize maybe i can initialize it with the constructor that i have created there okay so i just have the getter and also i'll go to the gp class right now and this constructor i want to create inside the transaction failure event class okay so can i create a transaction can i create a constructor there we go that's the constructor and this text to parameter let me initialize this dot username is equal to username sorry what is that username can i make the name same control v and let me say it amount ctrl c ctrl v okay so this dot name is equal to name and let me initialize the other one this dot amount is the ql2 amount all right so that's it and let me create the get getter again one more time control one create getter and setter do okay and create getter and setter for amount click ok and i don't want the setter let me remove the setter and let me remove the setter for this ctrl a ctrl shift f ctrl s so what happened here okay close this right click source um generate constructed using field do a generate and there you go okay so this looks good right now we have a simple puzzle called a transaction failure even so what actually happened when in gpa when someone is sending money and whenever some runtime exception or some transaction failure exception or any kind of exception is occurring we're casting that exception just right here and we are we are publishing an event called transaction failure event right now there can be multiple listeners who can listen to this event for example i can go to the listener class and i can click on new and i can click on class and i can create a listener class called um let's say send sms listener okay do a finish and the send sms listener is gonna be listen to that event and to listen to an event a listener should be in component okay because that that event will be published by the gpa and gpa is also in component so this event will be published through the application context and any listener which is registered with the application context or the container will be notified when the event is triggered okay right now let's say someone is uh trying to send the money and the exception is occurring and right now this event is getting fired the event name is transaction failure event so application context will notify all the listeners who are listening to that event let's say the send sms listener so what we can do we can just create a method public void listen or let's say start listening or start let me say start method and this will be a listener so event listener okay there are a couple of way as i said you can either use to application listener you can either implement application listener this class can implement to that interface or simply use the add event listener okay and it can do um sysout okay um and you want to just make sure guys whenever you are creating a listener right if you are not providing the arguments to this listener then you will have exception for example if i run this code i'll be having exception so make sure that whenever you are creating a listener you have to give the parameters so event parameter is mandatory for event listener okay illegal state exception so you cannot define a method called stat which is the event listener if the event is not defined okay so whenever you are creating an event listener make sure you are going to provide the event that you are expecting here for an example i'm expecting a transaction failure event event and now you can you know track this from this event this event has already been created and been initialized and from this event you can say event dot get username that's the get name and let's say um hi something the username okay let's say she saw one versus out let's say um sending an email with buddy okay and i can just concatenate something just for demo let's say hi plus i don't need this i can cut this one ctrl x paste it just right here okay hi um name whatever the name he has then transaction failed for the amount control sorry amount and plus sorry i'm pretty bad with all this thing event dot get amount okay so here i can say hi let's just sending an email sending email okay control s right now there is a simple listener and this listener is listening to an event and whenever this event occurs called transaction failure event is gonna send an email and it's gonna say that um hi sending an email with buddy hi whatever the name of the person just want to show you this by doing it because just want to let you know that i have a complete hold of the event failure i mean the transaction failure event i can get the attribute or the data of this particular event and accordingly i can just you know i can do whatever i want okay so now if i'm gonna run this if everything works well that is a null pointer exception occurred maybe in the gp class i have not auto added okay and do i mean remember one thing guys this is one way of publishing an event okay there is another way which is a traditional way by implementing an another interface but right now implementing an interface of writing the method instead of that just auto wear this application event publisher and just do event publisher dot publish event that's gonna work fine now let me run this application and there you go something happened sending an email okay okay started sending an email right now because the transaction is failed so the listener is listening and is sending an email i just want to check one more thing in the in my app config did i okay one second i'll just do a link to editor i'm using this class only right because yesterday i changed this class while explaining to you can i close this project okay so right now we can see that that the uh you know transaction has hap i mean the transaction is failed and after that we are sending an email and um you know our our listener is listening to that okay perfect so then if you want to extend your application and tomorrow your client is coming and your client is saying that okay i want another channel for communication and whenever a event is event is published for transaction failure event at that time i want you to send an email and also i want you to send an sms at that time you don't have to come over here your publisher is not going to uh be in relationship with any of the classes this is going to run independent this is only going to publish the event and you can just simply create one more listener and that's the code is going to run independently without interrupting the publisher or without hard coding anything just right here for an example i can simply say previous one is send sms listener right maybe i can say send email listener okay i can create one more listener called send email listener yeah boolean condition is right now false right i have a typo yeah so i can show you right now after java okay here the bulldog condition is true that means i want that exception to occur if i'll go to the send money method the boolean condition is true so this condition is executing it is failing it is triggering a transaction failure exception or runtime exception and that runtime exception i'm caching over here and publishing an event and this event is right now listened by my uh sent sm sent uh sent email listener and this email listener is listening to that listening to this event how can i know it is listening to this event because look at the start method okay and it's looking in i mean it's listening to this event and obviously whatever is needs to do is doing right over here it says an email event so it's sending an email and sending an email just right here and just like that i'm creating a new listener called send sms listener it's gonna be again an event listener it's gonna listen to the same event called transaction if transaction failure event and right now let's say it is sending an sms and is sending an sms with buddy whatever previous one was sending an email imagine and this is sending an sms right now if i'm gonna run this okay you will see that okay the email is getting triggered okay and it is also sending an email so email listener is getting triggered and also the sms listener is getting triggered so both the listener are right now listening to that particular you know um event okay and one more thing you can understand from here right now first of all let's say right now the email listener is basically uh triggering and then the sms listener is getting getting triggered if you want to control the order of that i think um you know for some purpose if you want to capture the order what i want the listener should run independently your listener should always you should not depend on any other listener but if you have some dependency and you want one of the listener to you know executed first and followed by an another listener if you want to do that let's say right now the email is getting triggered first so i want to make that email second and sms first right now the sms is triggered second and the email has been triggered first so what i can do over here i can actually do one thing so here i can use something called order and let's say i can give a number here so sms listener i want to make it first right now it's executed in second i'll just say one here so it will it will be ordered to one and the same thing i can do here in the email listener i can make it one and i'll make it the order is two okay so now what i believe that the email should trigger second and the sms should trigger first the order of this should be swapped let's see that whether it is happening or not okay now the email trigger first email is not triggered first and the sms listener triggers second if you want to do ordering in case if you have a requirement you can use at order i think eos was asking me yesterday like how to basically uh do this ordering okay so this is the answer to that okay still the email came first after doing ordering yeah email i have made the email first previously right this was first only i think it was two email is two okay hold on what happened there control s i think that one was not saved okay maybe sms first email is second now it's fine sms i'm ordering to first and the email i'm ordering to second right it's working fine right and you can also change it to one change the sms to two so by default the things will work ctrl two oh sorry ctrl s save everything run that yeah yes maybe that time it was not saved right now the sms is in second the email is in first so if you have multiple listeners and if you want to make some kind of ordering then you can do that but i'll never recommend that okay why do you why why your listeners should be uh you know i'm independent with each other okay why you want to create a dependency in between them and why you want to make them in order make them asynchronous i mean i'll be coming back to the asynchronous and signals part but always try your listener should be individually running okay so right now i think you understand how to create listener and how to listen to an event and i think this thing is making sense to you so far we are on the same page or not i know i have rushed it a little but i mean you guys are okay till now all right really your voice is sl i mean low today i don't know why i mean it's coming really low maybe the headset issue or maybe your mic is not connected uh with the one that you wanted to connect with maybe you can change your mic i mean change the zoom setting with the mic oh okay oh right now it's good all right cool so the next thing okay one more thing before i jump to the next topic okay i told you that okay obviously was asking me a question so what he was asking that if i want to listen to multiple events okay so if i have one listener and it wants to listen to multiple events so how can i do that so first of all if you are using event listener it is possible for sure but yesterday we have we have created one more project the big bang theory one and here also we got some listeners okay here also we changed the code yesterday right but but you know what if you are if you are creating a listener by implementing the interface the listener interface then it will be hard to do that because you know imagine if you are implementing what is that application listener and let's say if you want to listen to an event in this case transaction failure event then what you supposed to do here let me do comment this you have to override the method okay and inside this method you have to do this similar work i have discussed about this yesterday so maybe i'll just uncomment this three line okay so this is how i used to do the things right control shift or to import the things and there you go transaction failure event okay it is not a application event so i have to extend to the i can i have to extend this class to application event because the listeners i mean the listeners if you i have shown you the type is always exchanged to application events so if your class is not extending to application event then this will give you compilation error but anyhow the point that i want to state you that if you are creating your listeners just like this you have to override this particular method right and inside this method you have to write your code but in this case you have to i think that you cannot if you want this listener to listen multiple events you cannot do that okay how will be doing it this is one interface can you can you do control c again and do comma and ctrl v again so this is not possible right so the way to do that to the way to listen to multiple events let's say you want to listen to transaction failure event and let's say xyz event okay together let's say you have two different uh you know even to listen then this is not possible this is this is an impossible way to do it so maybe the way we can do it let me uncomment the code right here okay and do a ctrl shift o to import this event listener so you cannot do you cannot pass one more argument just right here but what you can do is in the event listener if i will control space and if i will do control space you can see it is taking a number of classes okay and that's an array so you can actually define your events just right here for an example transaction failure event dot class okay let's say if you have some other event let's say xyz event then do comma and do x y z event dot class that's that will work okay i don't have any xyz event right now but you know this particular thing you can do but the only problem with this approach is you cannot capture the object you cannot capture the object of transaction failure event right now okay because right now this event you can see i'm just capturing the get name from the event i'm captured capturing the amount from the event and this event is getting triggered in my publisher right here i'm creating that object by initializing the data with the name and amount okay whatever the data we are having here with that data only i'm creating an event and if i want to have a hold of that event object i will not be able to do it and i will not be able to capture the details about the event if i'll go with this particular approach okay so yes if you want this event to be triggered for multiple uh this particular method to be triggered for multiple events you can do something just like this by mentioning different classes but you know i will not be going with this i'll be going with the previous approach that i have told you because i want the name and amount i want to inform the user that well user you have sent you have done 10 transaction but the transaction that you have done for the amount 200 is not uh not gone through and that is the transaction which is unsuccessful so you know what this is one of the best offers i believe because you'll have you will have the hold of the event and you can do anything with that particular event object right perfect so these things is right now also you want multiple events so you will get another method and yes yes that is also possible that is also possible let's say if you want to that is now that is a different thing okay understand the thing right now let's say i have couple of event i have two different event let me go to the previous class that we have created yesterday maybe that will make more sense yesterday if i'll go to the events we have two events right so if i want to create a method and that method should listen to all the two events for an example comedy circus event and big bang theory event i want to create a method let's say in the listener i want to create a method let's say class and i'll be creating let's say demo uh listener where i said demo listeners okay anyhow let it be demo listeners and imagine i have a method here called public void start okay and i want this start method to be get executed when any kind of event is getting triggered either at a comedy circus event or big bang theory event so i can just do what one method event listener okay and i can define here let's say classes then this is gonna take an array and i'll say bigbang theory event dot class and i can do a comma and i can say committee circus event dot class so what does it mean so any of this event occurs right now the code that i have inside this method will be executed if you have that kind of scenario you can pass multiple events right here okay so any one of the event which is getting triggered this start method will be invocated making sense okay what i mean to say i guess i'm gonna learn but what but what you are saying is a different thing what you are saying is that avilash can i create multiple listeners just like this for an example i don't need this okay if you have only one method with two listeners just like you yeah i'll tell you the best way to do it the best way to do it the best way to do it for an example i have a start method let's start one method this is listening to a big bang theory event event here you'll be able to here you'll be tracking the event specific specific to big bang theory you can create another method ctrl c ctrl b and here you can track another event let's start i mean start to and here you want to track list there what we call that committee circus event right so right now there are a couple of method this method will be also triggered this method will be also triggered okay so when i mean this method will be triggered whenever the big bang theory event is getting triggered and this method will be invocated when committee circus event will be triggered making sense okay what i mean to say so one inside one class only inside one class only one listener class only you are defining multiple listeners okay okay all right but but you know i'll be just giving you some more examples let me close all the files okay and now before i go to the next thing till now we're okay guys we we're okay with everything okay all right okay so now let's go for the next thing okay maybe i'll just open my slide and let me tell you a few things right now okay it's very very easy thing right now you guys understand how to create your custom events and things like that now think like you know if you if you see your framework your spring framework your spring framework published a lot of different events and use it internally for example whenever your application starts whenever your application closed whenever your application context stopped whenever your application context whenever i say application i mean by i mean application context so whenever your application contacts start stop close refreshed every time it published an event and those events if you want you can actually use them but those events basically used internally by the framework okay but we can use it for our for our own convenience for an example yesterday i was saying if anyone any one of you is learning spring security whenever a login felt it published an event whenever a login is successful it publishes an event so that if you want to know okay now the login failed for the user then you just i know you it is my listener and the listener will be listening listening to that particular event and i want to do something when the login is felt i want to do something when the login is successful you can also do that by using the spring default events okay let me give you an example okay first event that i'm gonna uh i mean tell you about is called a context refreshed event okay so the context refreshed event is an event which get triggered when the application context is initialized or refreshed okay i will not go into the bin life cycle right now but i think most of you understand the bin life cycle you know that we can start the container we can stop the container we can close the container we can refresh the container if not there is a bin lifecycle video in the spring core lesson on youtube go there and follow it i will not go in depth of uh you know the application context events oh okay the application context life cycle but i have sent you the link right one second i've sent you a link right here if you'll go into that link okay this link will tell you one second i'll just get you something same lazily initialized event let's say application context context can i copy this name i just want to show you something maybe if you want to read more about this there is a way to do it in the documentation okay so refresh spelling is also wrong oh yeah there we go so you can see there are a lot of different events that we have in spring framework called application context uh context refreshed event context started event context stopped event contest close event uh i mean request handler event this is this i mean this is a web specific event and this is also a web specific event let's not go into that whenever a container start right there is the event get triggered by spring called context started event and whenever maybe i'll go back to my i mean there are a lot of description you can read if you understand the bin life cycle properly you'll be able to understand this as well but let me simplify this one is called context refresh event my um there are a lot of typo here please ignore that and context refresh event basically trigger when the application context is initialized or when you're whenever your container is initialized or refreshed one more event is they are called context started event when your application is started whenever your whenever your application context is started by using the start method then this particular event will trigger okay similarly whenever you are going to stop your application context manually by using the stop method the context stopped event will be triggered okay that's what i have written here an event is published when application context is stopped by using the stop method okay and similarly if you're gonna see one more event called context closed event so this event will be triggered whenever you are closing the application context by using the close method okay and similarly i mean the other classes that we we have we'll be talking about it in the spring mvc or in our web specific lessons right now this is how you have to create the you know listeners you know that how to create the listener you have to just write context refreshed event context started event context stopped even these are the events in spring and you have to use application listener and this application listener listener interface should be implemented by the listener class that you want to create and you have to override that method and then you can able to track those particular events for example let me just take one event from here let's say context refreshed event and let me try to tell you how you can deal with that for an example i'll be going to eclipse and i'll be going to my spring event002 so right now whenever your context will be refreshed let me go to source main java i'll go to the listener imagine i'll be creating a new listener and this listener is going to listen to an event okay this is going to be a new listener called application context refreshed um listener okay so now this listener what i want to do here whenever my application context is getting refreshed i want this list not to listen to that okay and for that i have told you that whenever my application context is getting initialized where my where my application context is getting initialized if i'll go to the config file another config file if i'll go to the app.java whenever i'm loading my application application context context sequel to new annotation config application context and i'm using app upconfig.class so here i'm loading my file and this one is basically uh kicking in um my beans like you know it is just instantiating my beans making them single turn okay and starting the container or initializing the container whenever my application context is getting initialized one um i mean it is going to trigger an event okay and that event is called context refreshed event control safety context refreshed event and if you'll be going to this event you will be able to understand this right now see that this context refresh event is exchanged to application context event so any any event which is getting published by your application context okay those are uh those are exchanged to an event or in class called application context event so you know that if you want to create any event in spring you have to extend to a class called application event but this is called application context event because they have created one more layer of abstraction if you'll be going to this application context event you'll see that it extends to application event okay so whenever we have created our event for next for an example whenever we created this is a listener whenever we have created transaction failure event i i mean i did not tell you to um you know extend to application event class but yesterday whenever we are creating event we used to exchange to a class abstract class called application event but we did not extend to a class called application context event why is that because whenever we are creating our event this is our custom event this is not application context specific event right these are not containers event this is our custom event that's why i am you know extending to application event but whenever you look at the framework for an example if i'll remove this and if i'll close this and if i if i'll go to the framework code let's say this is one event that they have created that is called control safety context refreshed event this is extending to one more uh class called application context event application context event is the event that means whatever the event that your application context is triggering those are extending to application context event and of course this is an abstract class again and that extends to application event okay so right now now we don't need to understand that much thing so we need to just understand there is a event called context refresh event and this event is raised when the application context gets initialized or refreshed okay right now our application context is getting initialized or refreshed just right here so i believe that if i will be listening to that event so whenever my application contacts will be initialized my contacts refreshed will be automatically triggered or my my my application context what is that event again go to go there context refresh event this event will be automatically triggered by my container and if that event is published if i have a listener then i can listen to this particular event isn't it so let's go there to the listener class and let's try to listen to one event i will go with the traditional approach right now uh just to make sure that i'm using here and there i'm using all the things so that you will be acquainted with all sort of code not only the annotation best approach so right now here i'll be using up i mean what is that application listener okay because i am creating my own listener that's called the application contact context refreshed listener and here i want to listen to an event called this one context refreshed event so let's do that context started event stopped event refreshed even i'll be overriding the method i'll be overriding the unimplemented method ctrl shift o and here i can just do something let's say this out let's say application ini cia lies okay application initialized or refreshed okay so this event will be triggered when the application will be initialized or refreshed at that time this method will be invoked and i'll be see this thing or see this method you know um triggering and i'll be even even if i want to log if if i want to log the event okay event dot get class or i can directly print the event you can see that a context refreshed event will be triggered and the object will be printed to the console so can i do one thing can i go to my after java imagine that this will refresh the i mean it will refresh or start the container so if i'll do run and you will see that what is that i have not made it a container oh sorry i have not made it a listener or i have not made it a component so right now this list not need to be registered with the component so that whenever my application context is getting started it is going to notify the listener only in case it is a component only in case it is maintained by the application context if you have a component and that component is not getting managed by your application container or application context then it will not notify to this particular listener and as right now it is a component i believe application context will notify to this listener that hey i have triggered a context refreshed event i mean you just go ahead and if you if anybody is listening to that please you know do whatever you want to do and that's what we are doing right now let me run this application again and there you go you can see at first our method get called this particular listener get triggered and it is saying application initialized or refreshed and then the next thing you can see over here it is printing the context refreshed event okay getting it what i'm trying to say what i'm trying to say is that when you're whenever your application context start right it publishes some event maybe i can show you something for an example i can manually start my container by doing a context dot start okay i cannot do context or start maybe i can copy this and paste it over here because maybe in this class i'm in the application context there's an interface then the start method is not available and now you can see i have a stack method coming from abstract application context okay now if you'll be doing this stat right it will be going into the start method okay look at the code here so they are saying whenever you're starting or whenever you are starting your container by using the stat method it is just doing whatever it is needs to do is calling the start method uh i mean it is it is calling some life cycle processor start method that's fine because start method is a is a life cycle method you can see implementation of lifecycle interface but one thing you can see it is publishing an event and what is that event the event is new context started event so right now whenever the start method is getting called the application context is firing or publishing an event called context started event and you can right now create and you know create an listener which is can listen to this particular event and if you want to do something when the application is started you can do that particular thing inside the listener isn't it and what is this akil was asking about this what is what is this this the this is basically so if you go to the application started event this is an event you can see this this extends to application context event and of course the application context event is having a is an abstract class and it is having an constructor and this constructor if we have then you know it is i mean we have to override this particular constructor and it is also calling the super constructor what is the super class application event it has a you know constructor here which takes a source which is which is an object in type so basically this source is from where this uh from where this uh event is getting triggered right now who has triggered our event our event has been triggered by the container who is our container that is our application context and who is that application context in our case if i'll be going to update java annotation config application context is our container so right now it will be going to the start method i'm doing context or start so who is the current class annotation config application context is the current class and here i'm using start method on top of this particular object so if i'll be going to the stack method here i'm saying new context started event so i'll be triggering the event by passing in some you know the current object the current object is who who has triggered it who has triggered it who who is who has originated this particular event that is your application context okay so you'll be getting application context object just right here if you're gonna debug it maybe i'm gonna debug it but first let me capture this event okay context started event let me close this let's say let's say i am manually starting my application doing a start if i manually starting my application it will trigger an event and i want to listen to that event so right now what is the event i want to listen to this is this event okay now let's not do like this i'll just do what i'll remove all these things and maybe i'll just make it a event listener okay i don't need to write overwrite okay and this guy will listen to one event and that event is called context refreshed event that's fine now one more event is there called context started event let's say let me handle that public void handle context started event okay and now i want to handle this event when this event is getting triggered so when the context started event is getting triggered handle that event so i'll just have one more line ctrl c ctrl v application initialized with start method make sure this event will only be fired whenever you're initializing your application by using the start method ctrl shift o to have the import okay and i'm just printing in the event just right here now if you're gonna see your app.java here i'm using context.start the start method will publish an event that event is context started event okay and right now if i'll be going to my listener and in this listener i'm listening to the context started event and whenever that happens you know this particular line will be executed and i'll make it an event listener okay and i will remove this okay so uh there you go right so now if i'm gonna run this okay you'll be saying first application initialized or refreshed then our context refreshed event is getting triggered then application is initialized with the start method and that's why context started event is getting printed because i'm listening to that event by using a listener as simple as that is that making sense so far yes all right can i hear it from everyone yes sir but the ordering is going to work on these listeners also yes and basically i'll tell you right how can you basically run it asynchronously okay this is just the basic thing here so i'm just making you familiar with the basics okay uh i mean you can still order these listeners right if you want to order these listeners okay yesterday when you created a big bang theory event so in that what was this was that was this c cafe source on still it wasn't cafe was the publisher yesterday yeah but what was that this uh um value this last that will be also application context okay you are getting whenever you get confused with that maybe i'll be telling you uh tomorrow or day after tomorrow okay see a an exception can trigger an event can be published from any kind of layer maybe it is a service layer maybe it is a dow layer maybe it is a controller layer so who is originating that particular you know that particular what we call uh event who is publishing that event controller is publishing uh your service is publishing your diy is publishing okay and accordingly the source will be very and in this case who is who is the source after java annotation config application context is gonna be my source because that's where i mean if you're gonna go i don't want to go into pretty depth into that because i want to do some other thing but if i'll be putting a breakpoint here in the line number one four two two my flow will stop here in the stack method so if i'll be going to after java and start it in debug mode okay um so i also got some other break point okay because i want to teach you this thing as well okay there you go so now you got a stat method okay the start method is right now uh now my flow has now my flow has stopped in the start method and if you have of hover on this see that this is annotation config application context okay so this means your i mean who has called that i mean what is your current object who is i mean you have called this method start method by using which class object context object who is context this that your current object right getting it right right yes so now let me resume this okay so i think i have covered a whole lot of things and just just like that guys you have multiple things maybe you can test this out if i'll go to my slide i have told you we have also called stopped event the stopped event will be triggered whenever you are you are you are you know stopping your application context by using the stop method okay and also we also have the close method and the close will be triggered whenever you are stopping your application by using the close method for for an example context closed event and context stopped event you guys can do the experimentation but i'll just let you know one thing you can you are starting the container here maybe once your work is done you can do context dot close or first let me do context dot stop or you can do context dot close don't don't ask me the difference right now i won't be going into the life cycle method was though was the youtube video that i have published that that is having around one and a half hour of content on the spring core series and that will clarify this thing stopping closing what is the exact difference maybe you can go and look at the documentation link that i have sent you okay there you that there you'll find also more idea about this but right now the thing is that whenever the stop will be called context stop the event will be triggered ctrl c this go to your listener so if you want to create one more listener for this stop okay and whenever go to after java whenever close will be called okay it is gonna publish what it is registered it is shutting down the things so where is that code shutdown hook i have told you about the shutdown hook what is the shutdown hook and the things like that but do close where they're publishing the event yeah there you go context closed event okay so whenever the close will be called the whenever the close method will be called another event will be published that will that is context closed event okay so right now maybe you can just copy the listener ctrl c for context stopped event context started event i want to listen to something called context stop event and i want to hear this event context stop event and let's say application is stopped using stop method okay at that time only this particular event will be fired and one more time let me copy this ctrl c ctrl v8 over here and this in this case this is going to be context close event okay and this one will be publishing i mean whenever your application will be uh you know closed by using the close method okay this event will be fired okay and i'm also publishing i'm just pasting the event event references right here control s i'll go to after java control shift o here i am stopping here i am closing so whenever stopping the stop i mean the context stop event will be fired and whenever i'm closing the context closed event will be fired and i'm hearing all those events just right now so if i want to do any kind of thing during the context of or during the context close i will be able to do that okay run this look at the thing application is stopped using the stop method context stopped event is getting called application is stopped using the close method and context closed event is getting called making sense this thing what is the what is the default events that we have inside the framework and all these events are getting used by the framework internally we don't have to worry much about it i mean we don't need this i never used it in my real time but in spring there are few spring boot events are there what what we use it in real time and also i'm gonna tell you about that but yes that if you want to do something during the startup you may get a requirement at any time okay let okay whenever the context is refreshed i want that event to be i want something to do and at that time how you will be know when the context is getting refreshed but whenever the context is getting refreshed spring is making it easy for you by parting this event isn't it and right now if you once captured the event you can do whatever you want to do right over here isn't it we're on the same page till now obviously which class these events are published published yeah very nice question okay that class name is that is a i mean that is also there in the documentation you can see very nice question they asked this question in the interview okay you can see in the context refreshed event okay what they are saying published when the application context is initialized or refreshed okay and there is the class is one second configurable application context okay so configurable application context which i mean they want to say publish the application context for the started event context started event published when the application context is started by using the start method on the configurable application context interface let's go there and check give a little more attention to this maybe you'll get this thing so it will go to your start method okay so look at there where you are right now the start method is there in which class okay let me zoom this in this is inside a class called abstract application context which is implementing configurable application context isn't it uh abhishek so we are inside the abstract application context but anyhow it is implementing that so anyhow we should have the stack method there or we don't have the start method here but anyhow this um this interface right now is the super of that right so same thing yeah i understood yeah for this configuration contest only is publishing yes yes yeah so all the start uh rephrase and all these things they i mean in the documentation this particular interface name is there so i think everything is clear let's do the last thing which is going to be an interesting part and it's going to be something related to trade what is the time right now i cannot see my time here okay 9 10. can i take 10 15 more minutes really you are okay yeah yeah all right oh a kill sleeping 11 40. so you can study it till 2 a.m in the morning anyhow you know for me it's not a problem i have a meeting later in the day okay okay all right anyhow you're giving interviews so this will help at least for that sex study right now that's that should be the motivation okay all right okay and right now the next thing that i'll be doing i think i made you understand all these things i believe so i made you understand i don't know what you guys are thinking so so okay so what we have spring event i have told you about the context refreshed event i have told you about the context started event this will trigger whenever the start method uh will be used to start the application context context top context of the event will be triggered whenever you are stopping the application by using the stop event context closed event will be triggered whenever you are closing the application context by using the close event context closed event um and this is the way to define all your events and we have defined all these things by using add event listener annotation but also you can use the traditional approach by implementing the application listener interface okay and also i have told about the defining multiple listeners you can define multiple listeners inside a single class by using at event listener annotation and this is the way to do it by writing multiple event listener and you can do that together right here the one more thing we can also listen to multiple events the question that obviously asked in the start of this meeting we can we can do that by using at event listener and we can specify multiple number of listeners by using the class array that we have inside this annotation and the pros and cons the cons the only con con there is if you are not for an example if i'll be going there the con is if you're going to this listener right if you are defining this event not here in case you are defining the event just right here okay the only con is sorry it's a class okay the only con of this is you are not going to have a hold of that particular object just right here inside the method okay so that is the con that's why i have written what's the what's the con of that okay so you will you will not be able to have a hold of that particular object just the way you have the hold of this particular object just right here okay so let me go to the next thing exception due to event listener i have told you you have to define something inside the parameter of the event listener method and if you are not defining something right there let's say if i'm not defining this particular thing then obviously it is going to trigger an exception for you okay so you have to you have to be very sure whenever you are defining a method with the event listener with the event listener annotation you have to give the parameter if you're not giving it it is gonna get you an exception okay so we cannot define an event listener without an event as the argument okay now let's go to the s sync versus sync and before i before i go to sync versus async let's see the advantages and right now you guys understand all these things before you see the advantages can you guys tell me what what do you what do you understand about the event listeners and publisher how do you think it's gonna improving um the different stuff for different aspect i mean why should we use event and why should we use listeners why should we use publisher what it is doing and what kind of uh problem it is shouting out any idea anyone it's notifying the users when some yes notifying whom obviously notifying the listeners right okay and any other thing dependency injection perfect the dependency means here i want to say that the tight coupling the tight coupling is not there between the publisher and the listeners publisher is just publishing the message publisher is just publishing the event and listener or subscribers are tracking those event there is no direct dependency or there is no direct coupling or i mean they are loosely coupled the publisher and the listeners are loosely coupled and there is no relations or there is no code dependencies there okay in between publishers and listeners okay and uh i mean they they are avoiding the tight coupling which is a very nice thing okay and that's the two or three things that i have written over here first of all just to make you remember about this thing this eventing stuff used observer design pattern i don't know if you know the observer design pattern or not but is internally use the observer design pattern okay and our components are loosely coupled as right now sandhya said no tight coupling between the publisher and subscribers and this opponent way to extend our application by adding and removing subscribers without affecting other subscribers so there is no dependency i am assuming that there is no dependency between two listeners or two subscribers subscribers means listeners only if there is no dependency in between two listeners then you can easily add and add and remove listeners the way you want or the way your client directed you isn't it so these are the advantages so remember one thing is that i am saying that there is no coupling there is there is no tight coupling between the publisher and subscribers but the way the code has written in between i mean uh in in behind the behind the scene the way spring has written the framework code well there might be some dependency but there are not direct dependency so it is still correct to say that there is no tight coupling between the publisher and subscriber and we are not writing any subscriber code or the listener code inside our publisher which is our gpa.java for an example this is what publishing and message for me right now but the moment is triggering the message the moment it is publishing the message all the listeners are by default getting notified by the application context okay so till now we are good yes no all right one last thing okay i'll wrap it up pretty soon one last thing now let's come to the exciting part okay the sink or s think part okay i'll just do one thing and i'll just let you to do one thing okay imagine a scenario where is my gpa java this is my publisher all right it is publishing an event called transaction failure event i have a listener i have a listener called send email listener let me go to that okay and right now let me remove all the ordering i don't need any ordering okay control s ctrl s run this application again okay there you go so sending email then sending email then sending sms sending sms i have couple of listeners just right here now imagine in one of the listener let's say in the email listener there is some code like this there is there is some heavy duty that we are performing just right here inside the start method okay so this is a listener because it's the event listener and imagine i'm doing what i have i have a thread.slip here okay i'm knowingly just stopping the application for a moment just to showing you something so thread dot slip and i'm making it sleeve for 3000 millisecond okay and obviously this is gonna get me an exception i have to handle that okay and imagine i'm doing a thread.slip just right here and maybe after that i'm gonna copy this cut it and paste it over here okay knowingly imagine whenever your send as sent email listener is listening to the event which is transaction failure event let's set that event triggered and now the flow is coming to the try block and i am stopping my application or i'm doing something imagine i'm doing something or i'm doing some a lot of work which is taking a lot of lot of time imagine 3000 millisecond it is taking okay and now at that time what i want to let you know that this send email listener as long as it is not going to invoke this particular method your application will go in hold okay all your listeners are getting used or getting getting triggered synchronously okay what do you mean by synchronous what do you mean by synchronous in threading one thread will wait for another thread to completely exactly exactly so your by default your listeners are one one single threaded only okay your application will work in a single trade okay one thread will be there and your application will be running in that thread only and let's say you are doing any work just right here and till the time this work is not finished the other listener will not get to work will not do their work for an example send sms listener should wait till the send email listener to complete this task okay and if the send email listener is completing this tax after 20 minutes this particular thing has to wait and all the other listener also has to wait and your application will go in a hold okay for an example right now i'll be just showing you and showing an example just right here maybe i'll just write another sis out just be for this and let's say um send email okay i'm inside the send email listener okay send email okay let me copy this send email listener inside start method okay and i'm also going to paste this just right here after the end after the catch where i'll be pasting it controller ctrl shift f let's let me paste it just right here if i can okay so here i'll say start okay starting okay and i'll say end okay methodology method starting method invocation starting method invocation end okay so right now you will you'll see that if i'm gonna start my application okay now see sending email uh inside start method method starting my application went into a hold right let me make it four thousand millisecond control s and if i'm gonna run this see it is gonna stop the execution when the email sender method is starting it is stopping the execution and then once is complete the execution right here after that only the sending sms is doing it work the other things are doing it work and also the stop method is getting an invocated and the close method is getting invocated so that means my application is going in a hold okay if i have 200 different listeners listening to an event then obviously there will be a hold and you know my application will be date and obviously it's going to take a whole lot of time to process a single work so i do not want i do not want this listener to be synchronously listening to an event okay rather what we can do we can make it sync we can make it asynchronous right so that if this is doing it work it it is doing it independently and a different thread will be there to do a another work of a listener if there are multiple listener will have multiple thread and all those threads will be work independently and will be listening to that event okay and in that way our application will behave faster right now see that whenever i'm running my application it is going in a hold right it is going in a hold for some time and then once that work is done once this method is method invocation is done for the send email listener stat method then only the other thing are getting worked or i mean they they are working after that okay so how to make it asynchronous okay the problem you guys can understand what i'm trying to explain here okay yes okay um all right okay so now let me tell you one more thing how to make it asynchronous to uh just to make to make it asynchronous i'll be telling you one thing listen to this very carefully i'll go for my app.java okay i'll be going to be my uh spring demo where is my there are so many after java i will go to the update java right here okay so i am using application config annotation config application context can i open the type hierarchy okay right now you can see we have i mean this is the type of that this is extending or implementing to this this is extending or implementing to this to this then the superior object right there is something called abstract application context if i'll be going into that there is a method okay and that method is something i'll be letting you know about this i have just opened that method and i have just seen that there are some changes in the new version and that method is there you go init application event multi multicaster okay so what is this method is doing just understand it guys right now i'm gonna show you some code everyone can understand it okay so let's try to understand like you know what we have to do what we supposed to do and what the documentation is saying so whenever whenever we are starting our application and we are publishing an event and our listeners are listening to that event okay how that thing is happening that thing is getting managed by this guy right this method called init application event multitasker initialize the application event multicaster sorry multitask not multitasker multicaster okay so what this method does basically whenever you start your application it is gonna look a particular bean inside your application context you can see this method is doing what it is setting up the bin factory you know right application context and bean factory bean factory is something where where all the beans will be there just like your application context it is going to get all the beans and inside that beans that you have inside the application it is going to look for a particular bean called this bin factory dot contains local bean and it is looking for a particular bean with this constant what is this constraint click over here it is looking for this particular bean sorry whispering go back multicaster bean name okay this bin okay it is looking for a bean inside the application that well this being the developer has configured or not okay and the bean name is application event multicaster okay if this application event multicaster bin is there inside your application context then it is going to make it multi-threaded okay and if this bean is not available what it will do let me go to that init application multicaster so there is a if clause and there is a else clause okay look at the code take your time okay no no hurry here just take your time and see the code i'm sure you'll be able to understand okay so it is looking for a bean inside the bean factory okay with a b name that b name file however over here what is that bin name the bin name is application event multicaster it is looking for that particular bean if the bin is available okay then it is creating a you know application event multicaster object so there is a object and obviously this is an interface let me copy this interface so we will make it even more simplified we'll try to understand this method for one last time let's say i'll open the brackets let's see that whether i make you understand this or not this is this won't be difficult okay so we just need to create a beam that's that's it so we have a bean this is an interface okay this is an interface now what they're saying we have to create a bin for this and the bin name will be what can i go back okay the they will by default search for a bean called application event multi caster so by default when your application gets started it will look for a bean called application event multicaster if that bean is not found in your application context then they'll do what it will straight away go to the else block and it will assign a new object there is an object called simple application event multicaster what is that what is that guy there is a root interface the one that i have told you application event multicaster there is a this is an interface and if i do control t on this there is a implementation to this particular class okay and that class is simple application event multicaster this is the implementation too let me copy this let me go there so this is an interface the implementation to this interface okay the implementation to this interface is this guy okay so right now this guy is going to we if we want to make it multi-threaded then we have to say to this particular class that hey we want our application to work asynchronously don't make it synchronize because by default this bean is making your application synchronized okay so what i have told you go back again first it will look the bean inside this if this bean is not available in this case we don't have any extra special been defined named application context application event multicaster so this bin is not defined so right now it is going into the else block and it is defining this application event multicaster what is this guy this guy is this guy is the one which is basically helping us to event publishing or for event publishing we are using this application event multicaster so to this application event multicaster to this particular class we have to tell it what is the type of the multi-cloud multicaster class that he had it has to use it has to use a synchronous one or a synchronous one so by default it will be going to that init method init application event multicaster here by default if we are not defining a beam this if clause will be skipped because we do not have that being inside our application context the flow will come to the else block and here to this particular beam a new simple application event multicaster beam will be assigned and i have told you what is this simple application event multicaster this is basically a basically an implementation of application event multicaster okay what is this application event multicaster and simple application even multicaster let's go to the application event multicaster first okay so let's see the documentation of this so it is saying that simple implementation of application event multicaster interface multicast all events to i mean to all register listener leaving it up to the listener okay so what is what it is doing basically basically you can understand if you're gonna list if you're gonna read this particular line okay so basically it is saying that you can you just you just take one minute and read this particular line it is gonna make a lot of a lot of sense to you that it is saying that if the default if you will go with the default implementation of simple application event multitasker bean then all your listeners will be work synchronously okay and the listener will be blocking the entire application it will be going with the default implementation whatever we have so far okay so right now what is the solution to that okay so so the solution is if i'll be calm down in the simple application event multicaster which is a default implementation right now to this particular bin there is a method there is a there is a variable called task executor and we have to set this task executed so when we have the set task executed method if i do ctrl o and go to set task executor you can see that right here that right now there are two type of uh this task executor okay it is going to take a executor i mean executor instance and there are two type one is called sync task executed and one is called simple async task executed so to this executor into i mean this set task executor by default is set with sync task executor so it is it is working with a synchronized i mean in a synchronized way but if you want to make it as synchronized then you have to make sure you have to create a bean you have to create a bean of simple application event multicaster you have to set this property and what what what is the value you have to set to this particular property and you have to set this particular property do control or do set task executor this executor should be a type of simple async task executor that's it you don't have to think about anything else okay if you so you can see executing all your listeners synchronously in in the calling thread in the calling thread only though all the threads will be you know executed synchronously but however if you want to make it asynchronous you can see however note that okay consider specifying the asynchronous task executor here to not to block the caller until all other listeners have executed if you want all the other listeners to be executed in parallel then make sure to set the tax executor this executor one to this particular instance and that's it now let's go and look for this particular thing again so we have a method called init application event multicaster this this particular method will be by default called and this particular method will be decide whether our b or whether our listeners or our listener bin will be executed synchronously or asynchronously whenever this method will be called first it will go to if and it will be checked whether at bin is defined with the name called application event multicaster if the bin is defined with this name it is going it is going to execute this particular code and with this application event multicaster which is going to which is going to help us to publish all the bean to this particular variable it is going to it is going to get a bean or it is going to initialize a bean with application event multicaster with this particular object it is i mean that particular object it is going to create and it is going to assign this to this particular beam and that object will work in an asynchronous way okay so what we have to do in that case we have to do ctrl c and we have to create a bin of this and with this we have a property and that property name is what if i'll be going to application even multicaster obviously this is a interface if i'll go to the implementation of this okay the implementation is simple application even multicaster it has a variable called task executor ctrl c this and you have to just make sure that this property you are setting it to what you are setting this property control or set task executed you will be using this particular object simple sync task executable click over here if you want you can read the documentation okay so it is going to set up the new thread for each task you can see over here implementation that fires off a new thread for each task executing it asynchronously so in our case we need this guy right simple async task executor copy that and just create the object of this and there you go i mean that that that's it you have to just set up a bin you have to set up this task executed and you have to initialize the task executor with this class that's it right and if you are not doing it by default what is happening go back go back again go back again okay if you are not doing this if you are not initializing your application event uh your application even multicaster with a synchronized object then by default it is just created is just creating a simple object of simple application event multicaster i have told you the same class the same class object only it is creating but this task executor is by default is set set to control or set task executed by default it is set to this sync task executed that's why the thread is getting blocked right now let's do that okay now now one thing i can show you uh if i will be going to the app.java right now whenever i'll be running my application in my send sms listener or in my send email listener i have a try block which is stopping my thread for 4000 millisecond and that's why you can see we do not set off any asynchronous stop over here so if i'll be going to uh going to this particular init application event multicaster if i'll put a breakpoint in eight zero five line number and if i put another breakpoint in the 80813 line number or in the 812 line number right now you can see that the flow will be going to here it will be coming to here not to here because we do not have any beam defined so if i'll be running this in uh in this debug mode i'll do no okay right now obviously we do not have any been defined in our application which does mean that which which uh means uh with which name is application event multicaster so if i do step over it this if block will be skipped see this if is skipped it came to the else block right right now a simple application event multicaster bean will be created so if i'll be doing step over if i'll be hover over here so the bean is which is defined to this is simple application event multicaster and if you're gonna see that particular property task executor i think it is gonna set after this okay now if i'll be hover over this okay see set task executor is not even set to this okay maybe some somewhere else it is setting that or something but right now we do not have any task executor set to this and obviously our flow came to the else block right so right now i want my flow come to here so that it will work asynchronous sorry asynchronously correct so now if i'll be resuming that the on the way to do that i'll be resume everything the way to do that i'll go to my after java and sorry on the after java app config and here i'll be creating a beam okay so what bin i'll be creating i'll be creating a public and the b name is going to be simple sorry application event multicaster you can also make this one so this is the interface okay and let's say making it async okay i'm just saying something and this one i'll be creating a object of simple application event multicaster simple application event multicaster application event multicaster is equal to can i do this equal to new simple application event multicaster and then i can just do application event multicaster dot set task executed and what is the type of the tax executor that will be using this guy simple async task executed copy this create an object of that guy new control space ctrl v and do a control shift or to import okay and at last i told you right this is the super class and this is the subclass now i'll do what return this object ctrl c ctrl v there we go right now i have a been defined let let's let's make it a bean okay and what is what is the name of that particular bin will be the name of that particular bean will be this by default it is going to look for the framework framework is going to look for whether this beam is defined in the application or not if it is defined it is going to execute this bin and if this bin will be executed in our application in that abstract class where is that okay right now can i open the hierarchy if i'll be going to that abstract application context and if i'll be going to that init method where is that okay right here this block should be execute okay first of all let me let me run it manually and let's see that what is the behavior of my application right now sorry one second let me drag this up let me go to after java let me start the application again and there you go it's quickly executed okay and right now see asynchronously right now it is working first see the method is starting here then the other things are getting executed as these things are running in a different trade and at last you know the sending email is happening and you know whatever the text i have logged there that is happening and it is just printing that the method is end let me run this again see that again first the other work has been done still it is sending the sms now it's sent to sms let's run it again see that start sending sms start over here is already inside the start method but other stuff had worked right now only that particular work once this that that work has been done that listener work has been done then only it is executing this particular thread okay this is how asynchronously i'm running this application so far we are on the same page or not and one more thing if i'm gonna run this application in debug mode right now okay i have that been created so right now it should go to this if clause i believe okay see it is it goes to that if block and accordingly our application event multicaster you know simple application event multicaster and the task executed is right now set to simple async task executor the one i set right now it is gonna work asynchronously for your application okay i have put another breakpoint in the stack method that time okay now the things are working fine are you guys understanding what i'm trying to explain here yes no i said the sleep yeah the sleep i have just i have just done the sleep let me close everything right now the sleep i have just defined there okay can i go to that email control shift r and my method name is email what is that star email send email listener okay so the reason why i have done this okay because i want to show you that if i'm making it sleep then only you guys can understand right it is going in hold let's say there are so many work there that you are doing you are not making your sleep right here you are doing so much work and that work is some heavy duty work and it is taking so much of time so it is going to block the other things to run isn't it it is going to block the other listener to run in the same time isn't it okay and the application will stop and other listeners will work right yeah exactly so it is working in a different thread right now it is working in a different thread let's say it is taking time okay no problem you take your time to execute this particular code because right now you are going into a sleep or you are doing some other work which is taking a lot of time that's fine you are working with a different thread and other listeners are not dependent to this particular trait and because this is working with a different thread so they all are working in different different thread so whenever you are running it it is doing it work it is doing it work right now that work is taking time and at last that work is getting done because this work these listeners whatever they are doing their work they're supposed not to wait for that listener isn't it getting it what i'm trying to say okay for an example this email should not the email is getting triggered here but why should the sms need to wait getting it why should the sms listener need to wait send sms listener why it should why it needs to wait okay you are sending the email the email is getting hold okay do i mean send the email in your i mean whenever that work will be done that email will be sent right right now the the thing is that my application is not stopping and you know the sending sms after that all the other listeners are working and then this one is getting i mean this one is getting triggered over here because that work has been done and all these things happen because you have created this particular bin if you are going to command this particular bin this thing will not work control jet if you're going to comment this one ctrl s and if you want to run this that will not work see it is gonna stop here only right and it will wait till that things to complete and once it is gonna send the sms and once this method end that only i mean then only all the other threads will you know uh go go on and all the other listener will invoked one by one making sense and instead of if you're not if you're not writing this bin maybe you're gonna try that in your own time i'm not going to take any other time maybe maybe you can do one thing let's say if you want this listener to run asynchronously you can also use an annotation called async okay and make sure this async one uh to to make this listener work asynchronously you have to go to offconfig.java and you also need to make enable sync okay then also i believe it is going to run asynchronously so you just take your time and you know i mean just experiment with this whether the things are working with the annotation if you're not defining the pin okay similar to the annotation at the red async you need to write the annotation over all the event listener methods yes i mean the one yeah you can write you can write it in all the event listener method because but if you want to avoid that we can simply create a bean shell yeah so this is the easiest work and that's why i have taken some time uh to explain you this thing okay okay so in this case we don't write android async no no it's not needed i'm just telling you you know if you want to um you know experiment with this guy there is an annotation also called sync and you can also use that and can try whether your listeners are working fine or not okay sorry guys i have taken so much time sorry about it okay so at last we are good how many of you are still here tell me i'm still here okay really again your voice is very low is that is that better yeah that's better so tell me really right now just you know how much you got from all these things i mean is it understandable yes it's understandable so everything clear so far we're okay with the event and listeners yes perfect then from the next class onwards i'll be just telling you about uh you know the spring security event listeners how to track authentication how to do user lockout right a lot of interesting things are there whenever some uh let's imagine someone is trying to log in the login is getting failed the account is getting locked how the account will be locking will be triggering will be triggering the event right the event will be triggered whenever there will be a held attempt and that event will be tracking we'll be tracking it for three times well the event is failed for three times that means this user is doing a failure attempt for three times right that means this is a wrong guy or this is a bot okay or he's just trying to do some attack on the account okay so we'll do what we'll track that event and accordingly after three attempt we will block it okay we'll block that user to access the account okay we can do all these things we can use uh event in spring boot while where application is getting started we want to do something there is an event we can trigger that event and accordingly we can have a listener which can listen to that event and accordingly when our application is getting started we can do something right cool lot of interesting things obviously no questions yeah i'm gonna ask okay did i bore you no no i don't know at least we're from same state you can tell me in case i have heard you okay sandhya sandhya you are good sandhya there okay perfect okay ravi is still there or you left for your office ravi i know you have a call at 9 30 or something okay anyone else i have any questions karthik or anyone okay you missed okay okay okay maybe your office calls and all right that's fine okay yeah felix you are there is for what's time for you felix it is three o'clock in the morning i joined it but i mean it's 11 for me or 10 for me so for you it's gonna be uh four hours late right okay okay but it's too late for akil i know willie is manageable maybe two hours late than ocul so that's fine and we guys are okay so there you go good night good day good evening good afternoon whatever i'll see you guys tomorrow bye bye okay thank you bye bye thank you bye [Music] you
Info
Channel: Selenium Express
Views: 2,801
Rating: undefined out of 5
Keywords: Spring Event, Async Listeners in Spring, Spring Events Master Class, Selenium Express, abhilash, Async
Id: TwJ2Zbk28QM
Channel Id: undefined
Length: 107min 5sec (6425 seconds)
Published: Wed Jul 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.