关于spring 3.2 @scheduled cron的
来源:互联网 发布:java qq2007.jar 编辑:程序博客网 时间:2024/04/29 18:27
首先是要改配置 在spring.xml主配置文件中加入
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
<task:annotation-driven/> //配置扫描接口<context:annotation-config/> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <context:component-scan base-package="com.test"/>
@Scheduled的注释可以被添加到一个方法以及 触发元数据。 例如,下面的方法会被调用 每5秒用一个固定的延迟,这意味着,周期从完成时间的测量每个前调用。
@Scheduled(fixedDelay=5000)public void doSomething() {// something that should execute periodically}
如果一个固定利率执行,是理想的,只需修改属性 名字中指定的注释。 以下将被执行 每5秒之间测量的开始时间的连续的每个调用。
@Scheduled(fixedRate5000)public void doSomething() {// something that should execute periodically}
对于固定的延迟和固定的任务,一个初始延迟可能 指示指定的毫秒数之前等待的第一 执行的方法。
@Scheduled(initialDelay=1000, fixedRate=5000)public void doSomething() {// something that should execute periodically}
如果简单的周期性调度并没有表现足够,那么一个 cron表达式可能被提供。 例如,以下只会 执行在工作日。
@Scheduled(cron="*/5 * * * * MON-FRI")public void doSomething() {// something that should execute on weekdays only}
注意,方法是计划必须有空隙的回报和 不要期望任何参数。 如果该方法需要互动 其他对象的应用程序上下文,然后那些通常 已经提供了通过依赖注入。
总结起来就是以下几点:
1、spring的@Scheduled注解 需要写在实现上、
2、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true)
3、实现类上要有组件的注解@Component
附 crone的各个字段详解
字段 允许值 允许的特殊字符秒
0-59
, - * /
分
0-59
, - * /
小时
0-23
, - * /
日期
1-31
, - * ? / L W C
月份
1-12 或者 JAN-DEC
, - * /
星期
1-7 或者 SUN-SAT
, - * ? / L C #
年(可选)
留空, 1970-2099
, - * /
每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是:
(1)*:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。
(2)?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。
(3)-:表示范围,例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次
(4)/:表示起始时间开始触发,然后每隔固定时间触发一次,例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次.
(5),:表示列出枚举值值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。
(6)L:表示最后,只能出现在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。
(7)W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份
(8)LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
(9)#:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。
- 关于spring 3.2 @scheduled cron的
- spring @Scheduled cron表达式
- 关于Spring任务调度<task:scheduled-tasks>的cron参数详解
- spring Scheduled cron定时调度时间的设置
- spring Scheduled cron定时调度时间的设置
- spring mvc task:scheduled cron配置
- Spring 定时任务之 @Scheduled cron表达式
- Spring 定时任务之 @Scheduled cron表达式
- spring 定时任务 scheduled Cron表达式
- Spring 定时任务之 @Scheduled cron表达式
- Spring 定时任务之 @Scheduled cron表达式
- Spring 定时任务之 @Scheduled cron表达式
- Spring 定时任务之 @Scheduled cron表达式
- Spring 定时任务之 @Scheduled cron表达式
- Spring 定时任务之 @Scheduled cron表达式
- Spring 定时任务之 @Scheduled cron表达式
- Spring 定时任务之 @Scheduled cron表达式
- Spring 定时任务之 @Scheduled cron表达式
- Stall Reservations
- 【论文笔记】T-Finder-A Recommender System for Finder Passengers and vacant Taxis
- iOS开发之UIButton
- git 命令使用
- ArrayList利用泛型对象操作
- 关于spring 3.2 @scheduled cron的
- c语言基础知识
- c3p0.properties
- Codeforces Round #334 (Div. 2) B. More Cowbell
- 数据结构--向量--归并排序
- fzu2198 快来快来数一数
- 2008
- sdut 1008 最大公共字串
- 同步刷新时对菜单格式的js点击增加class