spring3.0设置定时任务

73 篇文章 1 订阅
9 篇文章 0 订阅

今天做个小需求,需要用到定时器。 

就把以前写过的配置文件模板直接复制过来,又顺手点进去看了一下源码,发现TimerFactoryBean、ScheduledTimerTask都已经被标记成@Deprecated了 

Java代码  复制代码  收藏代码
  1. @Deprecated  
  2. public class TimerFactoryBean implements FactoryBean<Timer>, BeanNameAware, InitializingBean, DisposableBean  
[java]  view plain copy
 
  1. @Deprecated  
  2. public class TimerFactoryBean implements FactoryBean<Timer>, BeanNameAware, InitializingBean, DisposableBean  

 

Java代码  复制代码  收藏代码
  1. @Deprecated  
  2. public class ScheduledTimerTask  
[java]  view plain copy
 
  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. }  
[java]  view plain copy
 
  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. }  


然后配置文件也更简单 

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>  
[xml]  view plain copy
 
  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秒

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值