In this video we are going to learn and understand the concept of scheduling and
How do we schedule jobs using Spring Boot?
So, What is scheduling?
Performing a task at regular intervals of time or at specified time intervals is called
Assume, you want to perform a job at a certain time and you are unable to be there at that time.
So what you do is schedule a job and it will get executed at that point of time.
Now, this job is just like a java method where the method gets invoked and performs certain task.
The challenge here is to perform this scheduling using spring boot. Let's see how can we achieve this.
First we shall create a spring boot starter project. Have a look at the pom.xml
We have just two classes. The first one is the normal one which is their for every Spring Boot application
We have a special annotation called @EnableScheduling.
@EnableScheduling ensures that a background task executor is created.
@ComponentScan tells Spring to look for other components, configurations, and services in the package, allowing it to find the controllers.
The next class is MyTasks.java. Here you have @Component.
We have created a Java method which gets invoked every 5 seconds and sends email to friends.
Although their is no email configuration done here, its only a representation
You can see @Scheduled The Scheduled annotation defines when a particular method runs. NOTE: This example uses fixedRate, which specifies
the interval between method invocations measured from the start time of each invocation
Lets run this program and see what happens..