1. 程式人生 > >Atitit spring 定時器 CRON表示式 含義 目錄 1.1. 大概流程 1 1.2. 核心原始碼springboot 1 1.3. Cron表示式屬性——String 2 1.4

Atitit spring 定時器 CRON表示式 含義 目錄 1.1. 大概流程 1 1.2. 核心原始碼springboot 1 1.3. Cron表示式屬性——String 2 1.4

Atitit spring 定時器 CRON表示式    含義 

 

 

目錄

1.1. 大概流程 1

1.2. 核心原始碼springboot 1

1.3. Cron表示式屬性——String 2

1.4. cron表示式生成器 2

 

 

    1. 大概流程

增加一個定時配置類,新增@Configuration和@EnableScheduling註解

使用cron表示式生成器生成一個表示式

定義一個方法,增加Scheduled註解,講表示式放入即可

執行此springboot專案即可。

 

 

    1. 核心原始碼springboot

package timer;

 

import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.Scheduled;

 

@Configuration

@EnableScheduling

public class SchedulingConfig {

 

    @Scheduled(cron = "0/5 * * * * ? ")

    public void test(){

        System.out.println("定時排程。。。。。。。。。。。。");

    }

    

}

 

注意,實際的時間他說只能六個引數。。表示式生成器是7個引數,去掉最後一個即可

    1. Cron表示式屬性——String

Seconds (秒): 可出現", - * /"四個字元,有效範圍為0-59的整數

Minutes (分): 可出現", - * /"四個字元,有效範圍為0-59的整數

Hours (時): 可出現", - * /"四個字元,有效範圍為0-23的整數

DayofMonth (天/月): 可出現", - * / ? L W C"八個字元,有效範圍為0-31的整數

Month (月): 可出現", - * /"四個字元,有效範圍為1-12的整數或JAN-DEc

DayofWeek (星期幾): 可出現", - * / ? L C #"四個字元,有效範圍為1-7的整數或SUN-SAT兩個範圍。1表示星期天,2表示星期一, 依次類推

Year (年): 可出現", - * /"四個字元,有效範圍為1970-2099年

 

    1. cron表示式生成器

推薦一個cron表示式自動生成的網站 點選獲取

http://cron.qqe2.com/

---------------------