文章转自 http://blog.csdn.net/l454822901/article/details/51829307

最近发现真的凹凸了,spring升级到3后原来已经自带任务调度器了,之前还一直使用着Quartz。其实也不是Quartz不好,只是相比之下,使用Spring task真的简单很多,无论是理解还是使用。

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

具体配置参考如下即可

  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. <bean id="reminderProcessor" class="com.foo.task.ReminderProcessor">
  8. <property name="workers">
  9. <array value-type="com.foo.task.Worker">
  10. <ref bean="projectScheduleRemindWorker" />
  11. </array>
  12. </property>
  13. </bean>
  14. <!-- 配置任务线性池 -->
  15. <task:executor id="executor" pool-size="3" />
  16. <task:scheduler id="scheduler" pool-size="3" />
  17. <!-- 启用annotation方式 -->
  18. <task:annotation-driven scheduler="scheduler"
  19. executor="executor" proxy-target-class="true" />
  20. <task:scheduled-tasks scheduler="scheduler">
  21. <task:scheduled ref="reminderProcessor" method="process"
  22. cron="0 0 12 * * ?" />
  23. </task:scheduled-tasks>
  24. </beans>

核心部分见

  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足够了。

附上我这个例子的详细UML说明

最新文章

  1. jQuery插件 -- 动态事件绑定插件jquery.livequery.js
  2. OC-类方法
  3. spring controller中@Value取不到applicationContext.xml中加载配置文件的问题
  4. Python中and和or
  5. Oracle EBS-SQL (SYS-15):查询表空间2.sql
  6. 面向对象的方式进行数据交换网络之间的差异--无缝切换的发展到单机游戏C/S模式
  7. #pragma once 与 #ifndef 解析
  8. web.xml 简记
  9. Python常用字符编码(转)
  10. MacBook使用笔记2 - 安装windows虚拟机攻略
  11. Eclipse中通过Hibernate Tools插件实现从数据库逆向生成Hibernate带注解的实体类
  12. 06-padding(内边距)
  13. Windows:C++11并发编程-条件变量(condition_variable)详解
  14. US Open 2016 Contest
  15. [Luogu2656]采蘑菇
  16. HTTP Status 500 - com.opensymphony.xwork2.ActionSupport.toAddPage()
  17. (转 )Unity对Lua的编辑器拓展
  18. C++ new delete(一)
  19. 免费SSL申请
  20. Tomcat配置JVM运存

热门文章

  1. [Python] Python基础字符串
  2. 【RabbitMQ】4、RabbitMQ几种Exchange 模式
  3. 设计模式之模板方法模式(TemplateMethod)
  4. FIFO、LRU、LFU的含义和原理
  5. python自动化开发-6-面向对象编程
  6. JVM、Gc工作机制详解
  7. axure工具的使用总结
  8. Kotlin入门(6)条件分支的实现
  9. JAVA 设计模式遵循的六大基本准则
  10. Appium学习——安装appium Server