11. SpringBoot -- scheduled tasks

 

Development tools: STS

Code download link: https://github.com/theIndoorTrain/Springboot/tree/52ef6c0c805913db1e66ed18671c322e284233f0

Preface:

We talked about Quartz task scheduling before.

Now, let's talk about Spring's own task scheduling.

In the spring boot project, this super simple scheduling method will bring us convenience.

 

Task 1:

5 seconds later, once every 10 seconds, 2 seconds for each task;

1. Enable task scheduling in the startup class, plus @ EnableScheduling

 1 package com.xm;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 import org.springframework.boot.SpringApplication;
 7 import org.springframework.boot.autoconfigure.SpringBootApplication;
 8 import org.springframework.scheduling.annotation.EnableScheduling;
 9 
10 @EnableScheduling
11 @SpringBootApplication
12 public class Demo0061Application {
13 
14     public static void main(String[] args) {
15         16         SpringApplication.run(Demo0061Application.class, args);
       System.out.println("Start Job:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
17 } 18 }

2. Define tasks

 1 package com.xm.job;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 import org.springframework.scheduling.annotation.Scheduled;
 7 import org.springframework.stereotype.Component;
 8 
 9 @Component
10 public class HelloJob {
11     
12     @Scheduled(initialDelay=5000,fixedRate=10000)
13     public void run() {
14         System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
15         try {
16             Thread.sleep(2000);
17         } catch (InterruptedException e) {
18             // TODO Auto-generated catch block
19             e.printStackTrace();
20         }
21         System.out.println("hello world");
22     }
23 
24 }

3. Screenshot of operation result

4. analysis

1.initialDelay=5000: task start time is after 5s
2.fixedRate=10000: the task is completed once every 10 seconds
The task time here is less than fixedRate
Now let's change the task run time to 11s
       @Scheduled(initialDelay=5000,fixedRate=10000)
    public void run() {
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        try {
            Thread.sleep(11000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("hello world");
    }    
The operation result is:


Task two:


5 seconds later, the task is executed once every 10 seconds, and the task is executed once every 2 seconds;

1. Define tasks

    @Scheduled(initialDelay=5000,fixedDelay=10000)
    public void run() {
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        try {
            //Thread.sleep(11000);
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("hello world");
    }    

 

2. Screenshot of operation result:

Analysis:

Fixed delay: start timing after each task is completed


Task three:
Every 5 seconds from 16:00:00 to 16:21:59
1. Define tasks
 1     @Scheduled(cron="0/5 0-21 16 * * ?")
 2     public void run() {
 3         System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
 4         try {
 5             //Thread.sleep(11000);
 6             Thread.sleep(2000);
 7         } catch (InterruptedException e) {
 8             // TODO Auto-generated catch block
 9             e.printStackTrace();
10         }
11         System.out.println("hello world");
12     }

 

2. Screenshot of operation result:

 

 

 

3. analysis

Cron expression is applied here. Please click here for details. 3. Initial understanding of Cron expression

 


                               2018-07-16

Tags: Java Spring github SpringBoot

Posted on Thu, 13 Feb 2020 14:14:53 -0500 by ILYAS415