前言
Quartz作爲一種常用的定時任務框架,有一定的使用場景,因此以下介紹Spring 整合 Quartz。因爲是一篇介紹使用的博文,所以只介紹測試通過的配置:
配置詳解
項目版本配置:
//gradle ext{ springVersion = "4.2.6.RELEASE" } dependencies{ //Spring compile "org.springframework:spring-beans:$springVersion" compile "org.springframework:spring-context:$springVersion" compile "org.springframework:spring-core:$springVersion" compile "org.springframework:spring-oxm:$springVersion" compile "org.springframework:spring-web:$springVersion" compile "org.springframework:spring-webmvc:$springVersion" compile "org.springframework:spring-tx:$springVersion" compile "org.springframework:spring-aop:$springVersion" compile "org.springframework:spring-context-support:$springVersion" compile "org.springframework:spring-test:$springVersion" compile "org.springframework:spring-orm:$springVersion" compile "org.springframework:spring-jdbc:$springVersion" // https://mvnrepository.com/artifact/org.apache.archiva.redback.components/spring-quartz compile "org.apache.archiva.redback.components:spring-quartz:2.1" }
Spring+Quartz配置:
spring-quartz.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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 = "BaseJob" class="com.xxx.quartz.DailyJob"/> <bean name="dailyJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="BaseJob"/> </property> <property name="targetMethod"> <!-- 要執行的方法名稱 --> <value>execute</value> </property> </bean> <!-- 時間調度規則 --!> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="dailyJob" /> <!-- 此處具體規則可參考quartz的官方文檔或者其他博客 --!> <!-- 此處意思是每天每小時每隔一分鐘執行一次 --!> <property name="cronExpression" value="0 0/1 * * * ? *" /> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean> </beans>
public class DailyJob { @Resource private DrivingRecordService drivingRecordService; private static final Logger log = Logger.getLogger(DailyJob.class); protected void execute() throws JobExecutionException { log.info("before update"); try{ drivingRecordService.refreshRedisCache(); } catch(Exception e){ e.printStackTrace(); } log.info("finish update"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.xxx.service"/> <import resource="spring-quartz.xml"/> </beans>
web.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
綜述
首先這種配置方式與常規配置方式不同。Job並沒有實現Quartz的job接口,也沒有繼承Spring中的QuartzJobBean類,而是採用了普通的POJO類的形式。在配置中,通過Spring的MethodInvokingJobDetailFactoryBean配合類名和類方法構造出一個Job類,用這個代理的job類配合時間調度器cronTrigger和容器 Scheduler完成任務調度。
這種配置的最大的優點是可以注入Service。而常規配置方法,Job注入Service後空指針異常。這是解決這個問題最簡便的方式。