林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka
监听器是一个 Java 接口,它必须由一个具体类来实现。你也不需要只为这个目的创建一个专门的类;它可以是任何一个你希望能接收到方法回调的类。为符合良好的程序设计,你应当注意保持高内聚和松耦合性。认真的考虑哪个类你决定用于实现监听器类;这从总体设计的视角来看是很重要的。
1.4、全局之于非全局监听器
JobListener 和 TriggerListener 可被注册为全局或非全局监听器。一个全局监听器能接收到所有的 Job/Trigger 的事件通知。而一个非全局监听器(或者说是一个标准的监听器) 只能接收到那些在其上已注册了监听器的 Job 或 Triiger 的事件。
你要注册你的监听器为全局或非全局的需依据你特定的应用需要。我们在以下章节中提供了两种方式的例子。从另一方面来认识全局和非全局的监听器是来自于 Quartz 框架的创建者。James House 在描述全局和非全局监听器时是这样的:
全局监听器是主动意识的,它们为了执行它们的任务而热切的去寻找每一个可能的事件。通常,全局监听器要做的工作不用指定到特定的 Job 或 Trigger。非全局监听器一般是被动意识的,它们在所关注的 Trigger 激发之前或是 Job 执行之前什么事也不做。因此,非全局的监听器比起全局监听器而言更适合于修改或增加 Job 执行的工作。这有点像知名的装饰设计模式的装饰器。
全局: scheduler.addGlobalTriggerListener(new SimpleMyTriggerListener()); 局部: scheduler.addTriggerListener( triggerListener ); trigger.addTriggerListener( triggerListener.getName() );
org.quartz.JobListener 接口包含一系列的方法,它们会由 Job 在其生命周期中产生的某些关键事件时被调用。JobListener 可用的方法显示在如下代码
org.quartz.JobListener 接口中的方法
public interface JobListener { public String getName(); public void jobToBeExecuted(JobExecutionContext context); public void jobExecutionVetoed(JobExecutionContext context); public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException); }
package com.mucfc; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobListener; public class MyJobListener implements JobListener{ @Override public String getName() { return "MyJobListener"; } /** * Scheduler 在 JobDetail 即将被执行,但又被 TriggerListener 否决了时调用这个方法。 */ @Override public void jobExecutionVetoed(JobExecutionContext arg0) { System.out.println("Job监听器:MyJobListener.jobExecutionVetoed()"); } /** * Scheduler 在 JobDetail 将要被执行时调用这个方法。 */ @Override public void jobToBeExecuted(JobExecutionContext arg0) { System.out.println("Job监听器:MyJobListener.jobToBeExecuted()"); } /** * Scheduler 在 JobDetail 被执行之后调用这个方法。 */ @Override public void jobWasExecuted(JobExecutionContext arg0, JobExecutionException arg1) { System.out.println("Job监听器:MyJobListener.jobWasExecuted()"); } }
// 添加JobListener监听器 MyJobListener myJobListener=new MyJobListener(); scheduler.getListenerManager() .addJobListener(myJobListener, allJobs()); // 监听部分的job //scheduler.getListenerManager() //.addJobListener(myJobListener, keyEquals(jobKey("myJobName", "myJobGroup"))); //监听特定组的job //scheduler.getListenerManager() //.addJobListener(myJobListener, groupEquals("myJobGroup"));
正如 JobListener, org.quartz.TriggerListener 接口也包含一系列给 Scheduler 调用的方法。然而,与 JobListener 有所不同的是, TriggerListener 接口还有关于 Trigger 实例生命周期的方法。以下代码 列出了 TriggerListener 接口的方法。
org.quartz.TriggerListener 接口的方法
public interface TriggerListener { public String getName(); public void triggerFired(Trigger trigger, JobExecutionContext context); public boolean vetoJobExecution(Trigger trigger, JobExecutidonContext context); public void triggerMisfired(Trigger trigger); public void triggerComplete(Trigger trigger, JobExecutionContext context, int triggerInstructionCode); }
package com.mucfc; import org.quartz.JobExecutionContext; import org.quartz.Trigger; import org.quartz.Trigger.CompletedExecutionInstruction; import org.quartz.TriggerListener; public class MyTriggerListener implements TriggerListener{ @Override public String getName() { return "MyTriggerListener"; } /** * (1) * Trigger被激发 它关联的job即将被运行 * Called by the Scheduler when a Trigger has fired, and it‘s associated JobDetail is about to be executed. */ @Override public void triggerFired(Trigger trigger, JobExecutionContext context) { System.out.println("Trigger监听器:MyTriggerListener.triggerFired()"); } /** * (2) * Trigger被激发 它关联的job即将被运行,先执行(1),在执行(2) 如果返回TRUE 那么任务job会被终止 * Called by the Scheduler when a Trigger has fired, and it‘s associated JobDetail is about to be executed */ @Override public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) { System.out.println("Trigger监听器:MyTriggerListener.vetoJobExecution()"); return false; } /** * (3) 当Trigger错过被激发时执行,比如当前时间有很多触发器都需要执行,但是线程池中的有效线程都在工作, * 那么有的触发器就有可能超时,错过这一轮的触发。 * Called by the Scheduler when a Trigger has misfired. */ @Override public void triggerMisfired(Trigger trigger) { System.out.println("Trigger监听器:MyTriggerListener.triggerMisfired()"); } /** * (4) 任务完成时触发 * Called by the Scheduler when a Trigger has fired, it‘s associated JobDetail has been executed * and it‘s triggered(xx) method has been called. */ @Override public void triggerComplete(Trigger trigger, JobExecutionContext context, CompletedExecutionInstruction triggerInstructionCode) { System.out.println("Trigger监听器:MyTriggerListener.triggerComplete()"); } }
//添加TriggerListener监听器 MyTriggerListener myTriggerListener=new MyTriggerListener(); // 监听器所有的job //scheduler.getListenerManager().addTriggerListener(myTriggerListener, allTriggers()); // 监听部分的job //scheduler.getListenerManager().addTriggerListener(myTriggerListener, keyEquals(new TriggerKey("trigger1_1","tGroup1"))); // 监听特定组的job //scheduler.getListenerManager().addTriggerListener(myTriggerListener, triggerGroupEquals("tGroup1"));
org.quartz.SchedulerListener 接口包含了一系列的回调方法,它们会在 Scheduler 的生命周期中有关键事件发生时被调用。代码 7.9 列出了包括在 SchedulerListener 接口的方法。
org.quartz.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 将要被关闭。
以下为一个示例代码
package com.mucfc; 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 jobAdded(JobDetail arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobAdded()"); } @Override public void jobDeleted(JobKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobDeleted()"); } @Override public void jobPaused(JobKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobPaused()"); } @Override public void jobResumed(JobKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobResumed()"); } @Override public void jobScheduled(Trigger arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobScheduled()"); } @Override public void jobUnscheduled(TriggerKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobUnscheduled()"); } @Override public void jobsPaused(String arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobsPaused()"); } @Override public void jobsResumed(String arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobsResumed()"); } @Override public void schedulerError(String arg0, SchedulerException arg1) { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerError()"); } @Override public void schedulerInStandbyMode() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerInStandbyMode()"); } @Override public void schedulerShutdown() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerShutdown()"); } @Override public void schedulerShuttingdown() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerShuttingdown()"); } @Override public void schedulerStarted() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerStarted()"); } @Override public void schedulerStarting() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerStarting()"); } @Override public void schedulingDataCleared() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulingDataCleared()"); } @Override public void triggerFinalized(Trigger arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.triggerFinalized()"); } @Override public void triggerPaused(TriggerKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.triggerPaused()"); } @Override public void triggersPaused(String arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.triggersPaused()"); } @Override public void triggersResumed(String arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.triggersResumed()"); } @Override public void triggerResumed(TriggerKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.triggerResumed()"); } }添加监听器:
//添加监听 MySchedulerListener schedulerListener = new MySchedulerListener(); scheduler.getListenerManager().addSchedulerListener(schedulerListener);
1、 Job类:
package com.mucfc; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** *事件类 *作者 林炳文(ling20081005@126.com 博客:http://blog.csdn.net/evankaka) *时间 2015.5.5 */ public class MyJob implements Job{ public MyJob(){ System.out.println("MyJob创建成功"); } @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("Hello MyJob "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())); } }2、Job监听器类
package com.mucfc; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobListener; public class MyJobListener implements JobListener{ @Override public String getName() { return "MyJobListener"; } /** * Scheduler 在 JobDetail 即将被执行,但又被 TriggerListener 否决了时调用这个方法。 */ @Override public void jobExecutionVetoed(JobExecutionContext arg0) { System.out.println("Job监听器:MyJobListener.jobExecutionVetoed()"); } /** * Scheduler 在 JobDetail 将要被执行时调用这个方法。 */ @Override public void jobToBeExecuted(JobExecutionContext arg0) { System.out.println("Job监听器:MyJobListener.jobToBeExecuted()"); } /** * Scheduler 在 JobDetail 被执行之后调用这个方法。 */ @Override public void jobWasExecuted(JobExecutionContext arg0, JobExecutionException arg1) { System.out.println("Job监听器:MyJobListener.jobWasExecuted()"); } }3、Trigger监听器类
package com.mucfc; import org.quartz.JobExecutionContext; import org.quartz.Trigger; import org.quartz.Trigger.CompletedExecutionInstruction; import org.quartz.TriggerListener; public class MyTriggerListener implements TriggerListener{ @Override public String getName() { return "MyTriggerListener"; } /** * (1) * Trigger被激发 它关联的job即将被运行 * Called by the Scheduler when a Trigger has fired, and it‘s associated JobDetail is about to be executed. */ @Override public void triggerFired(Trigger trigger, JobExecutionContext context) { System.out.println("Trigger监听器:MyTriggerListener.triggerFired()"); } /** * (2) * Trigger被激发 它关联的job即将被运行,先执行(1),在执行(2) 如果返回TRUE 那么任务job会被终止 * Called by the Scheduler when a Trigger has fired, and it‘s associated JobDetail is about to be executed */ @Override public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) { System.out.println("Trigger监听器:MyTriggerListener.vetoJobExecution()"); return false; } /** * (3) 当Trigger错过被激发时执行,比如当前时间有很多触发器都需要执行,但是线程池中的有效线程都在工作, * 那么有的触发器就有可能超时,错过这一轮的触发。 * Called by the Scheduler when a Trigger has misfired. */ @Override public void triggerMisfired(Trigger trigger) { System.out.println("Trigger监听器:MyTriggerListener.triggerMisfired()"); } /** * (4) 任务完成时触发 * Called by the Scheduler when a Trigger has fired, it‘s associated JobDetail has been executed * and it‘s triggered(xx) method has been called. */ @Override public void triggerComplete(Trigger trigger, JobExecutionContext context, CompletedExecutionInstruction triggerInstructionCode) { System.out.println("Trigger监听器:MyTriggerListener.triggerComplete()"); } }4、Scheduler监听器类
package com.mucfc; 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 jobAdded(JobDetail arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobAdded()"); } @Override public void jobDeleted(JobKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobDeleted()"); } @Override public void jobPaused(JobKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobPaused()"); } @Override public void jobResumed(JobKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobResumed()"); } @Override public void jobScheduled(Trigger arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobScheduled()"); } @Override public void jobUnscheduled(TriggerKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobUnscheduled()"); } @Override public void jobsPaused(String arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobsPaused()"); } @Override public void jobsResumed(String arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.jobsResumed()"); } @Override public void schedulerError(String arg0, SchedulerException arg1) { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerError()"); } @Override public void schedulerInStandbyMode() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerInStandbyMode()"); } @Override public void schedulerShutdown() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerShutdown()"); } @Override public void schedulerShuttingdown() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerShuttingdown()"); } @Override public void schedulerStarted() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerStarted()"); } @Override public void schedulerStarting() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulerStarting()"); } @Override public void schedulingDataCleared() { System.out.println("SchedulerListener监听器:MySchedulerListener.schedulingDataCleared()"); } @Override public void triggerFinalized(Trigger arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.triggerFinalized()"); } @Override public void triggerPaused(TriggerKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.triggerPaused()"); } @Override public void triggersPaused(String arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.triggersPaused()"); } @Override public void triggersResumed(String arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.triggersResumed()"); } @Override public void triggerResumed(TriggerKey arg0) { System.out.println("SchedulerListener监听器:MySchedulerListener.triggerResumed()"); } }
package com.mucfc; import java.util.List; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Matcher; 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.KeyMatcher; import static org.quartz.JobKey.*; import static org.quartz.impl.matchers.KeyMatcher.*; import static org.quartz.impl.matchers.GroupMatcher.*; import static org.quartz.impl.matchers.AndMatcher.*; import static org.quartz.impl.matchers.OrMatcher.*; import static org.quartz.impl.matchers.EverythingMatcher.*; public class MyTest { public static void main(String[] args) { try { // 1、创建一个JobDetail实例,指定Quartz JobDetail jobDetail = JobBuilder.newJob(MyJob.class) // 任务执行类 .withIdentity("job1_1", "jGroup1") // 任务名,任务组 .build(); //2、创建Trigger SimpleScheduleBuilder builder=SimpleScheduleBuilder.simpleSchedule() //设置间隔执行时间 .withIntervalInSeconds(5) //设置执行次数 .repeatForever(); Trigger trigger=TriggerBuilder.newTrigger().withIdentity( "trigger1_1","tGroup1").startNow().withSchedule(builder).build(); //3、创建Scheduler Scheduler scheduler=StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); //添加TriggerListener监听器 MyTriggerListener myTriggerListener=new MyTriggerListener(); // 监听器所有的job //scheduler.getListenerManager().addTriggerListener(myTriggerListener, allTriggers()); // 监听部分的job scheduler.getListenerManager().addTriggerListener(myTriggerListener, keyEquals(new TriggerKey("trigger1_1","tGroup1"))); // 监听特定组的job //scheduler.getListenerManager().addTriggerListener(myTriggerListener, triggerGroupEquals("tGroup1")); // 添加JobListener监听器 MyJobListener myJobListener=new MyJobListener(); scheduler.getListenerManager() .addJobListener(myJobListener, allJobs()); // 监听部分的job //scheduler.getListenerManager() //.addJobListener(myJobListener, keyEquals(jobKey("myJobName", "myJobGroup"))); //监听特定组的job //scheduler.getListenerManager() //.addJobListener(myJobListener, groupEquals("myJobGroup")); //添加监听 MySchedulerListener schedulerListener = new MySchedulerListener(); scheduler.getListenerManager().addSchedulerListener(schedulerListener); //4、调度执行 scheduler.scheduleJob(jobDetail, trigger); try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace(); } scheduler.shutdown(); } catch (SchedulerException e) { e.printStackTrace(); } } }
或者看大图!
然后是scheduler关闭后
这就是最后的结果了
林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka
【Quartz】 JobListener、Triggerlistener、SchedulerListener介绍与使用
原文地址:http://blog.csdn.net/evankaka/article/details/45498363