在Linux系統中有cron定時任務,在spring中也對cron作了實現,它們基本都是相同的,能夠把spring的cron實現看作是Linux的cron實現的一個子集。這裏主要介紹cron的經常使用的表達式的寫法和容易出錯的地方,也會給出具體的例子。cron的表達式中主要有6個字段分別表示:秒 分 時 日 月 星期,在Linux中還支持第7個字段表示年份。在cron中還有2個重要的元字符* 和 -,其中*表示匹配任意,在那個位置上就匹配任意位置的時間,例如在分上是*就表示0-59都匹配。-表示區間。例如:JAN-DEC就表示1月至12月。java
##實例講解 一、 匹配每一天12點怎麼辦?spring
秒 分 時 日 月 星期測試
0 0 12 * * ?spa
在這裏咱們看到一個問號(?),問號是隻能是日 和星期使用,代替*號使用,爲何有星號了還要弄一個問號呢?你想當咱們一個時間的日期肯定了,那麼星期天然也就肯定了,因此不存在匹配任意星期了。同理通常年月星期肯定了日期基本也就肯定了,也就不存在任意的問題了。code
二、每5分鐘執行1次component
秒 分 時 日 月 星期xml
1 0/5 * * * ?it
咱們看到有一個/符號,不少資料把它解釋爲每,好比上面的0/5就表示從0開始,每5分鐘執行一次,不過只是不夠準確的,不信你能夠是一下* 0/5 * * * ?表達式看是否是每5分鐘執行一次。其實更加準確的表示是從0開始,分鐘的數字mod5==0的時候執行(匹配)。* 0/5 * * * ?因此這個表達式是在分鐘數爲0,5,15,20,25,30,35,40,45,50,55這些分鐘數的這1分鐘內會一直執行。(sping的cron中是這樣,Linux的沒有測)io
三、 每20分鐘執行一次ast
秒 分 時 日 月 星期
1 0,20,40 * * * ?
這裏主要是介紹一下分號(,),表示多個匹配
四、每週一到週五8點
秒 分 時 日 月 星期
"0 0 8 * * MON-FRI
符號-表示區間。
五、其它 固然還有一下其它的,例如# L,在spring的cron中是不支持的,能夠看一下下面註釋的代碼瞭解一下意思就能夠了。實在想用也能夠經過其它表達式實現。
##Spring cron ###任務代碼
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component("taskJob") public class AnnotationTaskJob { @Scheduled(cron = "1 0/5 * * * ?") public void everyFiveMin() { System.out.println("每5分鐘執行一次任務"); } @Scheduled(cron = "1 0,20,40 * * * ?") public void everyTwentyMin() { System.out.println("每20分鐘執行一次任務"); } @Scheduled(cron = "0 * * * * ?") public void everyMin() { System.out.println("每一分鐘執行一次任務"); } @Scheduled(cron = "0 0 * * * ?") public void everyHour() { System.out.println("每一小時執行一次任務"); } @Scheduled(cron = "0 0 0 * * ?") public void everyDay() { System.out.println("每一天0點執行一次任務"); } @Scheduled(cron = "0 0 0 1 * ?") public void everyMonth() { System.out.println("每個月1號0點執行一次任務"); } /** * 5月第二個星期天 */ // @Scheduled(cron = "* * * * 5 1#2") // @Scheduled(cron = "* * * * 5 SUN#2") // public void sendEmailWhenMotherDay() // { // System.out.println("母親節了發個郵件提醒一下本身"); // } /** * 6 月第3個星期天 */ // @Scheduled(cron = "* * * * 6 SUN#3") // public void sendEmailWhenFaherDay() // { // System.out.println("父親節了發個郵件提醒一下本身"); // } // @Scheduled(cron = "0 0 8 L * ?") // public void theMonthLast() { // System.out.println("每個月最後一天發郵件提醒一下本身總結"); // } @Scheduled(cron = "0 0 8 * * MON-FRI") public void workDay() { System.out.println("週一到週五8點弄個定時開機"); } // @Scheduled(cron = "0 0 0 1 10 ? 2049") // public void china() { // System.out.println("早點起來爲祖國慶生!"); // } }
###測試代碼
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:conf/spring-task-annotation.xml" }) public class TaskJobAnnotationTest { @Test public void testTaskJob() { Scanner scanner = new Scanner(System.in); scanner.nextLine(); scanner.close(); // System.in.read(); } }
###配置文件
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <context:annotation-config /> <context:component-scan base-package="cn.freemethod.task" /> <task:executor id="executor" pool-size="2" /> <task:scheduler id="scheduler" pool-size="5" /> <task:annotation-driven executor="executor" scheduler="scheduler" /> </beans>