【WEEK15】 【DAY3】Scheduled Tasks【English Version】

2024.6.5 Wednesday
Following 【WEEK15】 【DAY2】【DAY3】Email Tasks【English Version】

17. Asynchronous, Scheduled, and Email Tasks

17.3. Scheduled Tasks

In project development, it is often necessary to execute some scheduled tasks, such as analyzing the previous day’s log information at midnight every day. Spring provides us with asynchronous task scheduling methods, offering two interfaces (see TaskExecutor.class and TaskScheduler.class for details).

  • TaskExecutor interface (Task Executor)
  • TaskScheduler interface (Task Scheduler)

17.3.1. Two Annotations:

  • @EnableScheduling——Annotation to enable scheduling
  • @Scheduled——When to execute

17.3.2. Cron Expression

Cron Expression:

Field Allowed Values Allowed Special Characters
Second 0-59 , - * /
Minute 0-59 , - * /
Hour 0-23 , - * /
Day 1-31 , - * / ? L W C
Month 1-12 , - * /
Week 0-1 or SUN-SAT 0,7 is SUN , - * / ? L W C
Special Character Meaning
, Enumeration
- Range
* Any
/ Step
? Day/Week conflict match
L Last
W Weekday
C Value calculated after calendar practice
# Week, 4#2 means the second Wednesday of the month

17.3.3. Modify Springboot09TestApplication.java to Enable Scheduling

package com.P51;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableAsync   // Enable asynchronous annotation functionality

@EnableScheduling  // Enable scheduling annotation functionality

@SpringBootApplication
public class Springboot09TestApplication {

   public static void main(String[] args) {
      SpringApplication.run(Springboot09TestApplication.class, args);
   }

}

17.3.4. Create ScheduledService.java

package com.P51.service;

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

@Service
public class ScheduledService {
    // Cron expression (you can search for explanations online)
    @Scheduled(cron = "30 06 14 * * ?") // Execute once daily at 14:06:30
    @Scheduled(cron = "0/2 * * * * ?")  // Execute every two seconds daily
    public void hello(){
        System.out.println("execute");
    }
}

Official explanation, very detailed and worth reading:
Insert image description here
Insert image description here

Other references:
https://www.bejson.com/othertools/cron/
Insert image description here

17.3.5. Restart the Project

Insert image description here

Insert image description here

相关推荐

  1. 剑指offer题解合集——Week3day2

    2024-06-11 23:16:03       48 阅读
  2. d8<span style='color:red;'>week</span><span style='color:red;'>17</span>

    d8week17

    2024-06-11 23:16:03      39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-11 23:16:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-11 23:16:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-11 23:16:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-11 23:16:03       20 阅读

热门阅读

  1. C++ Compound types overview

    2024-06-11 23:16:03       8 阅读
  2. Spring Boot 事务传播机制详解

    2024-06-11 23:16:03       7 阅读
  3. 编程爱情——向日葵(小说)

    2024-06-11 23:16:03       6 阅读
  4. 道路运输安全员真题考试题库分享

    2024-06-11 23:16:03       7 阅读
  5. 【python】时间和日期

    2024-06-11 23:16:03       8 阅读
  6. Web前端后端框架:深度剖析与发展趋势

    2024-06-11 23:16:03       7 阅读
  7. 主题切换之根元素CSS自定义类

    2024-06-11 23:16:03       9 阅读
  8. 怎么使用join将数组转为逗号分隔的字符串

    2024-06-11 23:16:03       6 阅读
  9. 最大二叉树-力扣

    2024-06-11 23:16:03       5 阅读
  10. 数组中的map方法

    2024-06-11 23:16:03       6 阅读