quartz是一個(gè)高質(zhì)量的任務(wù)調(diào)度軟件包。其主要組成部分為:
Scheduler接口: quartz的執(zhí)行線程,它根據(jù)Trigger決定調(diào)度時(shí)刻,根據(jù)JobDetail的說(shuō)明實(shí)例化并運(yùn)行Job
JobDetail類: 可持久化的任務(wù)描述信息。任務(wù)雖然分組,但是僅用作管理標(biāo)示,任務(wù)之間并無(wú)實(shí)質(zhì)性關(guān)聯(lián), 例如無(wú)法定義job chain。
Trigger類:任務(wù)的調(diào)度策略。這里的特點(diǎn)是調(diào)度策略與任務(wù)描述分開,調(diào)度策略和任務(wù)描述都可以分別在Scheduler注冊(cè),然后再關(guān)聯(lián)起來(lái)。JobDetail與Trigger的關(guān)系是一對(duì)多。
JobDataMap: 將任務(wù)的運(yùn)行時(shí)可持久化狀態(tài)信息從JobDetail類中分離出來(lái)
Job接口: 任務(wù)的執(zhí)行代碼
StatefulJob接口: 無(wú)狀態(tài)任務(wù)對(duì)應(yīng)的JobDataMap可以認(rèn)為是只讀的,而有狀態(tài)的任務(wù)在多次執(zhí)行過(guò)程中保留對(duì)JobDataMap所作的修改,一個(gè)后果是有狀態(tài)任務(wù)無(wú)法被并發(fā)執(zhí)行。
JobExecutionException類: 可以通過(guò)JobExecutionException調(diào)整調(diào)度程序的下一步動(dòng)作
Calendar接口: 用于從trigger的調(diào)度計(jì)劃中排除某些時(shí)間段,例如假期等。
以上幾個(gè)部分的交互關(guān)系如下:
class JobImpl implements Job{
??? public void execute(JobExecutionContext context) throws JobExecutionException{
??????? JobDetail detail = context.getJobDetail();
??????? JobDataMap dataMap = detail.getJobDataMap();
??????? ...
??? }
}
scheduler.addCalendar("myHolidays", holidayCalendar, false);
trigger.setCanlendarName("myHolidays");
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, JobImpl.class);
scheduler.scheduleJob(jobDetail, trigger);
JobDetail可以設(shè)置如下屬性:
1. Durability: non-durable的任務(wù)當(dāng)不再與任何active trigger關(guān)聯(lián)的時(shí)候?qū)?huì)從scheduler中被自動(dòng)刪除。
2. Volatility: volatile的任務(wù)在scheduler的兩次啟動(dòng)之間不會(huì)被持久化
3. RequestsRecovery: 如果在執(zhí)行過(guò)程中程序意外崩潰,標(biāo)記為"request recovery"的任務(wù)在scheduler重起之后將會(huì)被再次執(zhí)行,此時(shí)JobExecutionContext.isRecovering()返回true.
Trigger可以設(shè)置如下屬性:
1. misfireInstruction: 設(shè)定當(dāng)trigger錯(cuò)過(guò)了觸發(fā)時(shí)刻的時(shí)候需要采取的處理策略
SimpleTrigger按照固定的時(shí)間間隔進(jìn)行觸發(fā)
startTime, endTime, repeatCount, repeatInterval
CronTrigger按照日歷間隔進(jìn)行觸發(fā)
seconds minutes hours day-of-month month day-of-week
在quartz內(nèi)部,QuartzSchedulerThread按照時(shí)間順序選擇trigger(沒有任務(wù)優(yōu)先級(jí)的概念), 然后在JobRunShell中運(yùn)行Job。
JobRunShell中的調(diào)用順序如下:
TriggerListener.triggerFired
??? Called by the Scheduler when a Trigger has fired, and it's associated JobDetail is about to be executed.
TriggerListener.vetoJobExecution
??? Called by the Scheduler when a Trigger has fired, and it's associated JobDetail is about to be executed.
JobListener.jobToBeExecuted
??? Called by the Scheduler when a JobDetail is about to be executed (an associated Trigger has occured).
Job.execute
??? Called by the Scheduler when a Trigger fires that is associated with the Job.
?
JobListener.jobWasExecuted
??? Called by the Scheduler after a JobDetail has been executed, and be for the associated Trigger's triggered(xx) method has
been called.
Trigger.executionComplete
??? Called after the Scheduler has executed the JobDetail associated with the Trigger in order to get the final instruction
code from the trigger.
TriggerListener.triggerComplete
???? Called by the Scheduler when a Trigger has fired, it's associated JobDetail has been executed, and it's triggered(xx)
method has been called.
SchedulerListener.triggerFinalized [if(trigger.getNextFireTime() == null)]
???? Called by the Scheduler when a Trigger has reached the condition in which it will never fire again.