1.是什麼: perform actions based on events occurring within the scheduler.spa
2.分類:a.TriggerListeners ; b.JobListeners ; c.SchedulerListeners 代理
1. 處理與trigger相關的事件,包括: trigger firings, trigger mis-firings , and trigger completions (the jobs fired off by the trigger is finished).code
2. 接口定義:orm
public interface TriggerListener { // 獲取監聽器的名稱 public String getName(); // 1.Called by the Scheduler when a Trigger has fired,
// 2.before it's associated org.quartz.JobDetail is executed.
// 3.called before the vetoJobExecution(..)method
public void triggerFired(Trigger trigger, JobExecutionContext context);
// 1.若是返回true,將veto關聯JobDetail的執行;
// 2. 在triggerFired()方法以後執行
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context);
// 1. 當trigger misfired以後執行此方法
// 2. 須要注意的是:本方法的耗時須要被關注,由於若是耗時太長,那麼當有大量triggers同時misfire時,將會出現問題 public void triggerMisfired(Trigger trigger);
// 1. jobDetail public void triggerComplete(Trigger trigger, JobExecutionContext context, int triggerInstructionCode); }
3. 內置實現:內置實現有倆個,一個是TriggerListenerSupport抽象類,只須要實現getName()指定名稱,別的方法都是空的,實現須要實現的便可。對象
另外一個是BroadcastTriggerListener,這個類又是另一個做用了,至關於一個代理吧,內部有一個TriggerListener實例的集合,當有事件通知時,廣播給內部的listeners。主要代碼以下:blog
private List<TriggerListener> listeners; // 內部經過循環進行請求的分發,這樣就只須要這個listener與trigger關聯了 public void triggerFired(Trigger trigger, JobExecutionContext context) { Iterator<TriggerListener> itr = listeners.iterator(); while(itr.hasNext()) { TriggerListener l = itr.next(); l.triggerFired(trigger, context); } }
1. 處理與job有關的事件,包括:the job is about to be executed, and the job has completed execution.繼承
2. 接口定義:接口
public interface JobListener { public String getName(); // 1.JobDetail即將執行時觸發 // 2.若是job被vetoed了,此方法不會執行 public void jobToBeExecuted(JobExecutionContext context); // 觸發時機:JobDetail即將執行,可是TriggerListener veto 了該執行時會觸發此方法 public void jobExecutionVetoed(JobExecutionContext context); public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException); }
3. 實現:與trigerListener相似,事件
triggerFired jobToBeExecuted job executing。。。 jobWasExecuted triggerComplete
1. 監聽scheduler的相關事件,包括job/trigger的新增移除,執行期間的報錯以及scheduler的關閉。ci
2. 接口定義
public interface SchedulerListener { // 當一個JobDetail被scheduled時。 public void jobScheduled(Trigger trigger); public void jobUnscheduled(String triggerName, String triggerGroup); public void triggerFinalized(Trigger trigger); public void triggersPaused(String triggerName, String triggerGroup); public void triggersResumed(String triggerName, String triggerGroup); public void jobsPaused(String jobName, String jobGroup); public void jobsResumed(String jobName, String jobGroup); public void schedulerError(String msg, SchedulerException cause); public void schedulerStarted(); public void schedulerInStandbyMode(); public void schedulerShutdown(); public void schedulingDataCleared(); }
1.名字必須有;2.能夠繼承support類,沒必要實現接口實現不須要的方法。
1.經過scheduler的ListenerManager 對象註冊在scheduler上,隨帶着一個Matcher,用來指示listener監聽的是那個trigger/job
代碼示意以下;
scheduler.getListenerManager().addJobListener(myJobListener, KeyMatcher.jobKeyEquals(new JobKey("myJobName", "myJobGroup")));
Matcher接口方法以下:
public interface Matcher<T extends Key<?>> extends Serializable { boolean isMatch(T key); public int hashCode(); public boolean equals(Object obj); }
Matcher的類型不少,能夠根據本身的須要靈活應用:EverythingMatcher,GroupMatcher,KeyMatcher,NameMatcher, AndMatcher, NotMatcher, OrMatcher。這些均可以經過靜態方法執行使用,若是有自定義的需求,quartz還提供了StringMatcher抽象類,具體再也不贅述。