Quartz使用 - Quartz监听器Listerner

Posted mengrennwpu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Quartz使用 - Quartz监听器Listerner相关的知识,希望对你有一定的参考价值。

1. 概述

Quartz的监听器用于当任务调度中你所关注事件发生时,能够及时获取这一事件的通知。类似于任务执行过程中的邮件、短信类的提醒。Quartz监听器主要有JobListener、TriggerListener、SchedulerListener三种,顾名思义,分别表示任务、触发器、调度器对应的监听器。三者的使用方法类似,在开始介绍三种监听器之前,需要明确两个概念:全局监听器与非全局监听器,二者的区别在于:全局监听器能够接收到所有的Job/Trigger的事件通知,而非全局监听器只能接收到在其上注册的Job或Trigger的事件,不在其上注册的Job或Trigger则不会进行监听。关于全局与非全局的监听器的使用,在本文中会做一介绍。

2. JobListener

任务调度过程中,与任务Job相关的事件包括:job开始要执行的提示; job执行完成的提示灯。其接口如下:

技术分享
public interface JobListener {

    public String getName();

    public void jobToBeExecuted(JobExecutionContext context);

    public void jobExecutionVetoed(JobExecutionContext context);

    public void jobWasExecuted(JobExecutionContext context,
            JobExecutionException jobException);

}
View Code

 其中:

1) getName方法:用于获取该JobListener的名称。

2) jobToBeExecuted方法:Scheduler在JobDetail将要被执行时调用这个方法。

3) jobExecutionVetoed方法:Scheduler在JobDetail即将被执行,但又被TriggerListerner否决时会调用该方法

4) jobWasExecuted方法:Scheduler在JobDetail被执行之后调用这个方法

示例:

下面的代码展示了简单使用JobListener的使用,其中区别了全局监听器与非全局监听器的创建及使用方法。

1) 简单的HelloWordJob创建

技术分享
package org.ws.quartz.test3;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorldJob implements Job{
    
    private static Logger logger = LoggerFactory.getLogger(HelloWorldJob.class);
    
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        logger.info("Hello World");
    }
}
View Code

2) 创建简单的JobListener

技术分享
package org.ws.quartz.test3;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleJobListener implements JobListener{

    private static Logger logger = LoggerFactory.getLogger(SimpleJobListener.class);
    
    @Override
    public String getName() {
        String name = getClass().getSimpleName();
        logger.info(" listener name is:"+name);
        return name;
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext context) {
        String jobName = context.getJobDetail().getKey().getName();
        logger.info(jobName + " is going to be executed");
    }

    @Override
    public void jobExecutionVetoed(JobExecutionContext context) {
        String jobName = context.getJobDetail().getKey().getName();
        logger.info(jobName + " was vetoed and not executed");
    }

    @Override
    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
        String jobName = context.getJobDetail().getKey().getName();
        logger.info(jobName + " was executed");

    }
}
View Code

3) 调度器的创建与执行

技术分享
package org.ws.quartz.test3;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.EverythingMatcher;
import org.quartz.impl.matchers.KeyMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleQuartzExample {
    
    private static Logger logger = LoggerFactory.getLogger(SimpleQuartzExample.class);
    
    public static void main(String[] args) throws SchedulerException, InterruptedException {
        
        SimpleQuartzExample exam = new SimpleQuartzExample();
        
        logger.info("init scheduler componets");
        
        // 创建调度器
        Scheduler scheduler = exam.createScheduler();
        
        // 创建两个任务及对应的触发器
        JobDetail jobDetail1 = exam.createJobDetail("HelloWord1_Job", "HelloWorld1_Group");
        Trigger trigger1 = exam.createTrigger("HelloWord1_Job", "HelloWorld1_Group");
        
        JobDetail jobDetail2 = exam.createJobDetail("HelloWord2_Job", "HelloWorld2_Group");
        Trigger trigger2 = exam.createTrigger("HelloWord2_Job", "HelloWorld2_Group");
        
        // 构建调度任务
        scheduler.scheduleJob(jobDetail1, trigger1);
        scheduler.scheduleJob(jobDetail2, trigger2);
        
        // 创建并注册一个全局的Job Listener
        scheduler.getListenerManager().addJobListener(new SimpleJobListener(), EverythingMatcher.allJobs());
        
        // 创建并注册一个指定任务的Job Listener
//        scheduler.getListenerManager().addJobListener(new SimpleJobListener(), KeyMatcher.keyEquals(JobKey.jobKey("HelloWorld1_Job", "HelloWorld1_Group")));
        
        logger.info("execute scheduler");
        // 开启调度器
        scheduler.start();
        
        Thread.sleep(20000);
        
        scheduler.shutdown();
        
        logger.info("shut down scheduler");
    }

    protected Scheduler createScheduler() throws SchedulerException{
        return StdSchedulerFactory.getDefaultScheduler(); 
    }
    
    protected JobDetail createJobDetail(String jobName, String jobGroup){
        return JobBuilder.newJob(HelloWorldJob.class)
                .withIdentity(jobName, jobGroup)
                .build();
    }
    
    protected Trigger createTrigger(String triggerName, String triggerGroup){
        return  TriggerBuilder.newTrigger()
                .withIdentity(triggerName, triggerGroup)
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(10)
                        .repeatForever()
                        ).build();
    }
}
View Code

