定義一個任務(wù)是很簡單的實現(xiàn)TimerTask的run方法就可以了.
如下:SayHelloTask.java
package test.timerTask;
import java.util.TimerTask;
public class SayHelloTask extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("測試TimerTask : Hello !!");
}
}
然后是配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >
<beans>
<bean id="sayHelloTask" class="test.timerTask.SayHelloTask"></bean>
<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask">
<ref bean="sayHelloTask"/>
</property>
<!-- 任務(wù)執(zhí)行周期 2m 關(guān)于一些任務(wù)的參數(shù)請參考JDK doc文檔和Spring相關(guān)文檔-->
<property name="period">
<value>2000</value>
</property>
<!-- 延時1m 執(zhí)行任務(wù) -->
<property name="delay">
<value>1000</value>
</property>
</bean>
<!-- 啟動定時器 -->
<bean id="timerBean" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="scheduledTask"/>
</list>
</property>
</bean>
</beans>
測試類如下: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");
}
// 只要加載配置文件就可以了,
}
使用Java中的定時器比較簡單,其提供的任務(wù)也比較簡單, 下面來看看使用quartz來執(zhí)行一個復(fù)雜的任務(wù).
首先制定一個任務(wù), 實現(xiàn)QuartzJobBean中的方法.
package test.timerTask;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class SayHelloTaskUsingQuartz extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println("使用Quartz 認為調(diào)度: Hello!!");
}
}
配置代碼如下:quartzTimer.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >
<beans>
<bean id="sayHelloJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>test.timerTask.SayHelloTaskUsingQuartz</value>
</property>
</bean>
<!-- 關(guān)鍵在如下兩個觸發(fā)器的配置 -->
<!-- 類似于Java的簡單觸發(fā)器 -->
<bean id="helloTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="sayHelloJob"/>
</property>
<property name="startDelay">
<value>1000</value>
</property>
<property name="repeatInterval">
<value>3000</value>
</property>
</bean>
<!-- 復(fù)雜觸發(fā)器 -->
<bean id="helloCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="sayHelloJob"/>
</property>
<property name="cronExpression">
<!-- 關(guān)鍵在配置此表達式 -->
<value>0 49 15 * * ?</value>
</property>
</bean>
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="helloCronTrigger"/>
</property>
</bean>
</beans>
關(guān)于簡單觸發(fā)器和復(fù)雜觸發(fā)器,查考下面的解釋:
Quartz設(shè)計者做了一個設(shè)計選擇來從調(diào)度分離開作業(yè)。Quartz中的觸發(fā)器用來告訴調(diào)度程序作業(yè)什么時候觸發(fā)。框架提供了一把觸發(fā)器類型,但兩個最常用的是SimpleTrigger和CronTrigger。SimpleTrigger為需要簡單打火調(diào)度而設(shè)計。典型地,如果你需要在給定的時間和重復(fù)次數(shù)或者兩次打火之間等待的秒數(shù)打火一個作業(yè),那么SimpleTrigger適合你。另一方面,如果你有許多復(fù)雜的作業(yè)調(diào)度,那么或許需要CronTrigger。
CronTrigger是基于Calendar-like調(diào)度的。當你需要在除星期六和星期天外的每天上午10點半執(zhí)行作業(yè)時,那么應(yīng)該使用CronTrigger。正如它的名字所暗示的那樣,CronTrigger是基于Unix克隆表達式的。
作為一個例子,下面的Quartz克隆表達式將在星期一到星期五的每天上午10點15分執(zhí)行一個作業(yè)。
0 15 10 ? * MON-FRI
下面的表達式
0 15 10 ? * 6L 2002-2005
將在2002年到2005年的每個月的最后一個星期五上午10點15分執(zhí)行作業(yè)。
你不可能用SimpleTrigger來做這些事情。你可以用兩者之中的任何一個,但哪個跟合適則取決于你的調(diào)度需要。
更多詳細介紹參考
此處:
關(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 , - * /
如上面的表達式所示:
詳細說明如下:
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”在星期域里表示”星期一、星期三、星期五”.