70 - Spring Boot : How to run asynchronous code with Spring? | @Async | @EnableAsync

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends in this video we will see how to implement asynchronous execution of any method in spring pool asynchronous execution means that the method execution will not be part of the running thread then who will execute that code so that code execution will be taken care by the different thread why is this asynchronous code execution required so sometimes because of complex logic or some third party dependencies so instead of keep on waiting for the response we prefer to execute that code asynchronously don't worry at the end of this video you will get a clear understanding of the async let's implement create a springboot starter project now add a required dependencies like web and devtools and click on finish let's open the async demo application class and run it see it's working and running on it0 it's report now let's create a new class in class name let's say asynctask annotate this class as a component let's add a simple method with the void return type annotate this method as async here just add some simple message with thread detail so these class changes are done this application class we have to annotate with enable async now let's see how to call the test async method of the async task let's create a controller class and class name let's say async controller annotate this class as a rest controller as we alerted a sync task as a component so here we can create a variable for async task class and annotate that as an auto wired let's create one get endpoint just add some simple messages for start and end and at the end just return some message now in between of start and end call the test async method of async task and the code implementation is done now let's run and observe the output see server started let's open the internal browser and execute the get endpoint see it is working and if you observe the output then you can see like end message is printing immediately after the start message and the asynctask method execution is happening at the end or even you can see both method executed by a different thread i hope you got some idea about the async let's stop the server and enhance this code to some sort of real time example let's rename this async controller to notification controller and async task rename to email sender task here inside the notification controller let's create one private method which will give us the email addresses assume in real time scenario there could be a database call that will return the list of email addresses but here this method is capable of creating a list of emails based on the n value change the email center task method name from test async to send email for this send email method pass the email addresses list and to get n value let's read this from the path variable but before that let's implement the send email method and here just iterate the email addresses and inside the loop just sleep a thread for thousand milliseconds in real time scenario obviously mail sending part takes some time and replace this message with some other message i hope this logic you can easily understand now here to read the n value let's add endpoints array so if there is some value then use that as an n otherwise consider one add path variable annotation and to capture the actual value and that could be null so use optional pass this count to prepare email list method add some simple check like if value is there then use count otherwise one i hope you got some idea from this example see scenario is very simple let's run again and see it is working or not the server started without any error let's test the get endpoint see with no value it is sending only one mail asynchronously see now it is sending two mails so basically email sending or sending message is a time taking operation so the best way to execute that code asynchronously let's change the value to 20. i hope you got an idea about async like how to execute some piece of code synchronously and once the execution is done you can capture that response and store in the database and later you can read data from that table and show as part of notification like mail send successfully or like these many messages we send so this is the basic or you can say the simplest way to implement there are other things also we can do like what if the async method returns something or throws an exception in these cases we'll cover in coming video as you can see this is the 17th video so if you want to learn about the spring boot other concepts then please watch previous videos as well thanks for watching
Info
Channel: Almighty Java
Views: 14,545
Rating: undefined out of 5
Keywords: spring boot, spring, async, @async in spring boot, spring boot microservices, @async in spring boot example, spring async, async spring, @async annotation in spring boot, spring boot async, spring boot exception handling, async listeners in spring, transactional in spring boot, spring cloud, transactionality in spring boot, async query result in spring data jpa, spring framework, spring boot 2, spring boot assincrono, spring boot java, spring boot zuul
Id: LCGr7-ZgiIA
Channel Id: undefined
Length: 5min 23sec (323 seconds)
Published: Thu Dec 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.