spring3.0定时任务

来源:互联网 发布:同花顺自动交易软件 编辑:程序博客网 时间:2024/05/24 02:23

spring3.0定时任务

     今天做个小需求,需要用到定时器。 
     就把以前写过的配置文件模板直接复制过来,又顺手点进去看了一下源码,发现TimerFactoryBean、ScheduledTimerTask都已经被标记成@Deprecated了 
Java代码 复制代码 收藏代码
  1. @Deprecated  
  2. public class TimerFactoryBean implements FactoryBean<Timer>, BeanNameAware, InitializingBean, DisposableBean  
Java代码 复制代码 收藏代码
  1. @Deprecated  
  2. public class ScheduledTimerTask  
那肯定就不乐意用了,就上网找了找spring3.0之后的新用法,果然是有变化,而且比以前简单了很多,在这里记录一下 
我记得以前那种做法,业务类是要继承自TimerTask才行的,现在就不用了,是一个pojo就可以 
Java代码 复制代码 收藏代码
  1. public class TestService {   
  2.   
  3.     private Logger logger = LoggerFactory.getLogger(TestService.class);   
  4.   
  5.     public void sayHello() {   
  6.         System.out.println("hello world");   
  7.     }   
  8.   
  9.     public void sayBye() {   
  10.         System.out.println("bye world");   
  11.     }   
  12.   
  13. }  copy

然后配置文件也更简单 
Xml代码 复制代码 收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  4.     http://www.springframework.org/schema/beans/spring-beans.xsd   
  5.     http://www.springframework.org/schema/task      
  6.     http://www.springframework.org/schema/task/spring-task-3.0.xsd">  
  7.   
  8.     <bean id="testService" class="com.xxx.spring.business.TestService" />  
  9.   
  10.     <task:scheduled-tasks>  
  11.         <task:scheduled ref="testService" method="sayHello" cron="3/11 * * * * ?" />  
  12.         <task:scheduled ref="testService" method="sayBye" cron="7/13 * * * * ?" />  
  13.     </task:scheduled-tasks>  
  14.   
  15. </beans>  

只要用一个新增的<task:scheduled-tasks>就可以了 
就是有一点要注意一下,新的时间配置,是类似于cron的语法,比以前强大很多。 
不过我只用到了第一个参数:3/11,表示延迟3秒启动,间隔11秒;7/13表示延迟7秒启动,间隔13秒

 转自:http://www.iteye.com/topic/1125517


2、cron表达式

字段允许值允许的特殊字符

秒 0-59 , - * /

分 0-59 , - * /

小时 0-23 , - * /

日期 1-31 , - * ? / LW C

月份 1-12 或者 JAN-DEC ,- * /

星期 1-7 或者 SUN-SAT ,- * ? / L C #

年(可选)留空, 1970-2099 ,- * /

==============================================================================================

*(秒0-59)    *(分钟0-59)   *(小时0-23)  *(日期1-31)   *(月份1-12或是JAN-DEC)  *(星期1-7或是SUN-SAT) 

---------->>---------------------------->>-------------顺序------------------>>---------------------------------------->>

============================================================================================== 

表达式意义

"0 0 12 * *?" 每天中午12点触发

"0 15 10 ? **" 每天上午10:15触发

"0 15 10 * *?" 每天上午10:15触发

"0 15 10 * * ?*" 每天上午10:15触发

"0 15 10 * * ?2005" 2005年的每天上午10:15触发

"0 * 14 * *?" 在每天下午2点到下午2:59期间的每1分钟触发

"0 0/5 14 * *?" 在每天下午2点到下午2:55期间的每5分钟触发

"0 0/5 14,18 ** ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

"0 0-5 14 * *?" 在每天下午2点到下午2:05期间的每1分钟触发

"0 10,44 14 ? 3WED" 每年三月的星期三的下午2:10和2:44触发

"0 15 10 ? *MON-FRI" 周一至周五的上午10:15触发

"0 15 10 15 *?" 每月15日上午10:15触发

"0 15 10 L *?" 每月最后一日的上午10:15触发

"0 15 10 ? *6L" 每月的最后一个星期五上午10:15触发

"0 15 10 ? * 6L2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发

"0 15 10 ? *6#3" 每月的第三个星期五上午10:15触发



原创粉丝点击