Spring Scheduled Tasks – 3 Ways To Use Spring Scheduler
In this article, we will see how to run any task or a program using Spring Scheduled Tasks. As usual, we will demonstrate this with our favorite Spring Boot
Softwares used
- Spring Boot 1.5.9.RELEASE
- Java 8
- Maven
- Eclipse
Spring provides us with convenient @Scheduled annotation. Using this you can turn any methods of your class to a Spring Scheduled Tasks
Spring Scheduled Tasks Mandates
When scheduling tasks with Spring @Scheduled annotation you need to remember and follow a couple of things:
- The method you want to schedule a task should not accept any parameters
- The method should not return anything i.e. return type should be void
- You need to specify one of the methods from cron, fixed-rate or fixed delay to schedule your task.
Let’s dive into an actual example and see one by one each of the ways we can schedule a task.
Enabling Spring Scheduled Tasks
To enable the scheduling you need to let Spring know that with help of @EnableScheduling also on the class in which we add our scheduled method we need to annotate with @Component.
1 2 3 4 5 6 7 |
@SpringBootApplication @EnableScheduling public class ScheduledTasksApplication { public static void main(String[] args) { SpringApplication.run(ScheduledTasksApplication.class, args); } } |
1. Fixed Rate Scheduled Task
1 2 3 4 |
@Scheduled(fixedRate = 1000) public void scheduleFixedRateTask() { System.out.println("Fixed rate task - " + new Date()); } |
As shown in the snippet above you can schedule a task running after a certain interval. The interval is provided in milliseconds.
Now in the above example, we have hardcoded the interval. What if we want to externalize it? Well, we do have an option for that. Just define your interval as:
1 |
@Scheduled(fixedRateString = "${batch.fixedrate}") |
and define a property batch.fixedrate in your application properties file.
One important thing to note here is that in this method, the next execution of the task does now wait to check if earlier execution has finished or not.
2. Fixed Delay Scheduled Task
1 2 3 4 |
@Scheduled(fixedDelay = 1000) public void scheduleFixedDelayTask() { System.out.println("Fixed delay task - " + new Date()); } |
In this approach, the time between tow executions is fixed. The next execution waits to for first is completed its run.
To externalize the configuration you can use:
1 |
@Scheduled(fixedDelayString = "${batch.fixedDelay}") |
3. Scheduled Task with Initial Delay
In all the above examples, the task starts right after your application is up and running. If you want to run your task say after 5 minutes of application start what you would do? The Spring has the configuration for that as well.
1 2 3 4 |
@Scheduled(fixedDelay = 1000, initialDelay = 5000) public void scheduleInitialDelayTask() { System.out.println("Initial delay task - " + new Date()); } |
4. Cron Scheduled Tasks
Sometimes you can not achieve the fine-grained scheduling with the help of fixed-rate or delay. In such scenarios, you can use cron expression to schedule your task.
1 2 3 4 |
@Scheduled(cron = "*/10 * * * * *") public void scheduleTaskCronExpression() { System.out.println("Fixed delay task - " + new Date()); } |
The above task runs every 10 seconds after the application is up.
To achieve externalization use:
1 |
@Scheduled(cron = "${cron.expression}") |
Summary
In this article, we have seen and understood how we can leverage Spring scheduling capabilities. We have learned different ways to configure scheduled tasks.
Please feel free to comment or ask a question or two.
You may also like:
WebSocket Application With Spring Boot Step By Step – Source Code On GitHub
Simple and Easy way to connect MongoDB Atlas with Spring Boot
Steps to develop Spring Batch jobs using Spring Boot with example
Simplest and Easy way to Upload and Download Files in Java with Spring Boot
Hi, sometimes my scheduled task runs late even though the method clearly finished before the desired start. Any way to increase threads or what configuration options are available for this? Thanks.
how to achieve notification API call after receiving success response ?
Hi Khyati,
Sorry, I did not get your question. Can you elaborate on your requirement please.
How do ensure the scheduler running status?
Hi Vadivelan, I did not get your question. Can you share some more insights/details?