Spring任务调度器之Task的使用

转:http://kennylee26.iteye.com/blog/1965291

 

Spring Task提供两种方式进行配置,正如大家所想吧,还是一种是annotation(标注),而另外一种就是XML配置了。但其实这里我觉得比较尴尬,因为任务调度这样的需求,通常改动都是比较多的,如果用annotation的方式的话,改动就变得麻烦了,必须去重新编译。所以,我只是选择用XML配置的方式,不过我还是习惯性地启用着标注方式,就如AOP配置那样。annotation方式请自行查找@Scheduled

 

具体配置参考如下即可

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"  
  4.     xsi:schemaLocation="  
  5.           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">  
  7.   
  8.     <bean id="reminderProcessor" class="com.foo.task.ReminderProcessor">  
  9.         <property name="workers">  
  10.             <array value-type="com.foo.task.Worker">  
  11.                 <ref bean="projectScheduleRemindWorker" />  
  12.             </array>  
  13.         </property>  
  14.     </bean>  
  15.   
  16.     <!-- 配置任务线性池 -->  
  17.     <task:executor id="executor" pool-size="3" />  
  18.     <task:scheduler id="scheduler" pool-size="3" />  
  19.     <!-- 启用annotation方式 -->  
  20.     <task:annotation-driven scheduler="scheduler"  
  21.         executor="executor" proxy-target-class="true" />  
  22.   
  23.     <task:scheduled-tasks scheduler="scheduler">  
  24.         <task:scheduled ref="reminderProcessor" method="process"  
  25.             cron="0 0 12 * * ?" />  
  26.     </task:scheduled-tasks>  
  27. </beans>  

 

 

 

核心部分见

 

Xml代码  收藏代码
  1. <task:scheduled-tasks scheduler="scheduler">  
  2. <task:scheduled ref="reminderProcessor" method="process"  
  3. cron="0 0 12 * * ?" />  
  4. </task:scheduled-tasks>  

 

 

意思就是每天的12点执行reminderProcessor这个Bean中的process方法。cron的配置表达式跟Quartz基本一致,但实测不支持一些特殊字符,如配置天的时候的L,W和Z,因为遇到要每个月倒数第三天执行任务调度的需求,但我一配置SpringTask报非法字符。

 

所以,Quartz和SpringTask间的差距也显而易见的。SpringTask用起来十分简单,毕竟是Spring自家的,虽然跟Quartz也可以实现结合,但没那么简单。而SpringTask功能也没Quartz强大,Quartz的集群和高级特性多的去了。所以大家可以自行选择了。不过一般情况下,觉得SpringTask足够了。

 

 

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-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">
        
    
    <context:annotation-config />
    <!-- Spring自动扫描包 -->
    <context:component-scan base-package="com.test.schedule" />
    
    <!-- 计划任务 -->
    <task:executor id="executor" pool-size="5" />
    <task:scheduler id="scheduler" pool-size="10" />
    <task:annotation-driven executor="executor" scheduler="scheduler" />
    
</beans>

  

@Component
public class JobDemo {

    @Scheduled(cron = "1 * * * * ?")
    public void execute() {
        System.out.println("JobDemo"));
        long ms = System.currentTimeMillis();
        System.out.println(ms);
    }

}

  

posted @ 2015-06-19 10:18  antball  阅读(306)  评论(0编辑  收藏  举报