1. 程式人生 > >spring 定時調度

spring 定時調度

() 一段時間 not spa 註解 quartz 使用 job 配置

很多時候,都需要使用定時調度,比如每隔一段時間運行一個爬蟲爬取新的數據,實現方法有Quartz,spring task和Timer

spring task可以將它比作一個輕量級的Quartz,而且使用起來很簡單,除spring相關的包外不需要額外的包

推薦使用註解式實現

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

@Component
public class WebTask {

    // 每五秒執行一次
    @Scheduled(cron = "0/5 * * * * ?")
    public void TaskJob() {
        System.out.println("test");
    }
}

 在相關的配置下加個這句配置,不用其他參數

<task:annotation-driven />

cron的具體解析如下

技術分享圖片
字段      允許值     允許的特殊字符
秒       0-59        , - * /
分       0-59        , - * /
小時      0-23        , - * /
日期      1-31        , - * ? / L W C
月份      1-12 或者 JAN-DEC     , - * /
星期      1-7 或者 SUN-SAT      , - * ? / L C #
年(可選)       留空, 1970-2099       , - * /
View Code

Quartz

 

spring 定時調度