How to work with Spring Scheduler

Spring Framework provides a mechanism for scheduling purpose: Spring Scheduler. The tutorial will introduce about how to schedule tasks with Spring.

I. Technology for Spring Scheduler tutorial

– Java 1.8
– Maven: 3.3.9
– Editor: Spring Tool Suite – Version 3.7.3.RELEASE
– Spring Boot: Version: 3.8.0.RELEASE

II. Overview
1. Structure of project

spring-scheduler-structure-of-project

2. Step to Do

– Create Spring Boot project
– Create Create Scheduler Task
– Enable Spring Scheduler
– Run & Check result

III. Practices
1. Create Spring Boot project

Open Spring Tool Suite, on main menu, Choose File->New->Spring Starter Project.
Input needed information about project as below images:

spring-scheduler-input-project-info-2

Press Next button then press Finish. Spring Boot project is created.

2. Create Create Scheduler Task

Create a simple component class with 3 scheduled tasks defined by 3 methods:
– @Scheduled(fixedRate = 2000)
public void timeReport(): the method will be invoked each 2 seconds
– @Scheduled(initialDelay=1000, fixedRate = 2000)
public void initDelayTimeReport(): the method will be delay 1 second & will be invoked each 2 seconds after the first time
– @Scheduled(cron = “*/5 * * * * *”)
public void cronTimeReport(): using cron-expression for define a scheduled task that will be invoked for each 5 seconds.

– @Scheduled: An annotation that marks a method to be scheduled. Exactly one of the cron(), fixedDelay(), or fixedRate() attributes must be specified.

Code details:

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 2000)
    public void timeReport() {
        System.out.println("[TimeReport] Now: " + dateFormat.format(new Date()));
    }
    
    @Scheduled(initialDelay=1000, fixedRate = 2000)
    public void initDelayTimeReport() {
        System.out.println("[InitDelayTimeReport] Now: " + dateFormat.format(new Date()));
    }
    
    @Scheduled(cron = "*/5 * * * * *")
    public void cronTimeReport() {
        System.out.println("[CronTimeReport] Now: " + dateFormat.format(new Date()));
    }
    
}
3. Enable Spring Scheduler

Using @EnableScheduling in main Spring class: Enables Spring’s scheduled task execution capability, similar to functionality found in Spring’s XML namespace.

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);
	}
}
4. Run & Check result

– Compile sourcecode:
clean install

– Run project with Spring mode: Spring Boot App.
– Log:

[TimeReport] Now: 16:01:13
2016-08-21 16:01:13.938  INFO 5292 --- [           main] c.s.s.SpringSchedulerApplication         : Started SpringSchedulerApplication in 4.827 seconds (JVM running for 7.839)
[InitDelayTimeReport] Now: 16:01:14
[CronTimeReport] Now: 16:01:15
[TimeReport] Now: 16:01:15
[InitDelayTimeReport] Now: 16:01:16
[TimeReport] Now: 16:01:17
[InitDelayTimeReport] Now: 16:01:18
[TimeReport] Now: 16:01:19
[CronTimeReport] Now: 16:01:20
[InitDelayTimeReport] Now: 16:01:20
[TimeReport] Now: 16:01:21
[InitDelayTimeReport] Now: 16:01:22
[TimeReport] Now: 16:01:23
[InitDelayTimeReport] Now: 16:01:24
[CronTimeReport] Now: 16:01:25
[TimeReport] Now: 16:01:25
[InitDelayTimeReport] Now: 16:01:26
IV. Source code

Spring-Scheduler

0 0 votes
Article Rating
Subscribe
Notify of
guest
467 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments