spring中使用定時任務
1、基於xml配置文件使用定時任務
首先配置spring開啟定時任務
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <task:annotation-driven /> <!-- 定時器開關--> <bean id="myTask" class="com.spring.task.MyTask"></bean> <task:scheduled-tasks> <!-- 這里表示的是每隔五秒執行一次 --> <task:scheduled ref="myTask" method="show" cron="*/5 * * * * ?" /> <task:scheduled ref="myTask" method="print" cron="*/10 * * * * ?"/> </task:scheduled-tasks> <!-- 自動掃描的包名 --> <context:component-scan base-package="com.spring.task" /> </beans>
定義自己的任務執行邏輯
package com.spring.task; /** * 定義任務 */ public class MyTask { public void show() { System.out.println("show method 1"); } public void print() { System.out.println("print method 1"); } }
2、基於注解使用定時任務
package com.spring.task; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * 基於注解的定時器 */ @Component public class MyTask2 { /** * 定時計算。每天凌晨 01:00 執行一次 */ @Scheduled(cron = "0 0 1 * * *") public void show() { System.out.println("show method 2"); } /** * 啟動時執行一次,之后每隔2秒執行一次 */ @Scheduled(fixedRate = 1000*2) public void print() { System.out.println("print method 2"); } }
這樣,當項目啟動,定時任務就會按照規則按時執行了。
3、Spring Boot中使用定時任務
Spring Boot中使用更加方便。
引入springboot starter
包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
在程序入口啟動類添加@EnableScheduling
,開啟定時任務功能
@SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }
定義定時任務邏輯
@Component public class MyTask3 { private int count=0; @Scheduled(cron="*/6 * * * * ?") private void process() { System.out.println("this is scheduler task runing "+(count++)); } }
任務執行規則說明
先來看看@Scheduled
注解的源碼
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(Schedules.class) public @interface Scheduled { String cron() default ""; String zone() default ""; long fixedDelay() default -1; String fixedDelayString() default ""; long fixedRate() default -1; String fixedRateString() default ""; long initialDelay() default -1; String initialDelayString() default ""; }
可以看出,注解中可以傳8種參數:
- cron:指定cron表達式
- zone:默認使用服務器默認時區。可以設置為
java.util.TimeZone
中的zoneId - fixedDelay:從上一個任務完成開始到下一個任務開始的間隔,單位毫秒
- fixedDelayString:同上,時間值是
String
類型 - fixedRate:從上一個任務開始到下一個任務開始的間隔,單位毫秒
- fixedRateString:同上,時間值是
String
類型 - initialDelay:任務首次執行延遲的時間,單位毫秒
- initialDelayString:同上,時間值是
String
類型
cron表達式的使用方法
Cron表達式是一個字符串,字符串以5或6個空格隔開,分為6或7個域,每一個域代表一個含義,Cron有如下兩種語法格式:
- Seconds Minutes Hours DayofMonth Month DayofWeek Year
- 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表示星期一, 依次類推
- Year: 可出現", - * /"四個字符,有效范圍為1970-2099年
每一個域都使用數字,但還可以出現如下特殊字符,它們的含義是:
*
:表示匹配該域的任意值,假如在Minutes域使用*, 即表示每分鍾都會觸發事件。?
:只能用在DayofMonth和DayofWeek兩個域。它也匹配域的任意值,但實際不會。因為DayofMonth和 DayofWeek會相互影響。例如想在每月的20日觸發調度,不管20日到底是星期幾,則只能使用如下寫法: 13 13 15 20 ?, 其中最后一位只能用?,而不能使用,如果使用*表示不管星期幾都會觸發,實際上並不是這樣。-
:表示范圍,例如在Minutes域使用5-20,表示從5分到20分鍾每分鍾觸發一次。/
:表示起始時間開始觸發,然后每隔固定時間觸發一次,例如在Minutes域使用5/20,則意味着5分鍾觸發一次,而25,45等分別觸發一次。,
:表示列出枚舉值值。例如:在Minutes域使用5,20,則意味着在5和20分每分鍾觸發一次。L
:表示最后,只能出現在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味着在最后的一個星期四觸發。W
:表示有效工作日(周一到周五),只能出現在DayofMonth域,系統將在離指定日期的最近的有效工作日觸發事件。例如:在 DayofMonth使用5W,如果5日是星期六,則將在最近的工作日:星期五,即4日觸發。如果5日是星期天,則在6日(周一)觸發;如果5日在星期一 到星期五中的一天,則就在5日觸發。另外一點,W的最近尋找不會跨過月份。LW
:這兩個字符可以連用,表示在某個月最后一個工作日,即最后一個星期五。#
:用於確定每個月第幾個星期幾,只能出現在DayofMonth域。例如在4#2,表示某月的第二個星期三。
看來這個東西還是要經常用才不會忘啊。。。