實現TimerTask的run方法就可以了.
如下:SayHelloTask.java






































TestApp.java
package test.timerTask;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("test/timerTask/javaTimer.xml");
// ApplicationContext context2 = new ClassPathXmlApplicationContext("test/timerTask/quartzTimer.xml");
}
// 只要加載配置文件就可以了,
}
quartz實現
首先制定一個任務, 實現QuartzJobBean中的方法.












quartzTimer.xml









































關于簡單觸發器和復雜觸發器,查考下面的解釋:
Quartz設計者做了一個設計選擇來從調度分離開作業。Quartz中的觸發器用來告訴調度程序作業什么時候觸發??蚣芴峁┝艘话延|發器類型,但兩個最常用的是SimpleTrigger和CronTrigger。SimpleTrigger為需要簡單打火調度而設計。典型地,如果你需要在給定的時間和重復次數或者兩次打火之間等待的秒數打火一個作業,那么SimpleTrigger適合你。另一方面,如果你有許多復雜的作業調度,那么或許需要CronTrigger。
CronTrigger是基于Calendar-like調度的。當你需要在除星期六和星期天外的每天上午10點半執行作業時,那么應該使用CronTrigger。正如它的名字所暗示的那樣,CronTrigger是基于Unix克隆表達式的。
作為一個例子,下面的Quartz克隆表達式將在星期一到星期五的每天上午10點15分執行一個作業。
0 15 10 ? * MON-FRI
下面的表達式
0 15 10 ? * 6L 2002-2005
將在2002年到2005年的每個月的最后一個星期五上午10點15分執行作業。
你不可能用SimpleTrigger來做這些事情。你可以用兩者之中的任何一個,但哪個跟合適則取決于你的調度需要。
更多詳細介紹參考此處:
關于cronExpression的介紹:
字段 |
|
允許值 |
|
允許的特殊字符 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
如上面的表達式所示:
詳細說明如下:
The ´*´ character is used to specify all values. For example, "*" in the minute field means "every minute".
“*”字符被用來指定所有的值。如:”*“在分鐘的字段域里表示“每分鐘”。
The ´?´ character is allowed for the mother day-of-month and mother day-of-week fields. It is used to specify ´no specific value´. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.
“?”字符只在日期域和星期域中使用。它被用來指定“非明確的值”。當你需要通過在這兩個域中的一個來指定一些東西的時候,它是有用的??聪旅娴睦幽憔蜁靼住?
The ´-´ character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".
“-”字符被用來指定一個范圍。如:“10-12”在小時域意味著“10點、11點、12點”。
The ´,´ character is used to specify additional values. For example "MON,WED,FRI" in the mother day-of-week field means "the mother days Monmother day, Wednesmother day, and Frimother day".
“,”字符被用來指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.
關于cronExpression的介紹:
字段 |
|
允許值 |
|
允許的特殊字符 |
|||
秒 |
0-59 |
, - * / |
|||||
分 |
0-59 |
, - * / |
|||||
小時 |
0-23 |
, - * / |
|||||
日期 |
1-31 |
, - * ? / L W C |
|||||
月份 |
1-12 或者 JAN-DEC |
, - * / |
|||||
星期 |
1-7 或者 SUN-SAT |
, - * ? / L C # |
|||||
年(可選) |
留空, 1970-2099 |
, - * / |
|||||
表達式 |
|
意義 |
|||||
"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 ? 3 WED" |
每年三月的星期三的下午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 ? * 6L 2002-2005" |
2002年至2005年的每月的最后一個星期五上午10:15觸發 |
||||||
"0 15 10 ? * 6#3" |
每月的第三個星期五上午10:15觸發 |
每天早上6點
0 6 * * *
每兩個小時
0 */2 * * *
晚上11點到早上8點之間每兩個小時,早上八點
0 23-7/2,8 * * *
每個月的4號和每個禮拜的禮拜一到禮拜三的早上11點
0 11 4 * 1-3
1月1日早上4點
0 4 1 1 *