@Async in Spring Boot | Code Debugger

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to code debugger in today's video we are going to cover what is asynchronous communication and how we can implement asynchronous communication in spring boot applications what is asynchronous communication and how many types of communications are there in java there are three types of message exchanging patterns or communication format is there in java one is synchronous asynchronous and fire and forget what is synchronous communication this is called as blocking way of programming that means one thread will wait until the execution completion of another thread okay so second question is what is asynchronous communication so this is called as non-blocking way of programming one thread will not wait until the another thread execution completion okay the third is what is fire and forget the fire and forget is called as also non-blocking way of programming this we can achieve using asynchronous communication okay so what is the difference between synchronous and asynchronous communication let's see there we are having four different different tasks in synchronous communication it will wait until the whole the process completion that means it will take 45 seconds to complete the whole job but in case of asynchronous communication it will take only 20 seconds to complete the whole job because the first thread will do its job and it will return the response to the user then internally another thread will continue with other tasks okay so our synchronous communication will go like whenever the tasks are independent to each other in that case we can go for asynchronous communication okay so let's go ahead and try to implement that in spring boot application so this is the normal springboard project i have imported into the intellij let's go to the form.xml and we'll see what are the dependencies i have added so if you see in our pom.xml i have added springboot starter web dependence okay so let's quickly go ahead and create the service class and controller class we'll see how we can implement the asynchronous communication in springboot so let me create one service package service then i will create one controller also okay so in service i will write one class called notification service okay in notification service let me annotate this with other service so here i will have one method which will send one sms to the customer okay so let me write the method public let's say void send text message okay so it will take one mobile number as a argument so inside that i will write some dummy logic to call the third party services and will send the sms from that server services okay so for now we are not calling any third party services we'll just write the dummy logic okay so here let's see i will wait for 10 second after that i will write some logic like sms sent successfully and we will call this method inside the controller class okay so let me wait for some time so let me call this method sleep sleep of two seconds i will call after that i will do your sees out this out of yeah that's it let me go to the controller let's create a controller notification controller okay so this controller we need to annotate with other rest controller let me write one method public string send message here it will take one mobile number so let me inject the service here at the right autoware so we injected the service here let's call the service class method inside the controller method notification service dot send message of mobile number will pass here okay so inside that we have written some dummy logic to call the third party services that's it we have written let me return here something dummy response like written message sent successfully okay so let me annotate this with another data get mapping slash send the url so let's start the server and we'll see what is the problem we are facing and why we should go for asynchronous communication okay so let's start the application is getting up i started an edurated report let me go to the browser since it is get mapping you can call directly from the browser localhost 8080 send right then we need to pass one parameter okay so let's hit if you see here third party is calling after two second and the browser is waiting until two second completion right this is called synchronous programming who is calling that service he has to wait until the process completion right but in this case this task is independent right so whenever we are sending any message that this task is independent task so what we can do we can make this asynchronous okay how we can make this asynchronous call spring has given one annotation called other async that annotation we can use let me use this so if you are using other async then automatically whenever a call will come to this method then another thread will create and he will call this services so let me print here the thread name also okay so in controller so let me give the thread name here thread.currentthread.getname so we have annotated here other async but we need to enable the async communication so we need to go to the main class and we need to write one annotation called enable async that's it string will do the default configuration it will do and it will enable the async communication for us let me start the server and we'll see what is the difference we are getting here yeah server started on edurated report let me go to the browser let me hit if you see here here we are getting immediately the response we are getting right with the head name is 800 ex ec one we are getting okay and if you go to the console here called third party services send text message and here the thread name is task 1. but here the thread name is efec one that means another thread created and that task is executed by another thread that's why you are getting the response immediately here okay if you see here the threads are getting changed every time but this is another thread and in console here we are getting another threads okay so this is called asynchronous communication this is the simple way we can enable the asynchronous communication let's say whenever we are doing any fire and forget type of communication here we are sending one message right after that we are not doing anything that means fair and forget communication we are doing here right with asynchronous communication so let's say here one exception will occur okay let me raise one exception so here number format exception will come right so in this case whether our program is capable to handle that exception or not will check okay let me run the server yeah server started an agility report let me check here whenever calling we are getting the sms sent successfully but internally we are getting some exceptions here if you see third party call but from third party we are getting some error message that error message we are not handling and we are throwing that directly to the spring so how we can manage these exceptions how we can handle this exception in our application whenever we are doing any asynchronous communication in that case also we should handle the exceptions so how we can do that for that spring has provided one class that is called asynchronous configurable support that we need to extend and we need to write our custom logic to handle these exceptions okay so let me create one configuration class and we we'll see how we can do the configurations okay we should annotate this with other configuration and this should be extends from async configure support and here we need to override two methods add rate override there are two methods we need to override first one get async executor second one the uncached exception handler okay here spring has provided the default configuration but we can write our own configurations also let me write my own configuration okay let me write another exception handler i async exception handler so asic exception handler should implement one interface that is called asic on current exception handler okay so here is the one method we need to override that one handle uncut exception it will pass when throwable here and the method name and the arguments it will pass here we log here let me annotate this without the red component okay so this you should inject inside the configuration class and we need to return okay so we have enabled our own configuration plus our own exception handler let's go ahead and see whether we are able to handle this exception or not okay let me start the server and we'll check application started on itunes report let's go to the browser and we'll hit okay let me check what is the extension we are getting thread pull not initiated we need to initiate this thread okay that means initialize you need to do let's start again okay started let me hit if you see here the thread name is exc1 message and successfully are getting and if you see here there is no exception but we are logging that exception okay call third party services are sync by thread one and we are logging the exception message and this message is coming from our custom exception handler if you see this is the c out you have written here right so from this it is coming
Info
Channel: Code Debugger
Views: 22,078
Rating: undefined out of 5
Keywords: async, asynchronous programming, @async annotation in spring boot, @async in spring boot example, @async in spring boot, spring boot tutorial, spring boot java, spring boot, code debugger, code debugger channel, Code Debugger, codeDebugger, exception handling in async
Id: K3XWqkseO2A
Channel Id: undefined
Length: 11min 51sec (711 seconds)
Published: Sun Sep 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.