本文內(nèi)容
通過(guò)xml配置一個(gè)調(diào)度器Scheduler离斩,所有的任務(wù)通過(guò)該調(diào)度器來(lái)進(jìn)行調(diào)度兴垦,結(jié)合官方提供的數(shù)據(jù)結(jié)構(gòu)以及調(diào)度實(shí)現(xiàn)來(lái)達(dá)到前端控制定時(shí)器的目的嗅定。
調(diào)度器Scheduler的作用
調(diào)度器是Quartz的核心組成部分腺毫,其作用是調(diào)度Job能夠被Trigger觸發(fā),是Quartz的驅(qū)動(dòng)摸袁。
下圖是列出來(lái)的定時(shí)器的核心概念、組成部分以及
調(diào)度器的創(chuàng)建
Scheduler的實(shí)現(xiàn)類有以下幾個(gè):
- RemoteScheduler: 遠(yuǎn)程調(diào)度器义屏;
- StdScheduler:默認(rèn)標(biāo)準(zhǔn)調(diào)度器(最為常用的)靠汁;
- RemoteMBeanScheduler:抽象類,
- JBoss4RMIRemoteMBeanScheduler:是抽象類<code>RemoteMBeanScheduler</code> 的實(shí)現(xiàn)類
調(diào)度器的創(chuàng)建主要是通過(guò)其工廠模式創(chuàng)建的湿蛔,創(chuàng)建方式有:
- JBoss4RMIRemoteMBeanScheduler:是抽象類<code>RemoteMBeanScheduler</code> 的實(shí)現(xiàn)類
- StdSchedulerFactory膀曾;
- 使用一組參數(shù)(java.util.Properties)來(lái)創(chuàng)建和初始化Quartz調(diào)度器;
- 配置參數(shù)一般在quartz.properties中
- 調(diào)用getScheduler方法就能創(chuàng)建和初始化調(diào)度器對(duì)象阳啥;
- 通過(guò)<code>new StdSchedulerFactory().getScheduler();</code> 來(lái)獲取調(diào)度器添谊。
- DirectSchedulerFactory;
定時(shí)器創(chuàng)建以后察迟,可以進(jìn)行增加斩狱、刪除以及顯示Job和Trigger,對(duì)Job進(jìn)行暫停/恢復(fù)等操作扎瓶,調(diào)用了.start()方法后所踊,Scheduler才正式開(kāi)始執(zhí)行Job和Trigger;
StdSchedulerFactory
StdSchedulerFactory依賴于一系列屬性決定如何產(chǎn)生Scheduler概荷,提供配置信息的方式如下:
- 通過(guò)java.util.Properties屬性實(shí)例秕岛;
- 通過(guò)外部屬性文件提供;
- 通過(guò)含有屬性文件內(nèi)容的java.io.InputStream提供误证;
// 1. 無(wú)參方法继薛,會(huì)優(yōu)先加載當(dāng)前工作目錄的quartz.properties,如果未找到愈捅,則試圖從系統(tǒng)的classpath中加載該配置文件遏考。
factory.initialize();
// 2.通過(guò)外部屬性文件提供
// factory.initialize("lx-quartz-scheduler.properties");
// 3. 通過(guò)含有屬性文件內(nèi)容的java.io.InputStream提供
// factory.initialize(new FileInputStream(new File("lx-quartz-scheduler.properties")));
scheduler = factory.getScheduler("quartzScheduler");
前端控制定時(shí)器暫停、恢復(fù)等核心方法
原理:配置定時(shí)器項(xiàng)目默認(rèn)調(diào)度器名字蓝谨,并結(jié)合quartz官方提供的表結(jié)構(gòu)以及自動(dòng)從數(shù)據(jù)庫(kù)加載定時(shí)器配置的機(jī)制灌具,使用調(diào)度器Scheduler的幾個(gè)核心方法結(jié)合數(shù)據(jù)庫(kù)配置達(dá)到使用前端界面控制定時(shí)器恢復(fù)青团、執(zhí)行等操作。
- void start(); // 啟動(dòng)
- void standby();// 掛起
- void shutdown();// 關(guān)閉
- void shutdown(true); // 等待所有正在執(zhí)行的job執(zhí)行完畢之后咖楣,再關(guān)閉scheduler督笆;
暫停定時(shí)器核心方法
修改數(shù)據(jù)庫(kù)的執(zhí)行器狀態(tài)
@Override
public Message updateJobStatus(QuartzJobInfo job) {
QuartzTriggers triggers = new QuartzTriggers();
triggers.setSchedName(job.getSchedName());
triggers.setTriggerGroup(job.getTriggerGroup());
triggers.setTriggerName(job.getTriggerName());
triggers.setTriggerState(job.getTriggerState());
quartzTriggersDao.updateByPrimaryKeySelective(triggers);
if (QRTZ_TRIGGER_STATUS_WAITING.equals(job.getTriggerState())) {
quartzManager.resumeJob(job.getJobName(), job.getJobGroup());
} else {
quartzManager.pauseJob(job.getJobName(), job.getJobGroup());
}
return Message.success();
}
/**
* 調(diào)度器暫停Job
*
* @param jobName job名字
* @param jobGroupName job組名
*/
public void pauseJob(String jobName, String jobGroupName) {
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
try {
scheduler.pauseJob(jobKey);
} catch (SchedulerException e) {
throw new BusinessException("暫停任務(wù)[" + jobGroupName + SPLIT_DOT + jobName + "]中出現(xiàn)異常!", e);
} catch (IllegalArgumentException e) {
throw new BusinessException("暫停任務(wù)[" + jobGroupName + SPLIT_DOT + jobName + "]中出現(xiàn)異常!", e);
}
}
恢復(fù)定時(shí)器核心方法
/**
* 調(diào)度器恢復(fù)Job
*
* @param jobName job名字
* @param jobGroupName job組名
*/
public void resumeJob(String jobName, String jobGroupName) {
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
try {
scheduler.resumeJob(jobKey);
} catch (SchedulerException e) {
throw new BusinessException("恢復(fù)任務(wù)[" + jobGroupName + SPLIT_DOT + jobName + "]中出現(xiàn)異常!", e);
}
}
效果
回顧
定時(shí)器配置:SimpleTrigger和CronTrigger
spring-servlet.xml 中添加配置,用于托付給spring來(lái)管理截歉;
- 配置JobDetail胖腾;
- 配置Trigger;
- 配置Scheduler瘪松;
首先配置JobDetail咸作,前面的概念中也了解到定義的方式,方式如下: - MethodInvokingJobDetailFactoryBean
- JobDetailFactoryBean:可以傳入?yún)?shù)jobDataMap宵睦;比較靈活记罚;
- extends QuartzJobBean
<description>Quartz定時(shí)器配置</description>
<!-- JobDetail 定義方式一: MethodInvokingJobDetailFactoryBean-->
<bean id="lxJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定類 -->
<property name="targetObject" ref="lxJobDetailBean"/>
<!-- 指定方法 -->
<property name="targetMethod" value="init"/>
</bean>
<!-- JobDetail 定義方式二:JobDetailFactoryBean -->
<bean id="lxJobDetailBean1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.weyoung.platform.quartz.schedule.LxQuartzJobBean"/>
<!-- 傳入自定義參數(shù) -->
<property name="jobDataMap">
<map>
<entry key="anotherBean" value-ref="anotherBean"/>
</map>
</property>
<property name="durability" value="true"/>
</bean>
<!-- Trigger方式一:SimpleTriggerFactoryBean 距離當(dāng)前時(shí)間1秒之后執(zhí)行,之后每隔兩秒鐘執(zhí)行一次 -->
<bean id="lxSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="lxJobDetail"/>
<property name="startDelay" value="1000"/>
<property name="repeatInterval" value="2000"/>
</bean>
<!-- Trigger方式二:CronTriggerFactoryBean 每隔五秒鐘執(zhí)行一次 -->
<bean id="lxCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="lxJobDetailBean1"/>
<property name="cronExpression" value="0/5 * * ? * *"/>
</bean>
<bean id="lxQuartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="lxJobDetail"/>
<ref bean="lxJobDetailBean1"/>
</list>
</property>
<property name="triggers">
<list>
<ref bean="lxSimpleTrigger"/>
<ref bean="lxCronTrigger"/>
</list>
</property>
</bean>
其中壳嚎,定義的類lxJobDetailBean桐智、lxJobDetailBean1、anotherBean代碼如下:
LxJobDetailBean.java
package com.weyoung.platform.quartz.schedule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @功能描述:
* @時(shí)間: 2019/6/29 11:26
* @author: Mr.wang
*/
@Component("lxJobDetailBean")
public class LxJobDetailBean {
private static final Logger LOGGER = LoggerFactory.getLogger(LxJobDetailBean.class);
public void init() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
LOGGER.info("-----------lxJobDetailBean.init-----------" + sdf.format(date));
}
}
LxQuartzJobBean.java
package com.weyoung.platform.quartz.schedule;
import com.weyoung.platform.quartz.entity.AnotherBean;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @功能描述:
* @時(shí)間: 2019/6/29 11:33
* @author: Mr.wang
*/
public class LxQuartzJobBean extends QuartzJobBean {
// 定義一個(gè)需要傳入的參數(shù)烟馅,并給一個(gè)setter方法
private AnotherBean anotherBean;
private static final Logger LOGGER = LoggerFactory.getLogger(LxQuartzJobBean.class);
/**
* 業(yè)務(wù)邏輯
*
* @param jobExecutionContext
* @throws JobExecutionException
*/
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
LOGGER.info("---------LxQuartzJobBean.executeInternal---------" + sdf.format(date));
}
public void setAnotherBean(AnotherBean anotherBean) {
this.anotherBean = anotherBean;
}
}
AnotherBean.java
package com.weyoung.platform.quartz.entity;
import org.springframework.stereotype.Component;
/**
* @功能描述:
* @時(shí)間: 2019/6/29 11:36
* @author: Mr.wang
*/
@Component("anotherBean")
public class AnotherBean {
public void someMessage () {
}
}
Quartz中作業(yè)存儲(chǔ)方式
- RAMJobStore:作業(yè)说庭、觸發(fā)器、調(diào)度信息存儲(chǔ)在內(nèi)存中郑趁,這種方式存取速度比較快刊驴,但是如果定時(shí)器項(xiàng)目重啟或者崩潰的話,存儲(chǔ)的信息都會(huì)丟失寡润;
- JDBC作業(yè)存儲(chǔ):作業(yè)捆憎、觸發(fā)器、調(diào)度信息存儲(chǔ)在數(shù)據(jù)庫(kù)中梭纹,支持事務(wù)躲惰,支持集群;
前面的筆記里面記錄了Quartz官方提供的表結(jié)構(gòu)及創(chuàng)建腳本等变抽,使用該腳本創(chuàng)建數(shù)據(jù)庫(kù)础拨;
修改調(diào)度器信息及使用quartz.propertie文件配置
我的項(xiàng)目暫時(shí)是把定時(shí)器放在項(xiàng)目主程序中,也是使用同一個(gè)數(shù)據(jù)庫(kù)绍载,當(dāng)然也可以分開(kāi)太伊。如果需要自己配置數(shù)據(jù)庫(kù)的話,在quartz.properties中配置就行逛钻。
因此,修改<code>spring-quartz.xml</code>配置如下:
<!-- 調(diào)度器锰提,調(diào)度器的id不要改曙痘,數(shù)據(jù)庫(kù)中記錄調(diào)度器名稱 -->
<bean id="lxQuartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 使用spring中配置的數(shù)據(jù)源芳悲,需要在這兒配置 -->
<property name="dataSource" ref="masterDataSource"/>
<property name="overwriteExistingJobs" value="true"/><!--覆蓋JOB:true、以數(shù)據(jù)庫(kù)中已經(jīng)存在的為準(zhǔn): -->
<property name="autoStartup" value="true"/><!--自啟動(dòng)-->
<property name="startupDelay" value="20"/> <!-- 定時(shí)任務(wù)延時(shí)啟動(dòng)边坤,程序啟動(dòng)后20秒再啟動(dòng)定時(shí)任務(wù) -->
<!-- 調(diào)度器配置文件 -->
<property name="configLocation" value="classpath:config/quartz.properties"/>
</bean>
quartz.properties文件的配置
#===========================================================================
# Configure Main Scheduler Properties 調(diào)度器屬性
# ===========================================================================
org.quartz.scheduler.instanceName=lxQuartzScheduler
org.quartz.scheduler.instanceid=AUTO
org.quartz.scheduler.rmi.export=false
org.quartz.scheduler.rmi.proxy=false
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false
# ===========================================================================
# Configure ThreadPool 線程池屬性
# ===========================================================================
#線程池的實(shí)現(xiàn)類(一般使用SimpleThreadPool即可滿足幾乎所有用戶的需求)
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
#指定線程數(shù)名扛,至少為1(無(wú)默認(rèn)值)(一般設(shè)置為1-100直接的整數(shù)合適)
org.quartz.threadPool.threadCount=10
#設(shè)置線程的優(yōu)先級(jí)(最大為java.lang.Thread.MAX_PRIORITY 10,最小為T(mén)hread.MIN_PRIORITY 1茧痒,默認(rèn)為5)
org.quartz.threadPool.threadPriority=5
===========================================================================
# Configure JobStore 存儲(chǔ)調(diào)度信息(工作肮韧,觸發(fā)器和日歷等)
# ===========================================================================
# 信息保存時(shí)間 默認(rèn)值60秒
org.quartz.jobStore.misfireThreshold=60000
#保存job和Trigger的狀態(tài)信息到內(nèi)存中的類
#org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
# Mysql需要使用下面的鏈接
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#org.quartz.jobStore.useProperties = false
## 我們采用程序中的數(shù)據(jù)源,因此這塊不配置旺订,在spring-quartz.xml中配置
#org.quartz.jobStore.dataSource = masterDataSource
org.quartz.jobStore.tablePrefix = QRTZ_
#org.quartz.jobStore.isClustered = false
#============================================================================
# Configure Datasources
#============================================================================
#org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver
#org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/SSM_NOTE
#org.quartz.dataSource.myDS.user=root
#org.quartz.dataSource.myDS.password=lucifer
#org.quartz.dataSource.myDS.maxConnections=5
# ===========================================================================
# Configure SchedulerPlugins 插件屬性 配置
# ===========================================================================
# 自定義插件
org.quartz.plugin.triggHistory.class=org.quartz.plugins.history.LoggingTriggerHistoryPlugin
org.quartz.plugin.triggHistory.triggerFiredMessage=Trigger {1}.{0} fired job {6}.{5} at: {4, date, HH:mm:ss MM/dd/yyyy}
org.quartz.plugin.triggHistory.triggerCompleteMessage=Trigger {1}.{0} completed firing job {6}.{5} at {4, date, HH:mm:ss MM/dd/yyyy} with resulting trigger instruction code: {9}
qrtz_triggers表中的字段特別解析:
MISFIRE_INSTR:Misfire處理規(guī)則:
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
// 1
scheduleBuilder.withMisfireHandlingInstructionFireNow();
// -1
scheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
// 5
scheduleBuilder.withMisfireHandlingInstructionNextWithExistingCount();
// 4
scheduleBuilder.withMisfireHandlingInstructionNextWithRemainingCount();
// 2
scheduleBuilder.withMisfireHandlingInstructionNowWithExistingCount();
// 3
scheduleBuilder.withMisfireHandlingInstructionNowWithRemainingCount();
/*
1:withMisfireHandlingInstructionFireNow
——以當(dāng)前時(shí)間為觸發(fā)頻率立即觸發(fā)執(zhí)行
——執(zhí)行至FinalTIme的剩余周期次數(shù)
——以調(diào)度或恢復(fù)調(diào)度的時(shí)刻為基準(zhǔn)的周期頻率弄企,F(xiàn)inalTime根據(jù)剩余次數(shù)和當(dāng)前時(shí)間計(jì)算得到
——調(diào)整后的FinalTime會(huì)略大于根據(jù)starttime計(jì)算的到的FinalTime值
-1:withMisfireHandlingInstructionIgnoreMisfires
——以錯(cuò)過(guò)的第一個(gè)頻率時(shí)間立刻開(kāi)始執(zhí)行
——重做錯(cuò)過(guò)的所有頻率周期
——當(dāng)下一次觸發(fā)頻率發(fā)生時(shí)間大于當(dāng)前時(shí)間以后,按照Interval的依次執(zhí)行剩下的頻率
——共執(zhí)行RepeatCount+1次
2:withMisfireHandlingInstructionNowWithExistingCount
——以當(dāng)前時(shí)間為觸發(fā)頻率立即觸發(fā)執(zhí)行
——執(zhí)行至FinalTIme的剩余周期次數(shù)
——以調(diào)度或恢復(fù)調(diào)度的時(shí)刻為基準(zhǔn)的周期頻率区拳,F(xiàn)inalTime根據(jù)剩余次數(shù)和當(dāng)前時(shí)間計(jì)算得到
——調(diào)整后的FinalTime會(huì)略大于根據(jù)starttime計(jì)算的到的FinalTime值
3:withMisfireHandlingInstructionNowWithRemainingCount
——以當(dāng)前時(shí)間為觸發(fā)頻率立即觸發(fā)執(zhí)行
——執(zhí)行至FinalTIme的剩余周期次數(shù)
——以調(diào)度或恢復(fù)調(diào)度的時(shí)刻為基準(zhǔn)的周期頻率拘领,F(xiàn)inalTime根據(jù)剩余次數(shù)和當(dāng)前時(shí)間計(jì)算得到
——調(diào)整后的FinalTime會(huì)略大于根據(jù)starttime計(jì)算的到的FinalTime值
4:withMisfireHandlingInstructionNextWithRemainingCount
——不觸發(fā)立即執(zhí)行
——等待下次觸發(fā)頻率周期時(shí)刻,執(zhí)行至FinalTime的剩余周期次數(shù)
——以startTime為基準(zhǔn)計(jì)算周期頻率樱调,并得到FinalTime
——即使中間出現(xiàn)pause约素,resume以后保持FinalTime時(shí)間不變
5:withMisfireHandlingInstructionNextWithExistingCount
——不觸發(fā)立即執(zhí)行
——等待下次觸發(fā)頻率周期時(shí)刻,執(zhí)行至FinalTime的剩余周期次數(shù)
——以startTime為基準(zhǔn)計(jì)算周期頻率笆凌,并得到FinalTime
——即使中間出現(xiàn)pause圣猎,resume以后保持FinalTime時(shí)間不變
*/
// Cron的MisFire策略;使用最多的
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("");
// -1: 以錯(cuò)過(guò)的第一個(gè)頻率時(shí)間立刻開(kāi)始執(zhí)行,重做錯(cuò)過(guò)的所有頻率周期后當(dāng)下一次觸發(fā)頻率發(fā)生時(shí)間大于當(dāng)前時(shí)間后乞而,再按照正常的Cron頻率依次執(zhí)行送悔;
cronScheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
// 2: 不觸發(fā)立即執(zhí)行,等待下次Cron觸發(fā)頻率到達(dá)時(shí)刻開(kāi)始按照Cron頻率依次執(zhí)行晦闰;
cronScheduleBuilder.withMisfireHandlingInstructionDoNothing();
// 1:以當(dāng)前時(shí)間為觸發(fā)頻率立刻觸發(fā)一次執(zhí)行放祟,然后按照Cron頻率依次執(zhí)行
cronScheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
錯(cuò)誤
org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzJobServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzManager': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lxQuartzScheduler' defined in class path resource [spring/spring-quartz.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
是因?yàn)槿鄙賘ar包,添加如下依賴,即可
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>