`

spring3.0定时器

 
阅读更多

from《Spring in Action3》

基于注解的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:每隔多少毫秒执行一次该方法。如:

@Scheduled(fixedRate=2000)
public void scheduleMethod(){
	System.out.println("Hello world...");
}
	
 

 

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

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

@Scheduled(cron="0 0 0 * * SAT")
public voidarchiveOldSpittles(){
// ...
}
 

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


详细参考Spring reference document.

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics