Spring Boot – How to Configure Scheduler with Cron

This article explains how to configure Schedule using cron task. Scheduling a task with Spring Boot with cron job 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 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.

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.

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 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


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *