Spring3.0.6定時任務task:scheduled

項目使用的Spring版本比較舊是3.0.6版本,由於需要進行定時任務,就決定使用Spring自帶的scheduled task。

在網上找了很多文章,也查看了Spring3.0.6的官方文檔,按照網上和文檔所說,可以使用註解或者配置兩種方法之一都行,但是我發現單獨使用兩種方法都不行,怎麼配置任務都無法運行。

最後看到一篇文章說兩種方法同時用,才成功執行定時任務,可能是個Bug,我試了下,同時使用註解和XML配置後,任務確實成功執行了。

XML配置中,只需要配置一個方法即可,其他方法也能跟着運行了,而且XML中配置的定時時間會被註解覆蓋掉,只能先這麼做了,期待高手解答原因。

難道真的是Spring3.0.6的Bug??

Spring配置如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xmlns:jee="http://www.springframework.org/schema/jee"  
  4.        xmlns:tx="http://www.springframework.org/schema/tx"  
  5.        xmlns:context="http://www.springframework.org/schema/context"  
  6.        xmlns:aop="http://www.springframework.org/schema/aop"  
  7.        xmlns:task="http://www.springframework.org/schema/task"  
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  10.        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
  11.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  12.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  13.        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"  
  14.        default-lazy-init="true" default-autowire="byName">  
  15.   
  16.     <!-- 配置註解掃描 -->  
  17.     <context:annotation-config />  
  18.   
  19.     <!-- 自動掃描的包名 -->  
  20.     <context:component-scan base-package="com.demo" />  
  21.   
  22.     <!-- Spring定時器註解開關-->  
  23.     <task:annotation-driven />  
  24.     <!-- 此處對於定時時間的配置會被註解中的時間配置覆蓋,因此,以註解配置爲準 -->  
  25.     <task:scheduled-tasks scheduler="myScheduler">  
  26.         <task:scheduled ref="scheduledTaskManager" method="autoCardCalculate" cron="1/5 * * * * *"/>  
  27.     </task:scheduled-tasks>  
  28.     <task:scheduler id="myScheduler" pool-size="10"/>  
  29.   
  30.     <aop:aspectj-autoproxy />  
  31.   
  32.     <!-- 加載配置文件 -->  
  33.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  34.         <property name="locations">  
  35.             <list>  
  36.                 <value>classpath:config.properties</value>  
  37.             </list>  
  38.         </property>  
  39.     </bean>  
  40. </beans>  

執行任務的POJO類如下:

  1. package com.demo.schedule;  
  2.   
  3. import org.apache.log4j.Logger;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.scheduling.annotation.Scheduled;  
  6. import org.springframework.stereotype.Component;  
  7. import java.util.Date;  
  8. /** 
  9.  * Created with IntelliJ IDEA. 
  10.  * Author:  
  11.  * Date: 2013-10-09 14:39 
  12.  * Function: Spring定時任務管理 
  13.  */  
  14. @Component("scheduledTaskManager")  
  15. public class ScheduledTaskManager {  
  16.     /** 
  17.      * cron表達式:* * * * * *(共6位,使用空格隔開,具體如下) 
  18.      * cron表達式:*(秒0-59) *(分鐘0-59) *(小時0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT) 
  19.      */  
  20.   
  21.     /** 
  22.      * 定時卡點計算。每天凌晨 02:00 執行一次 
  23.      */  
  24.     @Scheduled(cron = "0 0 2 * * *")  
  25.     public void autoCardCalculate() {  
  26.         System.out.println("定時卡點計算... " + new Date());  
  27.     }  
  28.   
  29.     /** 
  30.      * 心跳更新。啓動時執行一次,之後每隔1分鐘執行一次 
  31.      */  
  32.     @Scheduled(fixedRate = 1000*60*1)  
  33.     public void heartbeat() {  
  34.         System.out.println("心跳更新... " + new Date());  
  35.     }  
  36.   
  37.     /** 
  38.      * 卡點持久化。啓動時執行一次,之後每隔2分鐘執行一次 
  39.      */  
  40.     @Scheduled(fixedRate = 1000*60*2)  
  41.     public void persistRecord() {  
  42.         System.out.println("卡點持久化... " + new Date());  
  43.     }  
  44. }  

對於這個問題,期待高手解答,也希望大家能一起討論下。

替換成Sping3.2.2之後,就可以直接在XML中配置,而不需要在方法上使用註解配置時間了。


發佈了11 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章