項目使用的Spring版本比較舊是3.0.6版本,由於需要進行定時任務,就決定使用Spring自帶的scheduled task。
在網上找了很多文章,也查看了Spring3.0.6的官方文檔,按照網上和文檔所說,可以使用註解或者配置兩種方法之一都行,但是我發現單獨使用兩種方法都不行,怎麼配置任務都無法運行。
最後看到一篇文章說兩種方法同時用,才成功執行定時任務,可能是個Bug,我試了下,同時使用註解和XML配置後,任務確實成功執行了。
XML配置中,只需要配置一個方法即可,其他方法也能跟着運行了,而且XML中配置的定時時間會被註解覆蓋掉,只能先這麼做了,期待高手解答原因。
難道真的是Spring3.0.6的Bug??
Spring配置如下:
- <?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:jee="http://www.springframework.org/schema/jee"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- 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.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"
- default-lazy-init="true" default-autowire="byName">
- <!-- 配置註解掃描 -->
- <context:annotation-config />
- <!-- 自動掃描的包名 -->
- <context:component-scan base-package="com.demo" />
- <!-- Spring定時器註解開關-->
- <task:annotation-driven />
- <!-- 此處對於定時時間的配置會被註解中的時間配置覆蓋,因此,以註解配置爲準 -->
- <task:scheduled-tasks scheduler="myScheduler">
- <task:scheduled ref="scheduledTaskManager" method="autoCardCalculate" cron="1/5 * * * * *"/>
- </task:scheduled-tasks>
- <task:scheduler id="myScheduler" pool-size="10"/>
- <aop:aspectj-autoproxy />
- <!-- 加載配置文件 -->
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:config.properties</value>
- </list>
- </property>
- </bean>
- </beans>
執行任務的POJO類如下:
- package com.demo.schedule;
- import org.apache.log4j.Logger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- import java.util.Date;
- /**
- * Created with IntelliJ IDEA.
- * Author:
- * Date: 2013-10-09 14:39
- * Function: Spring定時任務管理
- */
- @Component("scheduledTaskManager")
- public class ScheduledTaskManager {
- /**
- * cron表達式:* * * * * *(共6位,使用空格隔開,具體如下)
- * cron表達式:*(秒0-59) *(分鐘0-59) *(小時0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT)
- */
- /**
- * 定時卡點計算。每天凌晨 02:00 執行一次
- */
- @Scheduled(cron = "0 0 2 * * *")
- public void autoCardCalculate() {
- System.out.println("定時卡點計算... " + new Date());
- }
- /**
- * 心跳更新。啓動時執行一次,之後每隔1分鐘執行一次
- */
- @Scheduled(fixedRate = 1000*60*1)
- public void heartbeat() {
- System.out.println("心跳更新... " + new Date());
- }
- /**
- * 卡點持久化。啓動時執行一次,之後每隔2分鐘執行一次
- */
- @Scheduled(fixedRate = 1000*60*2)
- public void persistRecord() {
- System.out.println("卡點持久化... " + new Date());
- }
- }
對於這個問題,期待高手解答,也希望大家能一起討論下。
替換成Sping3.2.2之後,就可以直接在XML中配置,而不需要在方法上使用註解配置時間了。