Quartz Scheduler 可以对Trigger建立一个监听器,分别对Trigger的各种状态进行监听
实现监听器需要实现TriggerListener接口,然后注册到Scheduler上就可以了。
一:写一个类实现触发器监听接口
package com.gary.operation.jobdemo.example1;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.Trigger.CompletedExecutionInstruction;
import org.quartz.TriggerListener;
public class MyTriggerListener implements TriggerListener {
@Override
public String getName() {
return "MyOtherTriggerListener";
}
/**
* (1)
* Trigger被激发 它关联的job即将被运行
* Called by the Scheduler when a Trigger has fired, and it's associated JobDetail is about to be executed.
*/
@Override
public void triggerFired(Trigger trigger, JobExecutionContext context) {
System.out.println("MyOtherTriggerListener.triggerFired()");
}
/**
* (2)
* Trigger被激发 它关联的job即将被运行,先执行(1),在执行(2) 如果返回TRUE 那么任务job会被终止
* Called by the Scheduler when a Trigger has fired, and it's associated JobDetail is about to be executed
*/
@Override
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
System.out.println("MyOtherTriggerListener.vetoJobExecution()");
return false;
}
/**
* (3) 当Trigger错过被激发时执行,比如当前时间有很多触发器都需要执行,但是线程池中的有效线程都在工作,
* 那么有的触发器就有可能超时,错过这一轮的触发。
* Called by the Scheduler when a Trigger has misfired.
*/
@Override
public void triggerMisfired(Trigger trigger) {
System.out.println("MyOtherTriggerListener.triggerMisfired()");
}
/**
* (4) 任务完成时触发
* 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.
*/
@Override
public void triggerComplete(Trigger trigger, JobExecutionContext context,
CompletedExecutionInstruction triggerInstructionCode) {
System.out.println("MyOtherTriggerListener.triggerComplete()");
}
}
二进行注册: 细节与注册任务监听器一样(http://www.cnblogs.com/daxin/archive/2013/05/29/3105830.html)
Registering A TriggerListener With The Scheduler To Listen To All Triggers
scheduler.getListenerManager().addTriggerListener(myTriggerListener, allTriggers());
Registering A TriggerListener With The Scheduler To Listen To A Specific Trigger
scheduler.getListenerManager().addTriggerListener(myTriggerListener, triggerKeyEquals(triggerKey("myTriggerName", "myTriggerGroup")));
Registering A TriggerListener With The Scheduler To Listen To All Triggers In a Group
scheduler.getListenerManager().addTriggerListener(myTriggerListener, triggerGroupEquals("myTriggerGroup"));