Timer一種工具,線程用其安排以后在后臺線程中執(zhí)行的任務(wù)??砂才湃蝿?wù)執(zhí)行一次,或者定期重復執(zhí)行。
與每個 Timer 對象相對應(yīng)的是單個后臺線程,用于順序地執(zhí)行所有計時器任務(wù)。計時器任務(wù)應(yīng)該迅速完成。如果完成某個計時器任務(wù)的時間太長,那么它會“獨占”計時器的任務(wù)執(zhí)行線程。因此,這就可能延遲后續(xù)任務(wù)的執(zhí)行,而這些任務(wù)就可能“堆在一起”,并且在上述不友好的任務(wù)最終完成時才能夠被快速連續(xù)地執(zhí)行。
對 Timer 對象最后的引用完成后,并且 所有未處理的任務(wù)都已執(zhí)行完成后,計時器的任務(wù)執(zhí)行線程會正常終止(并且成為垃圾回收的對象)。但是這可能要很長時間后才發(fā)生。默認情況下,任務(wù)執(zhí)行線程并不作為守護線程 來運行,所以它能夠阻止應(yīng)用程序終止。如果調(diào)用者想要快速終止計時器的任務(wù)執(zhí)行線程,那么調(diào)用者應(yīng)該調(diào)用計時器的 cancel 方法。
如果意外終止了計時器的任務(wù)執(zhí)行線程,例如調(diào)用了它的 stop 方法,那么所有以后對該計時器安排任務(wù)的嘗試都將導致 IllegalStateException,就好像調(diào)用了計時器的 cancel 方法一樣。
此類是線程安全的:多個線程可以共享單個 Timer 對象而無需進行外部同步。
此類不 提供實時保證:它使用 Object.wait(long) 方法來安排任務(wù)。
實現(xiàn)注意事項:此類可擴展到大量同時安排的任務(wù)(存在數(shù)千個都沒有問題)。在內(nèi)部,它使用二進制堆來表示其任務(wù)隊列,所以安排任務(wù)的開銷是 O(log n),其中 n 是同時安排的任務(wù)數(shù)。
實現(xiàn)注意事項:所有構(gòu)造方法都啟動計時器線程
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListner implements ServletContextListener {
private Timer timer=null;
private long start=0;
private long end=1000*60;
public void contextInitialized(ServletContextEvent event1) {
timer=new Timer(true);
timer.schedule(new Str(),start,end);
void |
schedule(TimerTask task, Date time) 安排在指定的時間執(zhí)行指定的任務(wù)。 |
void |
schedule(TimerTask task, Date firstTime, long period) 安排指定的任務(wù)在指定的時間開始進行重復的固定延遲執(zhí)行。 |
void |
schedule(TimerTask task, long delay) 安排在指定延遲后執(zhí)行指定的任務(wù)。 |
void |
schedule(TimerTask task, long delay, long period) 安排指定的任務(wù)從指定的延遲后開始進行重復的固定延遲執(zhí)行 |
}
public void contextDestroyed(ServletContextEvent event0) {
timer.cancel();// 終止此計時器,丟棄所有當前已安排的任務(wù)。
}
}
import java.util.Date;
import java.util.TimerTask;
public class Str extends TimerTask{
public void run() {
System.out.println("定時測試成功!"+(new Date()));
}
}
web.xml加以配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class> MyListner</listener-class>
</listener>
------------------------------------------------------------------------------------------------------------
原文出處:
http://blog.csdn.net/feifniao/archive/2007/11/22/1897392.aspx
首先,如果你在web.xml文件中的配置是這樣的:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext-*.xml
</param-value>
</context-param>
那么需要在工程下創(chuàng)建一個以applicationContext- 為開頭的xml文件eg:applicationContext-jobconfig.xml
xml的頭和結(jié)尾部分跟其他spring配置文件相似,就不贅述,正文如下:
<bean id="youJobName(類別名)" class="com.******.YourJobClassLocation(類的定位)" />
<bean id="doYourJob(別名)" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="youJobName(類別名)""/>
</property>
<property name="targetMethod">
<value>runMethodName(定時執(zhí)行的方法名)</value>
</property>
</bean>
<bean id="youJobNameTrigger(觸發(fā)器別名)" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="doYourJob(別名)""/>
</property>
<property name="cronExpression">
<value>0 0/20 * * * ?(定時的時間配置)</value>
</property>
</bean>
<bean id="doScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="youJobNameTrigger(觸發(fā)器別名)"/>
</list>
</property>
</bean>
這樣的配置幾本就可以運轉(zhuǎn)了,但是有一個地方可能是你需要根據(jù)你的需求來確定的,那就是觸發(fā)時間。
下面有一些關(guān)于時間配置的說明:
字段順序 |
|
允許值 |
|
允許的特殊字符 |
秒 |
0-59 |
, - * / |
||
分 |
0-59 |
, - * / |
||
小時 |
0-23 |
, - * / |
||
日期 |
1-31 |
, - * ? / L W C |
||
月份 |
1-12 或者 JAN-DEC |
, - * / |
||
星期 |
1-7 或者 SUN-SAT |
, - * ? / L C # |
||
年(可選) |
留空, 1970-2099 |
, - * / |
The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".
“*”字符被用來指定所有的值。如:”*“在分鐘的字段域里表示“每分鐘”。
The '?' character is allowed for the day-of-month and 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.
“?”字符只在日期域和星期域中使用。它被用來指定“非明確的值”。當你需要通過在這兩個域中的一個來指定一些東西的時候,它是有用的。看下面的例子你就會明白。
月份中的日期和星期中的日期這兩個元素時互斥的一起應(yīng)該通過設(shè)置一個問號(?)來表明不想設(shè)置那個字段
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 day-of-week field means "the days Monday, Wednesday, and Friday".
“,”字符被用來指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.
The '/' character is used to specify increments. For example "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". Specifying '*' before the '/' is equivalent to specifying 0 is the value to start with. Essentially, for each field in the expression, there is a set of numbers that can be turned on or off. For seconds and minutes, the numbers range from 0 to 59. For hours 0 to 23, for days of the month 0 to 31, and for months 1 to 12. The "/" character simply helps you turn on every "nth" value in the given set. Thus "7/6" in the month field only turns on month "7", it does NOT mean every 6th month, please note that subtlety.
"/"字符用來指定漸增的值。例如0/15出現(xiàn)在秒字段的時候意味著“在第0,15,30和45秒”(的時候被觸發(fā))。而5/15出現(xiàn)在秒字段的時候意味著“在第5,20,35和第50秒”(的時候被觸發(fā))。"*/"和"0/"的指定是等價的。
注:當分子+分母的值大于該字段的最大值,如在秒鐘字段出現(xiàn)45/20的時候,表示在第45秒以后的每20會觸發(fā)一次,但是20秒以后又算做另外的時間段以內(nèi)了,所以該字段的/20即失效,如改為45/10 則只有第45 和55秒會執(zhí)行一次。
The 'L' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "last", but it has different meaning in each of the two fields. For example, the value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.
L是‘last’的省略寫法可以表示day-of-month和day-of-week域,但在兩個字段中的意思不同,例如day-of-month域中表示一個月的最后一天,
如果在day-of-week域表示‘7’或者‘SAT’,如果在day-of-week域中前面加上數(shù)字,它表示一個月的最后幾天,例如‘6L’就表示一個月的最后一個
星期五,
The 'W' character is allowed for the day-of-month field. This character is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify "15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month". So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify "1W" as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary of a month's days. The 'W' character can only be specified when the day-of-month is a single day, not a range or list of days
.
The 'L' and 'W' characters can also be combined for the day-of-month expression to yield 'LW', which translates to "last weekday of the month".
The '#' character is allowed for the day-of-week field. This character is used to specify "the nth" XXX day of the month. For example, the value of "6#3" in the day-of-week field means the third Friday of the month (day 6 = Friday and "#3" = the 3rd one in the month). Other examples: "2#1" = the first Monday of the month and "4#5" = the fifth Wednesday of the month. Note that if you specify "#5" and there is not 5 of the given day-of-week in the month, then no firing will occur that month.
The 'C' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the day-of-month field means "the first day included by the calendar on or after the 5th". A value of "1C" in the day-of-week field means "the first day included by the calendar on or after sunday".
關(guān)于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點觸發(fā)
"0 15 10 ? * *" 每天上午10:15觸發(fā)
"0 15 10 * * ?" 每天上午10:15觸發(fā)
"0 15 10 * * ? *" 每天上午10:15觸發(fā)
"0 15 10 * * ? 2005" 2005年的每天上午10:15觸發(fā)
"0 * 14 * * ?" 在每天下午2點到下午2:59期間的每1分鐘觸發(fā)
"0 0/5 14 * * ?" 在每天下午2點到下午2:55期間的每5分鐘觸發(fā)
"0 0/5 14,18 * * ?" 在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發(fā)
"0 0-5 14 * * ?" 在每天下午2點到下午2:05期間的每1分鐘觸發(fā)
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44觸發(fā)
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15觸發(fā)
"0 15 10 15 * ?" 每月15日上午10:15觸發(fā)
"0 15 10 L * ?" 每月最后一日的上午10:15觸發(fā)
"0 15 10 ? * 6L" 每月的最后一個星期五上午10:15觸發(fā)
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一個星期五上午10:15觸發(fā)
"0 15 10 ? * 6#3" 每月的第三個星期五上午10:15觸發(fā)
每天早上6點
0 6 * * *
每兩個小時
0 */2 * * *
晚上11點到早上7點之間每兩個小時,早上八點
0 23-7/2,8 * * *
每個月的4號和每個禮拜的禮拜一到禮拜三的早上11點
0 11 4 * 1-3
1月1日早上4點
0 4 1 1 *
quartz的高級特性不僅如此
1 數(shù)據(jù)庫存儲
2 集群支持
3 數(shù)據(jù)庫持久化任務(wù),trigger
4 trigger 的停止,運行
5 任務(wù)的任意添加
6 比corntrigger 更詳盡的任務(wù)安排
7 線程的內(nèi)部數(shù)據(jù)交換