Spring 定時器使用

一、spring的配置文件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-3.0.xsd  
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd    
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd    
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">  
  
    <task:annotation-driven /> <!-- 註解類定時器須要配置-->  
    
    <task:scheduler id="jobScheduler" pool-size="10"/>
    <task:scheduled-tasks scheduler="jobScheduler">  
        <task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />  
        <task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/>  
    </task:scheduled-tasks>  
      
    <!-- 自動掃描的包名 -->    
    <context:component-scan base-package="com.spring.task" />  
      
</beans>

二、基於xml的定時器任務spring-mvc

package com.spring.task;  
  
/** 
 * 基於xml的定時器 
 * @author hj 
 */  
public class MyTaskXml {  
      
      
    public void show(){  
        System.out.println("XMl:is show run");  
    }  
      
    public void print(){  
        System.out.println("XMl:print run");  
    }  
}

 三、基於註解的定時器任務mvc

package com.spring.task;  
  
import org.springframework.scheduling.annotation.Scheduled;  
import org.springframework.stereotype.Component;  
  
/** 
 * 基於註解的定時器 
 * @author hj 
 */  
@Component  
public class MyTaskAnnotation {  
      
    /**  
     * 定時計算。天天凌晨 01:00 執行一次  
     */    
    @Scheduled(cron = "0 0 1 * * *")   
    public void show(){  
        System.out.println("Annotation:is show run");  
    }  
      
    /**  
     * 心跳更新。啓動時執行一次,以後每隔2秒執行一次  
     */    
    @Scheduled(fixedRate = 1000*2)   
    public void print(){  
        System.out.println("Annotation:print run");  
    }  
}

 四、測試測試

package com.spring.test;  
  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
  
public class Main {  
    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mvc.xml");  
    }  
}

 

說明:spa

         1)Cron表達式的格式:秒 分 時 日 月 周 年(可選)code

               字段名                 容許的值                        容許的特殊字符  component

               秒                         0-59                               , - * /  xml

               分                         0-59                               , - * /  io

               小時                   0-23                               , - * /  class

               日                         1-31                               , - * ? / L W C  

               月                         1-12 or JAN-DEC          , - * /  

               周幾                     1-7 or SUN-SAT            , - * ? / L C #  

               年 (可選字段)     empty, 1970-2099      , - * /

               「?」字符:表示不肯定的值

               「,」字符:指定數個值

               「-」字符:指定一個值的範圍

               「/」字符:指定一個值的增長幅度。n/m表示從n開始,每次增長m

               「L」字符:用在日表示一個月中的最後一天,用在周表示該月最後一個星期X

               「W」字符:指定離給定日期最近的工做日(週一到週五)

               「#」字符:表示該月第幾個周X。6#3表示該月第3個週五

         2)Cron表達式範例:

                 每隔5秒執行一次:*/5 * * * * ?

                 每隔1分鐘執行一次:0 */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 * * ?

相關文章
相關標籤/搜索