quartz的监听器listener

2020-03-08  本文已影响0人  夜月河色

一、scheduler(调度器)监听

import org.quartz.*;

public class MySchedulerListener implements SchedulerListener {
    //----------------job相关---------------------
    //添加job时触发
    @Override
    public void jobAdded(JobDetail arg0) {
        System.out.println("添加job时触发"+":"+arg0.getKey().getGroup()+"-"+arg0.getKey().getName());
    }

    //删除job时触发
    @Override
    public void jobDeleted(JobKey arg0) {
        System.out.println("删除job时触发"+":"+arg0.getGroup()+"-"+arg0.getName());
    }

    //挂起job时触发(暂停)
    @Override
    public void jobPaused(JobKey arg0) {
        System.out.println("挂起job时触发"+":"+arg0.getGroup()+"-"+arg0.getName());
    }

    //恢复job时触发(继续)
    @Override
    public void jobResumed(JobKey arg0) {
        System.out.println("恢复job时触发"+":"+arg0.getGroup()+"-"+arg0.getName());
    }

    //部署job时触发
    @Override
    public void jobScheduled(Trigger arg0) {
        System.out.println("部署job时触发:"+arg0.getKey().getGroup()+"-"+arg0.getKey().getName());
    }

    //卸载job时触发
    @Override
    public void jobUnscheduled(TriggerKey arg0) {
        System.out.println("卸载job时触发:"+arg0.getGroup()+"-"+arg0.getName());
    }

    //暂停job group时触发
    @Override
    public void jobsPaused(String arg0) {
        System.out.println("暂停job group时触发:"+arg0);
    }

    //恢复job group时触发
    @Override
    public void jobsResumed(String arg0) {
        System.out.println("恢复job group时触发:"+arg0);
    }

    //----------------trigger相关---------------------
    //暂停trigger时触发
    @Override
    public void triggerPaused(TriggerKey arg0) {
        System.out.println("暂停trigger时触发:"+arg0.getGroup()+"-"+arg0.getName());
    }

    //恢复trigger时触发
    @Override
    public void triggerResumed(TriggerKey arg0) {
        System.out.println("恢复trigger时触发:"+arg0.getGroup()+"-"+arg0.getName());

    }

    //暂停group所有trigger时触发
    @Override
    public void triggersPaused(String arg0) {
        System.out.println("暂停group所有trigger时触发:"+arg0);
    }

    //恢复group所有trigger时触发
    @Override
    public void triggersResumed(String arg0) {
        System.out.println("恢复group所有trigger时触发:"+arg0);
    }

    //完成trigger时触发
    @Override
    public void triggerFinalized(Trigger arg0) {
        System.out.println("完成trigger时触发");
    }

    //----------------scheduler相关---------------------
    //Scheduler开始启动时触发
    @Override
    public void schedulerStarting() {
        System.out.println("scheduler开始启动");
    }

    //Scheduler启动完成时触发
    @Override
    public void schedulerStarted() {
        System.out.println("scheduler启动完成");
    }

    //Scheduler开始关闭时触发
    @Override
    public void schedulerShuttingdown() {
        System.out.println("scheduler开始关闭时触发");
    }

    //Scheduler关闭完成时触发
    @Override
    public void schedulerShutdown() {
        System.out.println("scheduler关闭完成时触发");
    }

    //Scheduler异常时触发
    // Scheduler 的正常运行期间产生一个严重错误时调用这个方法。
    //错误的类型会各式的,但是下面列举了一些错误例子:
    //初始化 Job 类的问题,试图去找到下一 Trigger 的问题
    //JobStore 中重复的问题
    //数据存储连接的问题
    //我们可以使用 SchedulerException 的 getErrorCode() 或者 getUnderlyingException() 方法或获取到特定错误的更详尽的信息。
    @Override
    public void schedulerError(String arg0, SchedulerException arg1) {
        System.out.println("scheduler异常时触发");
    }

    //scheduler清理数据时触发
    @Override
    public void schedulingDataCleared() {
        System.out.println("scheduler清理数据时触发");
    }

    //系统关闭时会触发
    @Override
    public void schedulerInStandbyMode() {
        System.out.println("scheduler被设为standBy等候模式时被执行");
    }
}
    //给调度器添加调度器监听
    scheduler.getListenerManager().addSchedulerListener(new MySchedulerListener());

二、job(任务)监听

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

public class MyJobListener implements JobListener {

    @Override
    public String getName() {//定义该监听器名称
        return "MyJobListener";
    }