① 执行全局监听器的结果如下:

技术分享
2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleQuartzExample] init scheduler componets
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleQuartzExample] execute scheduler
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord1_Job is going to be executed
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.HelloWorldJob1] Hello World
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord1_Job was executed
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord2_Job is going to be executed
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.HelloWorldJob1] Hello World
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:11 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord2_Job was executed
  2017-07-16 17:14:21 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:21 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord1_Job is going to be executed
  2017-07-16 17:14:21 [INFO]-[org.ws.quartz.test3.HelloWorldJob1] Hello World
  2017-07-16 17:14:21 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:21 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord1_Job was executed
  2017-07-16 17:14:21 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:21 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord2_Job is going to be executed
  2017-07-16 17:14:21 [INFO]-[org.ws.quartz.test3.HelloWorldJob1] Hello World
  2017-07-16 17:14:21 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:14:21 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord2_Job was executed
View Code

② 将代码中的注释去掉,并注释全区监听器的创建,执行非全局监听器结果如下:

技术分享
2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleQuartzExample] init scheduler componets
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleQuartzExample] execute scheduler
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord1_Job is going to be executed
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:32:57 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord1_Job was executed
  2017-07-16 17:33:07 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:33:07 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord1_Job is going to be executed
  2017-07-16 17:33:07 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 17:33:07 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:33:07 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord1_Job was executed
  2017-07-16 17:33:07 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:33:07 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 17:33:07 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:33:17 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:33:17 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord1_Job is going to be executed
  2017-07-16 17:33:17 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 17:33:17 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:33:17 [INFO]-[org.ws.quartz.test3.SimpleJobListener] HelloWord1_Job was executed
  2017-07-16 17:33:17 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:33:17 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 17:33:17 [INFO]-[org.ws.quartz.test3.SimpleJobListener]  listener name is:SimpleJobListener
  2017-07-16 17:33:17 [INFO]-[org.ws.quartz.test3.SimpleQuartzExample] shut down scheduler
  
View Code

备注:

除了代码中将对应的job注册到监听器中的两种方法,也有如下几种其他方法:

1) 将同一任务组的任务注册到监听器中

技术分享
scheduler.getListenerManager().addJobListener(new SimpleJobListener(), GroupMatcher.jobGroupEquals("HelloWorld2_Group"));
View Code

2) 将两个任务组的任务注册到监听器中

技术分享
scheduler.getListenerManager().addJobListener(new SimpleJobListener(), OrMatcher.or(GroupMatcher.jobGroupEquals("HelloWorld1_Group"), GroupMatcher.jobGroupEquals("HelloWorld2_Group")));
View Code

3. TriggerListener

任务调度过程中,与触发器Trigger相关的事件包括:触发器触发、触发器未正常触发、触发器完成等。TriggerListener的接口如下:

技术分享
public interface TriggerListener {

    public String getName();

    public void triggerFired(Trigger trigger, JobExecutionContext context);

    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context);

    public void triggerMisfired(Trigger trigger);

    public void triggerComplete(Trigger trigger, JobExecutionContext context,
            int triggerInstructionCode);
}
View Code

其中:

1) getName方法:用于获取触发器的名称

2) triggerFired方法:当与监听器相关联的Trigger被触发,Job上的execute()方法将被执行时,Scheduler就调用该方法。

3) vetoJobExecution方法:在 Trigger 触发后,Job 将要被执行时由 Scheduler 调用这个方法。TriggerListener 给了一个选择去否决 Job 的执行。假如这个方法返回 true,这个 Job 将不会为此次 Trigger 触发而得到执行。

