概述
SchedulerListener 是在 Scheduler 级别的事件产生时得到通知,不管是增加还是移除 Scheduler 中的 Job,或者是 Scheduler 遭遇到了严重的错误时。那些事件多是关于对 Scheduler 管理的,而不是专注于 Job 或 Trigger 的。
org.quartz.SchedulerListener 接口包含了一系列的回调方法,它们会在 Scheduler 的生命周期中有关键事件发生时被调用。
SchedulerListener接口方法解读
我们先看下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 schedulerShutdown(); }
jobScheduled() 和 jobUnscheduled():Scheduler 在有新的 JobDetail 部署或卸载时调用这两个中的相应方法。
triggerFinalized() :当一个 Trigger 来到了再也不会触发的状态时调用这个方法。除非这个 Job 已设置成了持久性,否则它就会从 Scheduler 中移除。
triggersPaused():Scheduler 调用这个方法是发生在一个 Trigger 或 Trigger 组被暂停时。假如是 Trigger 组的话,triggerName 参数将为 null。
triggersResumed():Scheduler 调用这个方法是发生成一个 Trigger 或 Trigger 组从暂停中恢复时。假如是 Trigger 组的话,triggerName 参数将为 null。
jobsPaused():当一个或一组 JobDetail 暂停时调用这个方法。
jobsResumed():当一个或一组 Job 从暂停上恢复时调用这个方法。假如是一个 Job 组,jobName 参数将为 null。
schedulerError():在 Scheduler 的正常运行期间产生一个严重错误时调用这个方法。错误的类型会各式的,但是下面列举了一些错误例子:
初始化 Job 类的问题
试图去找到下一 Trigger 的问题
JobStore 中重复的问题
数据存储连接的问题
你可以使用 SchedulerException 的 getErrorCode() 或者 getUnderlyingException() 方法或获取到特定错误的更详尽的信息。
schedulerShutdown():Scheduler 调用这个方法用来通知 SchedulerListener Scheduler 将要被关闭。
示例
Job复用TriggerListener中的SimpleJob1
自定义SchedulerListener
package com.xgj.quartz.quartzItself.listener.schedulerListener; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.SchedulerException; import org.quartz.SchedulerListener; import org.quartz.Trigger; import org.quartz.TriggerKey; public class MySchedulerListener implements SchedulerListener { @Override public void jobScheduled(Trigger trigger) { System.out.println("MySchedulerListener jobScheduled trigger"); } @Override public void jobUnscheduled(TriggerKey triggerKey) { System.out.println("MySchedulerListener jobScheduled triggerKey"); } @Override public void triggerFinalized(Trigger trigger) { System.out.println("MySchedulerListener triggerFinalized"); } @Override public void triggerPaused(TriggerKey triggerKey) { System.out.println("MySchedulerListener triggerPaused"); } @Override public void triggersPaused(String triggerGroup) { System.out.println("MySchedulerListener triggersPaused"); } @Override public void triggerResumed(TriggerKey triggerKey) { System.out.println("MySchedulerListener triggerResumed triggerKey"); } @Override public void triggersResumed(String triggerGroup) { System.out.println("MySchedulerListener triggerResumed triggerGroup"); } @Override public void jobAdded(JobDetail jobDetail) { System.out.println("MySchedulerListener jobAdded"); } @Override public void jobDeleted(JobKey jobKey) { System.out.println("MySchedulerListener jobDeleted"); } @Override public void jobPaused(JobKey jobKey) { System.out.println("MySchedulerListener jobPaused jobKey"); } @Override public void jobsPaused(String jobGroup) { System.out.println("MySchedulerListener jobsPaused jobGroup"); } @Override public void jobResumed(JobKey jobKey) { System.out.println("MySchedulerListener jobResumed jobKey"); } @Override public void jobsResumed(String jobGroup) { System.out.println("MySchedulerListener jobsResumed jobGroup"); } @Override public void schedulerError(String msg, SchedulerException cause) { System.out.println("MySchedulerListener schedulerError"); } @Override public void schedulerInStandbyMode() { System.out.println("MySchedulerListener schedulerInStandbyMode"); } @Override public void schedulerStarted() { System.out.println("MySchedulerListener schedulerStarted"); } @Override public void schedulerStarting() { System.out.println("MySchedulerListener schedulerStarting"); } @Override public void schedulerShutdown() { System.out.println("MySchedulerListener schedulerShutdown"); } @Override public void schedulerShuttingdown() { System.out.println("MySchedulerListener schedulerShuttingdown"); } @Override public void schedulingDataCleared() { System.out.println("MySchedulerListener schedulingDataCleared"); } }
调度类
package com.xgj.quartz.quartzItself.listener.schedulerListener; import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SchedulerListener; import org.quartz.SchedulerMetaData; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; import com.xgj.quartz.quartzItself.listener.triggerListener.SimpleJob1; public class SchedulerListenerDemo { public static void main(String[] args) throws Exception { System.out.println("------- 初始化 ----------------------"); // Scheduler SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); // 添加监听器 SchedulerListener schedulerListener = new MySchedulerListener(); scheduler.getListenerManager().addSchedulerListener(schedulerListener); // Job JobDetail job = newJob(SimpleJob1.class).withIdentity("job1", "group1") .build(); // Tirgger Trigger trigger = newTrigger().withIdentity("trigger1", "group1") .startNow().build(); // 将job任务加入到调度器 scheduler.scheduleJob(job, trigger); // 开始任务 System.out.println("------- 开始执行调度器 Scheduler ----------------"); scheduler.start(); try { System.out.println("------- 等待 30 秒... --------------"); Thread.sleep(30L * 1000L); } catch (Exception e) { e.printStackTrace(); } scheduler.shutdown(true); System.out.println("------- 关闭调度器 -----------------"); SchedulerMetaData metaData = scheduler.getMetaData(); System.out.println("~~~~~~~~~~ 执行了 " + metaData.getNumberOfJobsExecuted() + " 个 jobs."); } }
运行结果
------- 初始化 ---------------------- INFO StdSchedulerFactory - Using default implementation for ThreadExecutor INFO SimpleThreadPool - Job execution threads will use class loader of thread: main INFO SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl INFO QuartzScheduler - Quartz Scheduler v.2.2.3 created. INFO RAMJobStore - RAMJobStore initialized. INFO QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.2.3) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED' Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. INFO StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' INFO StdSchedulerFactory - Quartz scheduler version: 2.2.3 MySchedulerListener jobAdded MySchedulerListener jobScheduled trigger ------- 开始执行调度器 Scheduler ---------------- MySchedulerListener schedulerStarting INFO QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started. MySchedulerListener schedulerStarted ------- 等待 30 秒... -------------- Job1 - 任务key group1.job1执行时间:2017-11-16 22:14:33 MySchedulerListener triggerFinalized MySchedulerListener jobDeleted INFO QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down. INFO QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused. MySchedulerListener schedulerInStandbyMode MySchedulerListener schedulerShuttingdown MySchedulerListener schedulerShutdown INFO QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete. ------- 关闭调度器 ----------------- ~~~~~~~~~~ 执行了 1 个 jobs.
示例源码
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster