Quartz使用(4) - 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); }
其中:
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");
}
}
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"); }
}
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();
}
}
① 执行全局监听器的结果如下:
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
② 将代码中的注释去掉,并注释全区监听器的创建,执行非全局监听器结果如下:
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
备注:
除了代码中将对应的job注册到监听器中的两种方法,也有如下几种其他方法:
1) 将同一任务组的任务注册到监听器中
scheduler.getListenerManager().addJobListener(new SimpleJobListener(), GroupMatcher.jobGroupEquals("HelloWorld2_Group"));
2) 将两个任务组的任务注册到监听器中
scheduler.getListenerManager().addJobListener(new SimpleJobListener(), OrMatcher.or(GroupMatcher.jobGroupEquals("HelloWorld1_Group"), GroupMatcher.jobGroupEquals("HelloWorld2_Group")));
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);
}
其中:
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");
}
}
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");
}
}
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();
}
}
执行全局监听器的结果如下:
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
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();
}
其中:
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");
}
}
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");
}
}
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();
}
}
执行后的结果如下:
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
Quartz使用(4) - Quartz监听器Listerner的更多相关文章
- 定时组件quartz系列<二>quartz的集群原理
1.基本信息: Quartz是一个开源的作业调度框架,它完全由java写成,并设计用于J2Se和J2EE应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它 来为执行一个作业而创建简单的或 ...
- 实现quartz定时器及quartz定时器原理介绍(转)
一.核心概念 Quartz的原理不是很复杂,只要搞明白几个概念,然后知道如何去启动和关闭一个调度程序即可.1.Job表示一个工作,要执行的具体内容.此接口中只有一个方法void execute(Job ...
- 实现quartz定时器及quartz定时器原理介绍
今天研究定时器,在网上看了一篇不错的文章,推荐给大家! 实现quartz定时器及quartz定时器原理介绍
- 【Quartz.NET】Quartz.NET 入门
概述 Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调度作业.它实现了 ...
- Net作业调度(一) -Quartz.Net入门 Quartz表达式生成器 [转]
背景 很多时候,项目需要在不同个时刻,执行一个或很多个不同的作业. Windows执行计划这时并不能很好的满足需求了. 这时候需要一个更为强大,方便管理,集部署的作业调度了. 介绍 Quartz一个开 ...
- quartz (一) 基于 Quartz 开发企业级任务调度应用
本文转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-quartz/ Quartz 基本概念及原理 Quartz Scheduler 开 ...
- Quartz简介 用 Quartz 进行作业调度
http://www.ibm.com/developerworks/cn/java/j-quartz/现代的 Web 应用程序框架在范围和复杂性方面都有所发展,应用程序的每个底层组件也必须相应地发展. ...
- Quartz使用(6) - Quartz项目实战
本片博文将阐述项目工作中使用Quartz的情况,包含项目背景.项目框架.Quartz集群部署等方面,重点讲述如何在实际项目中使用Quartz. 1. 背景 因项目需求,需要定时调用数据下载接口,并将数 ...
- Quartz使用(5) - Quartz的Job存储及集群部署
1. Job的存储与持久化 Quartz的JobStore接口定义了作业Job.触发器trigger.调度器Scheduler等数据存储机制.Quartz主要有两种Job存储类型:内存存储RAMJob ...
随机推荐
- Java编程思想读书笔记之一切皆对象
一切皆对象 Java程序运行时,数据保存到哪里 寄存器 这是最快的保存区域,因为它位于和其他所有保存方式不同的地方:处理器内部.然而,寄存器的数量十分有限,所以寄存器是根据需要由编译器分配.我们对此没 ...
- MySQL慢日志线上问题分析及功能优化
本文来源于数据库内核专栏. MySQL慢日志(slow log)是MySQL DBA及其他开发.运维人员需经常关注的一类信息.使用慢日志可找出执行时间较长或未走索引等SQL语句,为进行系统调优提供依据 ...
- Paste JSON as Code • quicktype 软件的使用
1.软件图标认知 该软件为json字符串与对象之间相互转户的自动化软件. 下载地址 2.打开软件 配置基本设置 3.生成.h文件 选择生成.h文件 拷贝代码到你管理该对象json文件的.h文件下 4. ...
- Unity苹果(iOS)内购接入(Unity内置IAP)
https://www.jianshu.com/p/4045ebf81a1c Unity苹果(iOS)内购接入(Unity内置IAP) Kakarottog ...
- mysql 表压缩
mysql中经常出现历史表,这些表不会进行修改数据的操作,只有读操作.那么我们可以对其进行压缩处理,缩减磁盘空间.Innodb表和MyISAM表的压缩指令不一样.下面分别来讨论: 一.InnoDB表 ...
- 【Linux】-Ubuntu下配置JDK1.8
前言 这次实在是不想写前言了,好吧,那咱就不写了. 内容 怀着复杂的心情来整理这个小小的操作,其实我的内心是拒绝的,因为太简单了,但是我却花费了很长的时间,有效时间花费了将近两个小时去整理这个小玩意儿 ...
- javascript的最重要的特性之一:闭包的解决方案
初始代码: for (var j = 0; j < lnglats.length; j++) { AMap.event.addListener(markers[j], 'mouseover', ...
- C语言中函数声明、形参、实参
函数原型: 原型prototype是函数的声明:描述了函数的返回值与参数: 函数原型说明了两点: 1.该函数的返回值 2.该函数的参数及其类型 ++++++++++++++++++++++++++++ ...
- C#面试题-递归
有一组数1.1.2.3.5.8.13.21.34...,求第n个数是多少? public int Recursion (int n){ ){ ; } &&n<=){ ; } )+ ...
- linux 安装 配置网络 备份 快照
安装系统准备: 1.软件准备 vmware workstation14.vm14key.centos系统镜像 secureCRT http://sw.bos.baidu.com/sw-search-s ...