listener是一个监听器对象,用于监听scheduler中发生的事件,然后执行相应的操作;你可能已经猜到了,TriggerListeners接受与trigger相关的事件,JobListeners接受与jobs相关的事件。
package com.ickes.job; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; /** * 实现Job接口,定义运行的任务 * @author Ickes */ public class SimpleJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { //打印任务详情 System.out.println( context.getJobDetail().getGroup() +"——"+context.getJobDetail().getName() +"——"+context.getTrigger().getName() +"——"+context.getTrigger().getGroup()); } }
package com.ickes.job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobListener; /** * JobListener监听器 * @author Ickes */ public class Job1Listener implements JobListener { /** * 返回当前监听器的名字,这个方法必须被写他的返回值; * 因为listener需要通过其getName()方法广播它的名称 */ public String getName() { return "Job1Listener"; } /** * 任务被触发前触发 */ public void jobToBeExecuted(JobExecutionContext context) { System.out.println(context.getJobDetail().getName()+":A"); } /** * 这个不管 */ public void jobExecutionVetoed(JobExecutionContext context) { System.out.println(context.getJobDetail().getName()+":B"); } /** * 任务调度完成后触发 */ public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) { System.out.println(context.getJobDetail().getName()+":C"); } }
package com.ickes.job; import java.util.Date; import org.quartz.JobDetail; import org.quartz.JobListener; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.StdSchedulerFactory; /** * @author Ickes */ public class ListenerDemo { public static void main(String[] args) throws Exception { //第一步:创建一个JobDetail实例 JobDetail jobDetail = new JobDetail("j_job1","j_group1", SimpleJob.class); //第二步:通过SimpleTrigger触发器定义调度规则:马上启动,每2秒运行一次,共运行100次 SimpleTrigger simpleTrigger = new SimpleTrigger("t_job1","t_group1"); simpleTrigger.setStartTime(new Date()); simpleTrigger.setEndTime(new Date(System.currentTimeMillis()+1000*5)); simpleTrigger.setRepeatInterval(2000); //运行间隔单位为毫秒 simpleTrigger.setRepeatCount(10); //运行次数 //第三步:通过SchedulerFactory获取一个调度器实例 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); //第四步:关联监听器 JobListener listener = new Job1Listener(); scheduler.addJobListener(listener); jobDetail.addJobListener(listener.getName()); //第五步:将job跟trigger注册到scheduler中进行调度 scheduler.scheduleJob(jobDetail, simpleTrigger); //第六步:调度启动 scheduler.start(); } }
package com.ickes.job; import org.quartz.JobExecutionContext; import org.quartz.Trigger; import org.quartz.TriggerListener; /** * * @author Ickes * */ public class Trigger1Listener implements TriggerListener { public String getName() { return "Trigger1Listener"; } /** * 被调度时触发,和它相关的org.quartz.jobdetail即将执行。 * 该方法优先vetoJobExecution()执行 */ public void triggerFired(Trigger trigger, JobExecutionContext context) { System.out.println("A"); } /** * 被调度时触发,和它相关的org.quartz.jobdetail即将执行。 */ public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) { System.out.println("B"); return false; } /** * 被调度时,触发失败时触发 */ public void triggerMisfired(Trigger trigger) { System.out.println("C"); } /** * 执行完毕时触发 */ public void triggerComplete(Trigger trigger, JobExecutionContext context, int triggerInstructionCode) { System.out.println("D"); } }
package com.ickes.job; import java.util.Date; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.TriggerListener; import org.quartz.impl.StdSchedulerFactory; /** * @author Ickes */ public class TriggerListenerDemo { public static void main(String[] args) throws Exception { //第一步:创建一个JobDetail实例 JobDetail jobDetail = new JobDetail("j_job1","j_group1", SimpleJob.class); //第二步:通过SimpleTrigger触发器定义调度规则:马上启动,每2秒运行一次,共运行100次 SimpleTrigger simpleTrigger = new SimpleTrigger("t_job1","t_group1"); simpleTrigger.setStartTime(new Date()); //5秒钟还有任务没有完成,所以 simpleTrigger.setEndTime(new Date(System.currentTimeMillis()+1000*5)); simpleTrigger.setRepeatInterval(2000); //运行间隔单位为毫秒 simpleTrigger.setRepeatCount(10); //运行次数 //第三步:通过SchedulerFactory获取一个调度器实例 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); //第四步:关联监听器 TriggerListener listener = new Trigger1Listener(); scheduler.addTriggerListener(listener); simpleTrigger.addTriggerListener(listener.getName()); //第五步:将job跟trigger注册到scheduler中进行调度 scheduler.scheduleJob(jobDetail, simpleTrigger); //第六步:调度启动 scheduler.start(); } }