4) triggerMisfired方法:Scheduler 调用这个方法是在 Trigger 错过触发时。你应该关注此方法中持续时间长的逻辑:在出现许多错过触发的 Trigger 时,长逻辑会导致骨牌效应。你应当保持这上方法尽量的小。

5) triggerComplete方法:Trigger 被触发并且完成了 Job 的执行时,Scheduler 调用这个方法。

 示例:

下面的例子简单展示了TriggerListener的使用,其中创建并注册TriggerListener与JobListener几乎类似。

1) HelloWorldJob类

技术分享
package org.ws.quartz.test3;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorldJob implements Job{
    
    private static Logger logger = LoggerFactory.getLogger(HelloWorldJob.class);
    
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        logger.info("Hello World");
    }
}
View Code

 

2) SimpleTriggerListener

技术分享
package org.ws.quartz.test3;

import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.Trigger.CompletedExecutionInstruction;
import org.quartz.TriggerListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleTriggerListener implements TriggerListener{
    
    private static Logger logger = LoggerFactory.getLogger(SimpleTriggerListener.class);
    
    private String name;
    
    public SimpleTriggerListener(String name) {
        this.name = name;
    }
    
    @Override
    public String getName() {
        return name;
    }

    @Override
    public void triggerFired(Trigger trigger, JobExecutionContext context) {
        String triggerName = trigger.getKey().getName();
        logger.info(triggerName + " was fired");
    }

    @Override
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
        String triggerName = trigger.getKey().getName();
        logger.info(triggerName + " was not vetoed");
        return false;
    }

    @Override
    public void triggerMisfired(Trigger trigger) {
        String triggerName = trigger.getKey().getName();
        logger.info(triggerName + " misfired");
    }

    @Override
    public void triggerComplete(Trigger trigger, JobExecutionContext context,
            CompletedExecutionInstruction triggerInstructionCode) {
        String triggerName = trigger.getKey().getName();
        logger.info(triggerName + " is complete");
    }
}
View Code

 

3) 任务调度与运行,涉及到多种TriggerListener的创建与注册。

技术分享
package org.ws.quartz.test3;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.EverythingMatcher;
import org.quartz.impl.matchers.GroupMatcher;
import org.quartz.impl.matchers.KeyMatcher;
import org.quartz.impl.matchers.OrMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleQuartzExample {
    
    private static Logger logger = LoggerFactory.getLogger(SimpleQuartzExample.class);
    
    public static void main(String[] args) throws SchedulerException, InterruptedException {
        
        SimpleQuartzExample exam = new SimpleQuartzExample();
        
        logger.info("init scheduler componets");
        
        // 创建调度器
        Scheduler scheduler = exam.createScheduler();
        
        // 创建两个任务及对应的触发器
        JobDetail jobDetail1 = exam.createJobDetail("HelloWord1_Job", "HelloWorld1_Group");
        Trigger trigger1 = exam.createTrigger("HelloWord1_Job", "HelloWorld1_Group");
        
        JobDetail jobDetail2 = exam.createJobDetail("HelloWord2_Job", "HelloWorld2_Group");
        Trigger trigger2 = exam.createTrigger("HelloWord2_Job", "HelloWorld2_Group");
        
        // 构建调度任务
        scheduler.scheduleJob(jobDetail1, trigger1);
        scheduler.scheduleJob(jobDetail2, trigger2);
        
        // 创建并注册一个全局的Trigger Listener
        scheduler.getListenerManager().addTriggerListener(new SimpleTriggerListener("SimpleTrigger"), EverythingMatcher.allTriggers());
        
        // 创建并注册一个局部的Trigger Listener
//        scheduler.getListenerManager().addTriggerListener(new SimpleTriggerListener("SimpleTrigger"), KeyMatcher.keyEquals(TriggerKey.triggerKey("HelloWord1_Job", "HelloWorld1_Group")));
        
        // 创建并注册一个特定组的Trigger Listener
//        scheduler.getListenerManager().addTriggerListener(new SimpleTriggerListener("SimpleTrigger"), GroupMatcher.groupEquals("HelloWorld1_Group"));
        
        logger.info("execute scheduler");
        // 开启调度器""
        scheduler.start();
        
        Thread.sleep(20000);
        
        scheduler.shutdown();
        
        logger.info("shut down scheduler");
    }

    protected Scheduler createScheduler() throws SchedulerException{
        return StdSchedulerFactory.getDefaultScheduler(); 
    }
    
    protected JobDetail createJobDetail(String jobName, String jobGroup){
        return JobBuilder.newJob(HelloWorldJob.class)
                .withIdentity(jobName, jobGroup)
                .build();
    }
    
    protected Trigger createTrigger(String triggerName, String triggerGroup){
        return  TriggerBuilder.newTrigger()
                .withIdentity(triggerName, triggerGroup)
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(10)
                        .repeatForever()
                        ).build();
    }
}
View Code

 