    //执行jobDetail前,jobDetail被否决时触发(JobDetail即将被执行,但又被TriggerListener的vetoJobExecution()否决时,触发该方法)
    @Override
    public void jobExecutionVetoed(JobExecutionContext arg0) {
        System.out.println("job在执行前被否决了,就会触发:"+arg0.getJobDetail().getKey().getGroup()+"-"+arg0.getJobDetail().getKey().getName());
    }

    //将要执行jobDetail时触发
    @Override
    public void jobToBeExecuted(JobExecutionContext arg0) {
        System.out.println("将要执行jobDetail时触发:"+arg0.getJobDetail().getKey().getGroup()+"-"+arg0.getJobDetail().getKey().getName());
    }

    //执行完jobDetail时触发
    @Override
    public void jobWasExecuted(JobExecutionContext arg0,JobExecutionException arg1) {
        System.out.println("执行完jobDetail时触发:"+arg0.getJobDetail().getKey().getGroup()+"-"+arg0.getJobDetail().getKey().getName());
    }
}
JobListener myJobListener = new MyJobListener();
//监听全局
scheduler.getListenerManager().addJobListener(myJobListener, EverythingMatcher.allJobs());
//监听部分的job
scheduler.getListenerManager().addJobListener(myJobListener, KeyMatcher.keyEquals(new JobKey("myJobName", "myJobGroup")));
//监听特定组的job
scheduler.getListenerManager().addJobListener(myJobListener,GroupMatcher.groupEquals("myJobGroup"));

三、trigger(触发器)监听

import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.TriggerListener;

public class MyTriggerListener implements TriggerListener {

    @Override
    public String getName() {//定义该监听器名称
        return "MyTriggerListener";
    }//定义该listener的名字

    //激发trigger,与该trigger关联的job也将被运行
    @Override
    public void triggerFired(Trigger trigger, JobExecutionContext context) {
        System.out.println("激发trigger:"+context.getJobDetail().getKey().getGroup()+"-"+context.getJobDetail().getKey().getName());
    }

    //在trigger激发后,job执行前,否决job执行(返回false通过,返回true否决)
    @Override
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
        System.out.println("否决job执行:"+context.getJobDetail().getKey().getGroup()+"-"+context.getJobDetail().getKey().getName());
        return false;
    }

    //完成trigger时触发(代表完成job)
    @Override
    public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
        System.out.println("完成trigger:"+context.getJobDetail().getKey().getGroup()+"-"+context.getJobDetail().getKey().getName());
    }

    //错过trigger激发时执行,如当前时间有很多触发器都需要执行,但是线程池中的有效线程都在工作,导致任务等待时错过执行时间
    @Override
    public void triggerMisfired(Trigger trigger) {
        System.out.println("错过trigger激发:"+trigger.getKey().getGroup()+"-"+trigger.getKey().getGroup());
    }
}
    TriggerListener myTriggerListener = new MyTriggerListener();
    //监听全局
    scheduler.getListenerManager().addTriggerListener(myTriggerListener,EverythingMatcher.allTriggers());
    //监听指定job的Trigger
    scheduler.getListenerManager().addTriggerListener(myTriggerListener, KeyMatcher.keyEquals(new TriggerKey("name","group")));
    //监听特定组的Trigger
    scheduler.getListenerManager().addTriggerListener(myTriggerListener, GroupMatcher.groupEquals("group"));

四、执行顺序、触发顺序

1、scheduler启动:

MySchedulerListener.schedulerStarting()(开始启动scheduler)
↓
MySchedulerListener.schedulerStarted()(启动scheduler完成)

2、scheduler关闭:

MySchedulerListener.schedulerInStandbyMode()(设置scheduler为备份模式)
↓
MySchedulerListener.schedulerShuttingdown()(开始关闭scheduler)
↓
MySchedulerListener.schedulerShutdown()(关闭scheduler完成)

3、任务添加:

MySchedulerListener.jobAdded()(动态添加job)
↓
MySchedulerListener.jobScheduled()(部署job)

4、任务执行:

MyTriggerListener.triggerFired()(激活trigger)
↓
MyTriggerListener.vetoJobExecution()(是否否决执行job)
↓
MyJobListener.jobToBeExecuted()(执行job前)
↓
MyJobListener.jobWasExecuted()(执行job后)
↓
MyTriggerListener.triggerComplete()(执行完trigger)

5、任务停止:

MySchedulerListener.triggerPaused()(停止trigger)
↓
MySchedulerListener.jobUnscheduled()(卸载job)
↓
MySchedulerListener.jobDeleted()(删除job)
↓

6、任务暂停:

MySchedulerListener.triggerPaused()(停止trigger)

7、任务恢复:

MySchedulerListener.jobResumed()(恢复job)

8、失火:

MyTriggerListener.triggerMisfired()(trigger错过触发)
上一篇下一篇

猜你喜欢

热点阅读