近期接到一個開發任務業務方面不再詳述要求每天定時執行一段應用程序用來同步倆個數據庫
正好近期一直在用spring做相關開發嘗試了下Quartz感覺不錯目前也正在學習中
Quartz使用TriggerJob以及JobDetail等對象來進行各種類型的任務調度Spring對其提供了很好的支持
建立一個時序調度程序的過程如下 ) 配置Job及JobDetail Bean定義執行某個類裡的特定方法 ) 配置Trigger Bean定義觸發JobDetail的時間規律 ) 配置SchedulerFactoryBean負責調度實際的Trigger時序調度的運行模式有兩種 ) 一種是在某個特定時間自動運行例如每天凌晨點備份數據每月初號統計上月的數據等我們稱之為定時調度
)另一種是在服務啟動一段時間後開始運行再每隔一段時間再次運行如系統監控程序每個分鐘就要測試一下數據庫是否連接正常我們稱之為重復調度
下面舉出兩個簡單的范例詳細原理和配置方法請參考 Spring 官方文檔和程序范例的ApplicationContextquartzxml 文件
一定時調度參數配置是在每天的:的時間自動執行CronSimpleJobjava 類中的 executeInternal()方法在系統控制台上輸 出日志內容
ApplicationContextquartzxml內容
view plaincopy to clipboardprint?
<bean id=scheduler class=orgspringframeworkschedulingquartzSchedulerFactoryBean>
<property name=triggers>
<list>
<ref local=cronSimpleTrigger/>
</list>
</property>
</bean>
!定時簡單JobDetail>
<bean id=cronSimpleJob class=orgspringframeworkschedulingquartzJobDetailBean>
<property name=jobClass value=testquartzCronSimpleJob/>
</bean>
<!定時簡單Trigger>
<bean id=cronSimpleTrigger class=orgspringframeworkschedulingquartzCronTriggerBean>
<property name=jobDetail ref=cronSimpleJob/>
<property name=cronExpression value= * * ?/>
</bean>
<bean id=scheduler class=orgspringframeworkschedulingquartzSchedulerFactoryBean>
<property name=triggers>
<list>
<ref local=cronSimpleTrigger/>
</list>
</property>
</bean>
<!定時簡單JobDetail>
<bean id=cronSimpleJob class=orgspringframeworkschedulingquartzJobDetailBean>
<property name=jobClass value=testquartzCronSimpleJob/>
</bean>
<!定時簡單Trigger>
<bean id=cronSimpleTrigger class=orgspringframeworkschedulingquartzCronTriggerBean>
<property name=jobDetail ref=cronSimpleJob/>
<property name=cronExpression value= * * ?/>
</bean>CronSimpleJobjava文件內容
view plaincopy to clipboardprint?
package testquartz;
import monsloggingLog;
import monsloggingLogFactory;
import orgquartzJobExecutionContext;
import orgspringframeworkschedulingquartzQuartzJobBean;
public class CronSimpleJob extends QuartzJobBean
{
private final Log logger = LogFactorygetLog(getClass());
protected void executeInternal(JobExecutionContext context)
{
loggerwarn(getClass()getName() + out it!);
}
}
package testquartz;
import monsloggingLog;
import monsloggingLogFactory;
import orgquartzJobExecutionContext;
import orgspringframeworkschedulingquartzQuartzJobBean;
public class CronSimpleJob extends QuartzJobBean
{
private final Log logger = LogFactorygetLog(getClass());
protected void executeInternal(JobExecutionContext context)
{
loggerwarn(getClass()getName() + out it!);
}
}
二重復調度view plaincopy to clipboardprint?
在服務器啟動毫秒即秒鐘後調度程序首次執行以後每隔毫秒即分鐘再次執行
在服務器啟動毫秒即秒鐘後調度程序首次執行以後每隔毫秒即分鐘再次執行view plaincopy to clipboardprint?
自動執行RepeatMethodJobjava類中的testRepeatMethod()方法在系統控制台上輸出日志內容
自動執行RepeatMethodJobjava類中的testRepeatMethod()方法在系統控制台上輸出日志內容view plaincopy to clipboardprint?
對於相同的JobDetail當指定多個Trigger時 很可能第一個job完成之前第二個job就開始了
對於相同的JobDetail當指定多個Trigger時 很可能第一個job完成之前第二個job就開始了view plaincopy to clipboardprint?
指定concurrent設為false多個job不會並發執行第二個job將不會在第一個job完成之前開始
指定concurrent設為false多個job不會並發執行第二個job將不會在第一個job完成之前開始view plaincopy to clipboardprint?
ApplicationContextquartzxml內容
ApplicationContextquartzxml內容
view plaincopy to clipboardprint?
<!邏輯bean>
<bean id=repeatMethodJob class=testquartzRepeatMethodJob/>
<!間隔方法JobDetail>
<bean id=repeatMethodJobDetail class=orgspringframeworkschedulingquartzMethodInvokingJobDetailFactoryBean>
<property name=targetObject ref=repeatMethodJob/>
<property name=targetMethod value=testRepeatMethod/>
<property name=concurrent value=false/>
</bean>
<!間隔方法Trigger>
<bean id=repeatMethodTrigger class=orgspringframeworkschedulingquartzSimpleTriggerBean>
<property name=jobDetail ref=repeatMethodJobDetail/>
<property name=startDelay value=/>
<property name=repeatInterval value=/>
</bean>
<!邏輯bean>
<bean id=repeatMethodJob class=testquartzRepeatMethodJob/>
<!間隔方法JobDetail>
<bean id=repeatMethodJobDetail class=orgspringframeworkschedulingquartzMethodInvokingJobDetailFactoryBean>
<property name=targetObject ref=repeatMethodJob/>
<property name=targetMethod value=testRepeatMethod/>
<property name=concurrent value=false/>
</bean>
<!間隔方法Trigger>
<bean id=repeatMethodTrigger class=orgspringframeworkschedulingquartzSimpleTriggerBean>
<property name=jobDetail ref=repeatMethodJobDetail/>
<property name=startDelay value=/>
<property name=repeatInterval value=/>
</bean>view plaincopy to clipboardprint?
RepeatMethodJobjava文件內容
RepeatMethodJobjava文件內容view plaincopy to clipboardprint?
<PRE class=java name=code>package testquartz;
import monsloggingLog;
import monsloggingLogFactory;
public class RepeatMethodJob
{
private final Log logger = LogFactorygetLog(getClass());
public void testRepeatMethod(){
loggerwarn(getClass()getName()+out it!);
}
}
</PRE>
<PRE class=java name=code> </PRE>
<PRE class=java name=code> </PRE>
<PRE class=java name=code>關於cronExpression</PRE>
<PRE class=java name=code><IMG height= alt= src= width= twffan=done></PRE>
<PRE class=java name=code><IMG alt= src= twffan=done></PRE>
view plaincopy to clipboardprint?package testquartz; import monsloggingLog; import monsloggingLogFactory; public class RepeatMethodJob { private final Log logger = LogFactorygetLog(getClass()); public void testRepeatMethod(){ loggerwarn(getClass()getName()+out it!); } } package testquartz;
import monsloggingLog;
import monsloggingLogFactory;
public class RepeatMethodJob
{
private final Log logger = LogFactorygetLog(getClass());
public void testRepeatMethod(){
loggerwarn(getClass()getName()+out it!);
}
}
view plaincopy to clipboardprint?
view plaincopy to clipboardprint?
view plaincopy to clipboardprint?關於cronExpression 關於cronExpression
view plaincopy to clipboardprint?<IMG height= alt= src= width= twffan=done>
view plaincopy to clipboardprint?<IMG alt= src= twffan=done>
view plaincopy to clipboardprint?
view plaincopy to clipboardprint?
關於Quartz詳解可參考<A >>;/A>寫的比較深入
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28087.html