zoukankan      html  css  js  c++  java
  • Quartz使用(4)

    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中的数据被清除时,调用该方法。

     示例:

    下面的代码简单描述了如何使用SchedulerListener方法:

    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) 简单的SchedulerListener

    package org.ws.quartz.test3;
    
    import org.quartz.JobDetail;
    import org.quartz.JobKey;
    import org.quartz.SchedulerException;
    import org.quartz.SchedulerListener;
    import org.quartz.Trigger;
    import org.quartz.TriggerKey;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class SimpleSchedulerListener implements SchedulerListener{
    
        private static Logger logger = LoggerFactory.getLogger(SimpleSchedulerListener.class);
        
        @Override
        public void jobScheduled(Trigger trigger) {
            String jobName = trigger.getJobKey().getName();
            logger.info(jobName + " has been scheduled");
        }
    
        @Override
        public void jobUnscheduled(TriggerKey triggerKey) {
            logger.info(triggerKey + " is being unscheduled");
        }
    
        @Override
        public void triggerFinalized(Trigger trigger) {
            logger.info("Trigger is finished for " + trigger.getJobKey().getName());
        }
    
        @Override
        public void triggerPaused(TriggerKey triggerKey) {
            logger.info(triggerKey + " is being paused");
        }
    
        @Override
        public void triggersPaused(String triggerGroup) {
            logger.info("trigger group "+triggerGroup + " is being paused");
        }
    
        @Override
        public void triggerResumed(TriggerKey triggerKey) {
            logger.info(triggerKey + " is being resumed");
        }
    
        @Override
        public void triggersResumed(String triggerGroup) {
            logger.info("trigger group "+triggerGroup + " is being resumed");
        }
    
        @Override
        public void jobAdded(JobDetail jobDetail) {
            logger.info(jobDetail.getKey()+" is added");
        }
    
        @Override
        public void jobDeleted(JobKey jobKey) {
            logger.info(jobKey+" is deleted");
        }
    
        @Override
        public void jobPaused(JobKey jobKey) {
            logger.info(jobKey+" is paused");
        }
    
        @Override
        public void jobsPaused(String jobGroup) {
            logger.info("job group "+jobGroup+" is paused");
        }
    
        @Override
        public void jobResumed(JobKey jobKey) {
            logger.info(jobKey+" is resumed");
        }
    
        @Override
        public void jobsResumed(String jobGroup) {
            logger.info("job group "+jobGroup+" is resumed");
        }
    
        @Override
        public void schedulerError(String msg, SchedulerException cause) {
            logger.error(msg, cause.getUnderlyingException());
        }
    
        @Override
        public void schedulerInStandbyMode() {
            logger.info("scheduler is in standby mode");
        }
    
        @Override
        public void schedulerStarted() {
            logger.info("scheduler has been started");
        }
    
        @Override
        public void schedulerStarting() {
            logger.info("scheduler is being started");
        }
    
        @Override
        public void schedulerShutdown() {
            logger.info("scheduler has been shutdown");
        }
    
        @Override
        public void schedulerShuttingdown() {
            logger.info("scheduler is being shutdown");
        }
    
        @Override
        public void schedulingDataCleared() {
            logger.info("scheduler has cleared all data");
        }
    }
    View Code

    3) 任务调度以及SchedulerListener的加载调用

    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 jobDetail = exam.createJobDetail("HelloWord_Job", "HelloWorld_Group");
            Trigger trigger = exam.createTrigger("HelloWord_Job", "HelloWorld_Group");
            
            
            // 构建调度任务
            scheduler.scheduleJob(jobDetail, trigger);
            
            // 创建SchedulerListener
            scheduler.getListenerManager().addSchedulerListener(new SimpleSchedulerListener());
            
            // 移除对应的SchedulerListener
    //        scheduler.getListenerManager().removeSchedulerListener(new SimpleSchedulerListener());
            
            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 19:13:59 [INFO]-[org.ws.quartz.test3.SimpleQuartzExample] init scheduler componets
      2017-07-16 19:14:00 [INFO]-[org.ws.quartz.test3.SimpleQuartzExample] execute scheduler
      2017-07-16 19:14:00 [INFO]-[org.ws.quartz.test3.SimpleSchedulerListener] scheduler is being started
      2017-07-16 19:14:00 [INFO]-[org.ws.quartz.test3.SimpleSchedulerListener] scheduler has been started
      2017-07-16 19:14:00 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
      2017-07-16 19:14:10 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
      2017-07-16 19:14:20 [INFO]-[org.ws.quartz.test3.HelloWorldJob] Hello World
      2017-07-16 19:14:20 [INFO]-[org.ws.quartz.test3.SimpleSchedulerListener] scheduler is in standby mode
      2017-07-16 19:14:20 [INFO]-[org.ws.quartz.test3.SimpleSchedulerListener] scheduler is being shutdown
      2017-07-16 19:14:20 [INFO]-[org.ws.quartz.test3.SimpleSchedulerListener] scheduler has been shutdown
      2017-07-16 19:14:20 [INFO]-[org.ws.quartz.test3.SimpleQuartzExample] shut down scheduler
      
    View Code
  • 相关阅读:
    Java变量的作用域
    Java访问修饰符(访问控制符)
    Java类的定义及其实例化
    强调一下编程风格
    Java StringBuffer与StringBuider
    Java字符串(String)
    windows eclipse安装lombok插件
    mac 下eclipse安装lombok插件
    【四】Ribbon负载均衡
    【三】Eureka服务注册与发现
  • 原文地址:https://www.cnblogs.com/mengrennwpu/p/7191229.html
Copyright © 2011-2022 走看看