spring3.0定时器

来源:互联网 发布:js base64解码图片 编辑:程序博客网 时间:2024/04/29 09:57
我记得以前那种做法,业务类是要继承自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 plaincopy
  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 plaincopy
  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秒





于注解的sping3定时器配置

1.首先要在application-context.xml里面配置好namespace 和schema,如下:

xmlns:task="http://www.springframework.org/schema/task"


http://www.springframework.org/schema/task
 http://www.springframework.org/schema/task/spring-task-3.0.xsd

 

2.在application-context.xml里面配置<task:annotation-driven/>,加下面一行就行:

<task:annotation-driven/>

 

<!-- The <task:annotation-driven/> element sets Spring up to automatically support
    scheduled and asynchronous methods. These methods are identified with the
    @Scheduled and @Async methods, respectively -->

 

3.在Bean里面的调度方法加注解@Scheduled,其中@Scheduled的attribute有三种:  

  (1)fixedRate:每隔多少毫秒执行一次该方法。如:

Java代码  收藏代码
  1. @Scheduled(fixedRate=2000)  
  2. public void scheduleMethod(){  
  3.     System.out.println("Hello world...");  
  4. }  
  5.       
 

 

  (2)fixedDelay:当一次方法执行完毕之后,延迟多少毫秒再执行该方法。 

  (3)cron:详细配置了该方法在什么时候执行。cron值是一个cron表达式。如:

Java代码  收藏代码
  1. @Scheduled(cron="0 0 0 * * SAT")  
  2. public voidarchiveOldSpittles(){  
  3. // ...  
  4. }  
 

The value given to the cron attribute is a Cron expression. For those who aren’t so
well-versed in Cron expressions, let’s break down the cron attribute. The Cron expres-
sion is made up of six (or possibly seven) time elements, separated by spaces. In order
from left to right, the elements are defined as follows:
1 Seconds (0-59)
2 Minutes (0-59)
3 Hours (0-23)
4 Day of month (1-31)
5 Month (1-12 or JAN-DEC)
6 Day of week (1-7 or SUN-SAT)
7 Year (1970-2099)


一些cron表达式的例子:


Cron expression        What it means
0 0 10,14,16 * * ?       Every day at 10 a.m., 2 p.m., and 4 p.m.
0 0,15,30,45 * 1-30 * ?    Every 15 minutes on the first 30 days of the month
30 0 0 1 1 ? 2012       30 seconds after midnight on January 1, 2012
0 0 8-17 ? * MON-FRI     Every working hour of every business day




0 0