2009年6月22日 星期一

[Quartz]JobListener, TriggerListener

透過JobListener, TriggerListener
可以監控job和trigger的運作情況

JobListener code
public class JobMonitor implements JobListener {
static Logger logger = Logger.getLogger(JobMonitor.class);

public String getName() {
return getClass().getSimpleName();
}

public void jobToBeExecuted(JobExecutionContext context) {
String sJobName = context.getJobDetail().getName();
LogUtil.logDebug(logger, "The job "+sJobName+" will be execute!");
}

public void jobExecutionVetoed(JobExecutionContext context) {
String sJobName = context.getJobDetail().getName();
LogUtil.logDebug(logger, "The job "+sJobName+" was Vetoed by trigger!");
}

public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
String sJobName = context.getJobDetail().getName();
LogUtil.logDebug(logger, "The job "+sJobName+" was executed!");
}
}

TriggerListener code
public class TriggerMonitor implements TriggerListener {
static Logger logger = Logger.getLogger(TriggerMonitor.class);

public String getName() {
return getClass().getSimpleName();
}

public void triggerFired(Trigger trigger, JobExecutionContext context) {
String sTriggerName = trigger.getName();
LogUtil.logDebug(logger, sTriggerName + " was fired");
}

public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
String sTriggerName = trigger.getName();
LogUtil.logDebug(logger, sTriggerName + " was not vetoed");
return false;
}

public void triggerMisfired(Trigger trigger) {
String sTriggerName = trigger.getName();
LogUtil.logDebug(logger, sTriggerName + " misfired");
}

public void triggerComplete(Trigger trigger, JobExecutionContext context, int triggerInstructionCode) {
String sTriggerName = trigger.getName();
LogUtil.logDebug(logger, sTriggerName + " is complete");
}
}

然後在scheduler加入這兩個listener
JobListener jobMonitor = new JobMonitor();
scheduler.addGlobalJobListener(jobMonitor);
TriggerListener triggerMonitor = new TriggerMonitor();
scheduler.addGlobalTriggerListener(triggerMonitor);
實際跑了一下
監看的流程如下
TestQjobTrigger was fired
->TestQjobTrigger was not vetoed
->The job TestQjob will be execute!
->The job TestQjob was executed!
->TestQjobTrigger is complete

沒有留言:

張貼留言