How to use Spring Boot @Scheduled annotation for task scheduling
The @Scheduled
annotation is a feature in Spring Boot that allows developers to schedule tasks to run at specified intervals. This annotation is part of the Spring Framework’s task scheduling support and provides an easy way to schedule tasks in a Spring Boot application. In this post we will discuss @Scheduled Annotation for task scheduling.
The @Scheduled annotation can be used on methods that are intended to run at specific intervals. This annotation can be used in two different ways: fixed rate scheduling or cron expression scheduling.
Fixed rate scheduling
Fixed rate scheduling is used when you want to schedule a task to run at a specific rate, regardless of when the previous task was completed. To use fixed rate scheduling with the @Scheduled
annotation, you can specify the fixedRate
attribute, like this:
@Scheduled(fixedRate = 5000)
public void myTask() {
// This task will run every 5 seconds, regardless of when the previous task was completed.
}
In this example, the myTask()
method will be executed every 5 seconds.
Cron expression scheduling
Cron expression scheduling is used when you want to schedule a task to run at specific times or dates. Cron expressions are a powerful way to specify a schedule for your tasks. To use cron expression scheduling with the @Scheduled
annotation, you can specify the cron
attribute, like this:
@Scheduled(cron = "0 0/5 * * * *")
public void myTask() {
// This task will run every 5 minutes, on the 0th second of the minute.
}
In this example, the myTask()
method will be executed every 5 minutes, on the 0th second of the minute.
Using parameters with the @Scheduled
annotation
You can also use parameters with the @Scheduled
annotation to customize the scheduling behavior of your tasks. For example, you can use the initialDelay
parameter to specify a delay before the first execution of a task:
@Scheduled(fixedRate = 5000, initialDelay = 10000)
public void myTask() {
// This task will run every 5 seconds, with a 10-second delay before the first execution.
}
In this example, the myTask()
method will be executed every 5 seconds, with a 10-second delay before the first execution.
Another useful parameter is zone
, which allows you to specify the time zone for your task’s schedule:
@Scheduled(cron = "0 0 12 * * *", zone = "America/Los_Angeles")
public void myTask() {
// This task will run every day at 12:00 PM Pacific Time.
}
In this example, the myTask()
method will be executed every day at 12:00 PM Pacific Time.
The @Scheduled
annotation is a powerful feature in Spring Boot that allows developers to schedule tasks to run at specified intervals. This annotation can be used in two different ways: fixed rate scheduling or cron expression scheduling. You can also use parameters with the @Scheduled
annotation to customize the scheduling behavior of your tasks. With these tools, you can create complex scheduling patterns for your tasks and ensure that they run when and how you want them to.
In the last post we had discussed about How to use Spring Boot @EnableCaching Annotation for Data Caching. You can read that post here.
Happy Learning!
Subscribe to the BitsToGigs Newsletter