spring task的配置方式有兩種:配置文件配置和注解配置。
1.配置文件配置
在applicationContext.xml中增加spring task的命名空間:
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd">
在applicationContext.xml中配置spring task:
<!-- 配置文件方式配置spring task --> <task:scheduled-tasks scheduler="scheduler"> <task:scheduled ref="taskJob" method="test2" cron="*/5 * * * * ?" /> </task:scheduled-tasks>
編寫定時任務方法:
// spring task配置文件方式 public void test2() { String time = DateFormat.getDateTimeInstance().format(new Date()); System.out.println("test2定時器觸發打印" + time); }
2.注解配置
在applicationContext.xml中增加spring task的命名空間:
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd">
在applicationContext.xml中配置spring task:
<!-- 注解方式配置spring task --> <task:annotation-driven scheduler="scheduler" executor="executor"/>
如果使用springboot,則無需配置applicationContext.xml,直接添加@EnableScheduling注解即可。
@EnableScheduling注解的作用是發現注解@Scheduled的任務並后台執行。
@SpringBootApplication @EnableScheduling //允許支持schedule定時任務 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
編寫定時任務方法:
// spring task注解方式 @Scheduled(cron = "*/5 * * * * ?") public void test1() { String time = DateFormat.getDateTimeInstance().format(new Date()); System.out.println("test1定時器觸發打印" + time); }
3.配置多線程定時任務
上述方法可以實現定時任務,方式也比較簡單,不用配置什么文件啥的,但你會發現一個問題,就是不論定時任務被安排在多少個class類中,其依然是單線程執行定時任務(串行任務):
2016-02-14-15-05 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTasks.executeUploadTask 定時任務1:15,name:pool-2-thread-1 定時任務2:15 2016-02-14-15-06 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定時任務2:15,name:pool-2-thread-1
上述執行結果中ScheduledTest和ScheduledTasks是兩個獨立類,都有各自定時任務,但運行時起Thread Name都是一樣的pool-2-thread-1,因此每個定時任務若要新啟一個線程,需要自行編寫實現或者配置文件。
SpringBoot定時任務默認單線程,多線程需要在配置文件applicationContext.xml中添加如下內容:
<!-- 注解方式配置spring task --> <task:annotation-driven scheduler="scheduler" executor="executor"/> <!-- 調度線程池的大小 --> <task:scheduler id="scheduler" pool-size="10"/> <task:executor id="executor" pool-size="10" />
效果如下,每個調度處理一個任務,每個調度也是一個子線程:
4.cron表達式
在spring 4.x中已經不支持7個參數的cronin表達式了,要求必須是6個參數,cron表達式的格式如下。
Seconds Minutes Hours DayofMonth Month DayofWeek
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表示星期一, 依次類推
每一個域都使用數字,但還可以出現如下特殊字符,它們的含義是:
(1)*:表示匹配該域的任意值,假如在Minutes域使用*, 即表示每分鍾都會觸發事件。
(2)?:只能用在DayofMonth和DayofWeek兩個域。它也匹配域的任意值,但實際不會。因為DayofMonth和 DayofWeek會相互影響。例如想在每月的20日觸發調度,不管20日到底是星期幾,則只能使用如下寫法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期幾都會觸發,實際上並不是這樣。
(3)-:表示范圍,例如在Minutes域使用5-20,表示從5分到20分鍾每分鍾觸發一次 。
(4)/:表示起始時間開始觸發,然后每隔固定時間觸發一次,例如在Minutes域使用5/20,則意味着5分鍾觸發一次,而25,45等分別觸發一次。
(5),:表示列出枚舉值值。例如:在Minutes域使用5,20,則意味着在5和20分每分鍾觸發一次。
(6)L:表示最后,只能出現在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味着在最后的一個星期四觸發。
(7)W: 表示有效工作日(周一到周五),只能出現在DayofMonth域,系統將在離指定日期的最近的有效工作日觸發事件。例如:在 DayofMonth使用5W,如果5日是星期六,則將在最近的工作日:星期五,即4日觸發。如果5日是星期天,則在6日(周一)觸發;如果5日在星期一 到星期五中的一天,則就在5日觸發。另外一點,W的最近尋找不會跨過月份。
(8)LW:這兩個字符可以連用,表示在某個月最后一個工作日,即最后一個星期五。
(9)#:用於確定每個月第幾個星期幾,只能出現在DayofMonth域。例如在4#2,表示某月的第二個星期三。
舉幾個例子:
0 0 2 1 * ? * 表示在每月的1日的凌晨2點調度任務
0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15執行作業
0 15 10 ? 6L 2002-2006 表示2002-2006年的每個月的最后一個星期五上午10:15執行作
Cron表達式范例:
每隔5秒執行一次:*/5 * * * * ?
每隔1分鍾執行一次:0 */1 * * * ?
每天23點執行一次:0 0 23 * * ?
每天凌晨1點執行一次:0 0 1 * * ?
每月1號凌晨1點執行一次:0 0 1 1 * ?
每月最后一天23點執行一次:0 0 23 L * ?
每周星期天凌晨1點實行一次:0 0 1 ? * L
在26分、29分、33分執行一次:0 26,29,33 * * * ?
每天的0點、13點、18點、21點都執行一次:0 0 0,13,18,21 * * ?
參考: