spring執行定時任務

okone96發表於2007-03-24

定義一個任務是很簡單的實現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>

<!-- 任務執行週期 2m 關於一些任務的引數請參考JDK doc文件和Spring相關文件--&gt

<property name="period">
<
value>2000value>
property>

<!-- 延時1m 執行任務 --&gt

<property name="delay">
<
value>1000value>
property>
bean>

<!-- 啟動定時器 --&gt

<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中的定時器比較簡單,其提供的任務也比較簡單, 下面來看看使用quartz來執行一個複雜的任務.

首先制定一個任務, 實現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 認為排程: 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.SayHelloTaskUsingQuartzvalue>
property>
bean>

<!-- 關鍵在如下兩個觸發器的配置 --&gt
<!-- 類似於Java的簡單觸發器 --&gt

<bean id="helloTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<
property name="jobDetail">
<
ref bean="sayHelloJob"/>
property>
<
property name="startDelay">
<
value>1000value>
property>
<
property name="repeatInterval">
<
value>3000value>
property>
bean>

<!-- 複雜觸發器 --&gt

<bean id="helloCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<
property name="jobDetail">
<
ref bean="sayHelloJob"/>
property>
<
property name="cronExpression">
<!-- 關鍵在配置此表示式 --&gt
<
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>

關於簡單觸發器和複雜觸發器,查考下面的解釋:

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的介紹:

欄位 允許值 允許的特殊字元
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”在星期域裡表示”星期一、星期三、星期五”.

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/750220/viewspace-906748/,如需轉載,請註明出處,否則將追究法律責任。

相關文章