零、引言
關於Spring集成Quartz有2種方法:
1. JobDetailBean.
2. MethodInvokeJobDetailFactoryBean.
以下從自身使用和理解以及掌握的知識對其進行闡述。
需要注意的是,在使用Spring集成Quartz的時候,一定不要忘記引入spring-support這個包:
<!-- spring-support.jar 這個jar 文件包含支持UI模版(Velocity,FreeMarker,JasperReports),郵件服務,腳本服務(JRuby), 緩存Cache(EHCache),任務計划Scheduling(uartz)方面的類。 外部依賴spring-context, (spring-jdbc, Velocity, FreeMarker, JasperReports, BSH, Groovy, JRuby, Quartz, EHCache) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.6.RELEASE</version> </dependency>
它協助Spring集成了很多有用的第三方庫,包括了郵件服務、定時任務、緩存等。。。
當然也不要忘記引入Quart的jar包:
<!-- quartz定時任務 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency>
一、JobDetailBean
1. 創建一個Job方法,此方法必須繼承QuartzJobBean或者實現Job方法。
public class TestJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { System.out.println(TimeUtils.getCurrentTime()); } }
2. 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.mc.bsframe.job.TestJob"></property> <property name="durability" value="true"></property> </bean> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="jobDetail" /> <property name="startDelay" value="3000" /> <property name="repeatInterval" value="2000" /> </bean> <!-- 總管理類 如果將lazy-init='false'那么容器啟動就會執行調度程序 --> <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 管理trigger --> <property name="triggers"> <list> <ref bean="simpleTrigger" /> </list> </property> </bean> </beans>
二、MethodInvokeJobDetailFactoryBean
1. 創建一個Job類,此類不需要繼承任何類或者實現任何接口:
package com.mc.bsframe.job; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.TriggerKey; import org.quartz.impl.triggers.SimpleTriggerImpl; import org.springframework.beans.factory.annotation.Autowired; import com.mc.bsframe.service.TestService; import com.mc.bsframe.util.TimeUtils; public class TestJob2 { public void doSomething() { System.err.println("****:" + TimeUtils.getCurrentTime()); } }
2. 配置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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 要執行任務的任務類。 --> <bean id="testQuartz" class="com.mc.bsframe.job.TestJob2"> </bean> <!-- 將需要執行的定時任務注入JOB中。 --> <bean id="testJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="testQuartz"></property> <!-- 任務類中需要執行的方法 --> <property name="targetMethod" value="doSomething"></property> <!-- 上一次未執行完成的,要等待有再執行。 --> <property name="concurrent" value="false"></property> </bean> <!-- 基本的定時器,會綁定具體的任務。 --> <bean id="testTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="testJob"></property> <property name="startDelay" value="3000"></property> <property name="repeatInterval" value="2000"></property> </bean> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="testTrigger"></ref> </list> </property> </bean> </beans>
綜上:定時任務的基本配置完成。
三、兩種方法的說明
使用QuartzJobBean,需要繼承。而使用MethodInvokeJobDetailFactoryBean則需要指定targetObject(任務實例)和targetMethod(實例中要執行的方法)
后者優點是無侵入,業務邏輯簡單,一目了然,缺點是無法持久化(目前還不太清楚這點!)
從我使用的經驗來說,我更推薦的第二種,其中一個很重要的原因就是因為定時任務中注入相關Service的時候,后者可以直接注入,而前者還需要進行Schedular的替換修改。