执行全局监听器的结果如下:

技术分享
2017-07-16 18:31:58 [INFO]-[org.ws.quartz.test3.SimpleQuartzExample] init scheduler componets
  2017-07-16 18:31:58 [INFO]-[org.ws.quartz.test3.SimpleQuartzExample] execute scheduler
  2017-07-16 18:31:58 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord2_Job was fired
  2017-07-16 18:31:58 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord2_Job was not vetoed
  2017-07-16 18:31:58 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 18:31:58 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord2_Job is complete
  2017-07-16 18:31:58 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord1_Job was fired
  2017-07-16 18:31:58 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord1_Job was not vetoed
  2017-07-16 18:31:58 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 18:31:58 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord1_Job is complete
  2017-07-16 18:32:08 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord1_Job was fired
  2017-07-16 18:32:08 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord1_Job was not vetoed
  2017-07-16 18:32:08 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 18:32:08 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord1_Job is complete
  2017-07-16 18:32:08 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord2_Job was fired
  2017-07-16 18:32:08 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord2_Job was not vetoed
  2017-07-16 18:32:08 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 18:32:08 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord2_Job is complete
  2017-07-16 18:32:18 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord1_Job was fired
  2017-07-16 18:32:18 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord1_Job was not vetoed
  2017-07-16 18:32:18 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 18:32:18 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord1_Job is complete
  2017-07-16 18:32:18 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord2_Job was fired
  2017-07-16 18:32:18 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord2_Job was not vetoed
  2017-07-16 18:32:18 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
  2017-07-16 18:32:18 [INFO]-[org.ws.quartz.test3.SimpleTriggerListener] HelloWord2_Job is complete
  2017-07-16 18:32:18 [INFO]-[org.ws.quartz.test3.SimpleQuartzExample] shut down scheduler
  
View Code

 

4. SchedulerListener

SchedulerListener会在Scheduler的生命周期中关键事件发生时被调用。与Scheduler有关的事件包括:增加一个job/trigger,删除一个job/trigger,scheduler发生严重错误,关闭scheduler等。

SchedulerListener的接口如下:

技术分享
public interface SchedulerListener {

    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();
}
View Code

 

其中:

1) jobScheduled方法:用于部署JobDetail时调用

2) jobUnscheduled方法:用于卸载JobDetail时调用

3) triggerFinalized方法:当一个 Trigger 来到了再也不会触发的状态时调用这个方法。除非这个 Job 已设置成了持久性,否则它就会从 Scheduler 中移除。

4) triggersPaused方法:Scheduler 调用这个方法是发生在一个 Trigger 或 Trigger 组被暂停时。假如是 Trigger 组的话,triggerName 参数将为 null。

5) triggersResumed:Scheduler 调用这个方法是发生成一个 Trigger 或 Trigger 组从暂停中恢复时。假如是 Trigger 组的话,假如是 Trigger 组的话,triggerName 参数将为 null。参数将为 null。
6) jobsPaused:当一个或一组 JobDetail 暂停时调用这个方法。
7) jobsResumed:当一个或一组 Job 从暂停上恢复时调用这个方法。假如是一个 Job 组,jobName 参数将为 null。
8) schedulerError:在 Scheduler 的正常运行期间产生一个严重错误时调用这个方法。
9) schedulerStarted:当Scheduler 开启时,调用该方法
10) schedulerInStandbyMode: 当Scheduler处于StandBy模式时,调用该方法
11) schedulerShutdown:当Scheduler停止时,调用该方法
12) schedulingDataCleared:当Scheduler中的数据被清除时,调用该方法。

 

 

 

 

 

 

 

 

 

 

 

jobToBeExecuted







以上是关于Quartz使用 - Quartz监听器Listerner的主要内容,如果未能解决你的问题,请参考以下文章

quartz的框架特征

Quartz监听器

Quartz监听器

Quartz监听器

quartz2.3.0job任务监听器,监听任务执行前后取消手动处理方法

spring+quartz知识总结