Java World: Scheduling tasks with Spring Boot
It is not new that the Spring Boot ecosystem provides us with numerous facilities in the daily routines of developing software, part of Spring’s success has always been to abstract the complexity of very specific functions so that developers can better think about the complexities that surround the business rules of applications .
Among these practicalities, we can mention the possibility of making your code execute a certain routine in a predetermined period of time.
I am referring to Quartz Scheduler and the @Scheduled annotation that we will cover in this post.
This text can be more useful if you already have a previous knowledge of Java technology and want to use Spring Boot. I leave as a suggestion to use the website start.spring.io to set up the initial structure of your project with the necessary dependencies.
We are going to use the Spring Web and Quartz Scheduler dependencies.
If you already have a Java Maven project or a Java Gradle project and do not need to use start.spring.io, you can add the following dependencies…
After importing the project into your IDE and having the dependencies ready, create in the main package (the same as the class with main method) a new file called SchedulingTask.java.
Note: We will not create a new package to separate behaviors because it is a didactic example, when using this functionality in real projects, good practices indicate separating this file from the main package.
Your project directory structure should look like this:
Within the SchedulinTask class, create a void method called scheduleFutureTask() with the annotation of Quartz Scheduler, according to the model below:
But what do we have here? I explain it step by step.
Before the class declaration itself, we have two annotations, @Component and @EnableScheduling.
Component is an annotation that informs Spring that this class is part of its context and therefore will be able to make use of its facilities such as dependency injection and inversion of control.
EnableScheduling is the annotation that most interests us here, it will allow the use of the Quartz dependency that we insert in our pom.xml or build.gradle file, so it will allow us to schedule any code routine.
We created a variable of type long which is the simple calculation of 1000 milliseconds X 60, which in practice represents 1 minute.
In this method we have the @scheduled annotation with the fixedDelay property, where we insert our previously declared MINUT variable.
Our class can have N methods and not all of them need to execute pre-determined routines, so we use the scheduled annotation in the methods that will be called in the periods of time that we will determine.
In this example, our method uses the annotation followed by the fixedDelay property. This property specifies the interval between method invocations from the end of the task. The moment the called routine (in this case a simple println) is finished, Quartz starts counting the time again for this method to be called again.
In summary, we are telling the method:
Every 1 minute, run the print with the message “schedule ok!” and right after completing the print, count one more minute and repeat this operation.
We can still use the fixedRate property that will count the indicated interval (in this case, the MINUT variable) from the beginning of the routine that in this example is the print.
Depending on the need for use, it is worth studying which property is most suitable.
There is still the possibility to create patterns with more varied and personalized time intervals, for this it is worth taking a look at the crontab pattern that can be used with Quartz Scheduler.
Perfect, now that we have our class and our method that are responsible for calling a specific routine within a specific time interval, we need to make the initial call, the trigger that will make everything work.
For this we will use our main project class, the TaskApplication.java class and inside the main() method we will call our class and method with Quartz properties.
Now, run your code from the main class and follow your IDE’s console.
If everything went well, after build and initial execution of the application, every 1 minute the message “schedule ok!” must be printed.
If you want the complete source code for this example, just go to my github:
Thanks for reading!