This article explains how to configure a scheduler task with cron job expression using the annotation @Scheduled in Spring Boot. Scheduling a task with cron expression using @Scheduled and @EnableScheduling annotation is as simple as annotating a method with @Scheduled annotation, and giving cron expression as a parameter. This cron expression is used to determine when the task is going to run.
The annotation @Scheduler internally uses the TaskScheduler interface to schedule the method for execution in Spring Boot.The TaskScheduler interface is used by Spring Boot to schedule the annotated methods. As explained below, the creation of a scheduler using the cron task involves three steps.
Step 1 – @EnableScheduling annotation
The @EnableScheduling annotation is used to enable the scheduler. As shown below, the annotation @EnableScheduling should be added to the main method of the spring boot application. The annotation @EnableScheduling informs the spring boot application to initiate the scheduler within the program.
package com.yawintutor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SpringSchedulerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSchedulerApplication.class, args);
}
}
Step 2 – @Component annotation
The annotation @Component is used to declare as a scheduler component in the scheduler class. As shown below, the @Component annotation should be added to the spring boot application’s schedule class. The annotation @Component helps to load the class in start of the application. The scheduler is automatically configured and initiate while loading the class that is configured using the annotation @Component.
package com.yawintutor;
import org.springframework.stereotype.Component;
@Component
public class SchedulerCron {
}
Step 3 – @Scheduled annotation
The @Scheduled annotation is used to configure a scheduler task. This annotation with a cron parameter helps to create a cron-based scheduler for running. The cron job expression consist of six sequence of fields, second, minute, hour, day of month, month, day of week.
The annotation @Scheduled is using the TaskScheduler interface to schedule a method to execute based on cron expression. The @Scheduled method should not contain any method parameters and the method must be returned as void. The cron expression is added as a parameter for the annotation @Scheduled.
The following program shows a cron expression in the @Scheduled annotation.
package com.yawintutor;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class SchedulerCron {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
@Scheduled(cron = "0/5 * * ? * *")
public void task() {
System.out.println("Scheduler (cron expression) task with duration : " + sdf.format(new Date()));
}
}
Console Output log
The following console output indicates that the scheduler runs in cron expression every 5 seconds. Based on the cron expression, the method is called for the execution.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.1.RELEASE)
2019-12-03 04:25:43.538 INFO 17180 --- [ main] com.yawintutor.SpringSchedulerApplication : Starting SpringSchedulerApplication on banl1691b9157 with PID 17180 (D:/workspace/SpringScheduler/target/classes started by user in D:/STS/workspace/SpringScheduler)
2019-12-03 04:25:43.540 INFO 17180 --- [ main] com.yawintutor.SpringSchedulerApplication : No active profile set, falling back to default profiles: default
2019-12-03 04:25:43.934 INFO 17180 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2019-12-03 04:25:43.953 INFO 17180 --- [ main] com.yawintutor.SpringSchedulerApplication : Started SpringSchedulerApplication in 0.632 seconds (JVM running for 3.095)
Scheduler (cron expression) task with duration : 2019-12-03 04:25:45.001
Scheduler (cron expression) task with duration : 2019-12-03 04:25:50.004
Scheduler (cron expression) task with duration : 2019-12-03 04:25:55.006
Scheduler (cron expression) task with duration : 2019-12-03 04:26:00.004
Scheduler (cron expression) task with duration : 2019-12-03 04:26:05.005
Scheduler (cron expression) task with duration : 2019-12-03 04:26:10.002