直接先看spring設定檔:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" http://www.springframework.org/schema/beans "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">
<bean id="testTimerTask" class="com.test.timerTask.TestTimerTask"></bean>
<bean id="serviceFor" class="com.test.timerTask.Service4Job"></bean>
<!-- 第一步:定義任務調度類別,即需要進行業務工作的類別.分為兩種情況,選擇一種來進行調度-->
<!-- 任務調度類別情況1: 使用一個繼承自抽象類別QuartzJobBean的類別來實作任務調度 -->
<bean id="testTimerTaskJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<!-- 注意這裡是注入一個類別全名,而不是類別的引用-->
<property name="jobClass" value="com.test.timerTask.TestTimerTask"></property>
<!-- 表示向testTimerTask類別中註入所需的bean -->
<property name="jobDataAsMap">
<map>
<entry key="service4Job" value-ref="serviceFor"></entry>
</map>
</property>
</bean>
<!-- 任務排程類別情況2: 在不需要繼承自QuartzJobBean的情況下,直接調度現有類別中的業務方法-->
<bean id="noJobBeanTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="serviceFor"></property>
<property name="targetMethod" value="job"></property>
</bean>
<!-- 第二步:定義觸發器,表示啥時候或隔多久進行調度第一步中定義的那些任務-->
<!-- 觸發器情況1: 基於SimpleTriggerBean任務呼叫-->
<bean id="quartzSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="testTimerTaskJob"></property>
<property name="repeatInterval" value="3000"></property>
<property name="startDelay" value="2000"></property>
</bean>
<!-- 觸發器情況2: 基於SimpleTriggerBean任務呼叫-->
<bean id="quartzCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="noJobBeanTaskJob"></property>
<!-- 這裡使用cronExpression的字串形式.0/3 表示每3秒執行一次. -->
<property name="cronExpression" value="0/3 * * * * ?"></property>
</bean>
<!-- 第三步:開始執行任務排程-->
<!-- 開始執行任務調度觸發器-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="quartzCronTrigger"/>
</list>
</property>
</bean>
</beans>
//================================================ ===
兩個類別: TestTimerTask 和Service4Job
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class TestTimerTask extends QuartzJobBean{
private Service4Job service4Job;
public void setService4Job(Service4Job service4Job) {
this.service4Job = service4Job;
}
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
this.service4Job.job();
}
}
public class Service4Job {
public void job(){
System.out.println("**** "+System.currentTimeMillis());
}
}
(thismonth) 轉貼:http: //yanda20056.blog.163.com/blog/static/5650193120091113115434635/
本文來自CSDN博客,轉載請標示:http: //blog.csdn.net/thismonth/archive/2009/12/30/5103969.aspx