1. 準備好要被排程的job Bean,由於我這邊使用的是org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean 這個Class,因此我要被排程的程式不需要去implements org.quartz.Job,使用一般的Bean即可,我的Bean為KeywordStatisticJob,要被執行的方法為execute(),準備好要被排程的程式後,可以開始設定spring-quartz.xml。
2.我的spring-quartz.xml如下:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <bean id="targetClass" class="com.gorilla.util.quartz.PageStatisticJob"/> <bean id="targetKeywordStatisticJob" class="com.gorilla.util.quartz.KeywordStatisticJob"/> <bean id="jobPageStatistic" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="targetClass"/> <property name="targetMethod" value="execute"/> </bean> <bean id="jobKeywordStatistic" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="targetKeywordStatisticJob"/> <property name="targetMethod" value="execute"/> </bean> <!-- Run the job every 15 minutes --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="jobPageStatistic" /> <property name="cronExpression" value="0 0/10 * * * ?" /> </bean> <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="jobKeywordStatistic" /> <property name="cronExpression" value="0 0/11 * * * ?" /> </bean> <!-- scheduler factory --> <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="jobPageStatistic" /> <ref bean="jobKeywordStatistic" /> </list> </property> <property name="triggers"> <list> <ref bean="cronTrigger" /> <ref bean="cronTrigger2" /> </list> </property> <property name="quartzProperties"> <props> <prop key="org.quartz.scheduler.instanceName">EYMSheduler</prop> <prop key="org.quartz.threadPool.threadCount">5</prop> <!-- 可執行job的thread數量 --> <prop key="org.quartz.jobStore.class">org.quartz.simpl.RAMJobStore</prop> <prop key="org.quartz.plugin.triggHistory.class">org.quartz.plugins.history.LoggingTriggerHistoryPlugin</prop> <prop key="org.quartz.plugin.triggHistory.triggerFiredMessage"> Trigger [{1}.{0}] fired job [{6}.{5}] scheduled at: {2, date, yyyy-MM-dd HH:mm:ss.SSS}, next scheduled at: {3, date, yyyy-MM-dd HH:mm:ss.SSS} </prop> <prop key="org.quartz.plugin.triggHistory.triggerCompleteMessage"> Trigger {1}.{0} completed firing job {6}.{5} at {4, date, yyyy-MM-dd HH:mm:ss.SSS} with resulting trigger instruction code: {9} </prop> <prop key="org.quartz.plugin.triggHistory.triggerMisfiredMessage"> Trigger {1}.{0} misfired job {6}.{5} at: {4, date, yyyy/MM/dd HH:mm:ss }. Should have fired at: {3, date,yyyy/MM/dd HH:mm:ss} </prop> <prop key="org.quartz.jobStore.misfireThreshold">500</prop> </props> </property> <property name="overwriteExistingJobs" value="true" /> </bean> </beans>這邊稍微說明一下設定檔的內容:
a. 首先先將job bean定義如下
<bean id="targetKeywordStatisticJob" class="com.gorilla.util.quartz.KeywordStatisticJob"/>
b.接著新增一個MethodInvokingJobDetailFactoryBean這樣的類別設定如下:
<bean id="jobKeywordStatistic" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="targetKeywordStatisticJob"/>
<property name="targetMethod" value="execute"/>
</bean>
其中 targetObject 這個屬性是去參考到要執行的Job類別的 Bean Id,targetMethod參考到job要被排程執行的method name,這裡為execute。
c.再新增一個CronTrigger類別的Bean,如下設定:
<bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobKeywordStatistic" />
<property name="cronExpression" value="0 0/10 * * * ?" />
</bean>
這個cronTrigger物件有兩個屬性,一個為jobDetail,指定他要排程的目標為jobKeywordStatistic,cronExpression屬性則是設定多久執行一次目標,這邊設定為每11分鐘執行一次jobKeywordStatistic。
d.最後指定我們的schedulerFactory Bean,並且設定它,將上述的MethodInvokingJobDetailFactoryBean以及CronTriggerFactoryBean分別設定置它的schedulerFactory Bean的jobDetails property以及triggers propety,如下:
<!-- scheduler factory --> <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="jobPageStatistic" /> <ref bean="jobKeywordStatistic" /> </list> </property> <property name="triggers"> <list> <ref bean="cronTrigger" /> <ref bean="cronTrigger2" /> </list> </property>
e.最後將spring-quartz.xml import進 spring.xml,即完成整個設定,設定如下:
<import resource="classpath:conf/spring-quartz.xml" />