现在的位置: 首页 > 综合 > 正文

Spring3.0.6定时任务task:scheduled

2019年05月12日 ⁄ 综合 ⁄ 共 3678字 ⁄ 字号 评论关闭

转载自:这里


项目使用的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中配置,而不需要在方法上使用注解配置时间了。

抱歉!评论已关闭.