Quartz主要包含三个组件job,trigger,scheduler,也就对应了有三个监听器,当有重要事件发生,监听器的相应方法就会被调用。有了监听器我们就可以做一个简单的网页应用来监控这些定时任务的执行情况。现在就分别介绍这三种监听器
API:http://www.quartz-scheduler.org/docs/api/1.8.1/index.html
JobListener
官方文档:http://www.quartz-scheduler.org/docs/cookbook/JobListeners.html
JobListener 和 TriggerListener 可被注册为全局或非全局监听器。一个全局监听器能接收到所有的 Job/Trigger 的事件通知。而一个非全局监听器(或者说是一个标准的监听器) 只能接收到那些在其上已注册了监听器的 Job 或 Triiger 的事件。
我们有两种方式来实现job监听器:实现接口JobListener或者继承JobListenerSupport
全部:
JobListener jobListener = new SimpleJobListener("SimpleJobListener");
scheduler.addGlobalJobListener(jobListener);
scheduler.start();
局部:
JobListener jobListener =new SimpleJobListener("SimpleJobListener");
scheduler.addJobListener(jobListener);
// addJobListener必须在scheduleJob之前执行
jobDetail.addJobListener(jobListener.getName());
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
//2.0版本的方法
Ø 监听器所有的job
scheduler.getListenerManager()
.addJobListener(myJobListener, allJobs());
Ø 监听部分的job
scheduler.getListenerManager()
.addJobListener(myJobListener, keyEquals(jobKey("myJobName", "myJobGroup")));
Ø 监听特定组的job
scheduler.getListenerManager()
.addJobListener(myJobListener, groupEquals("myJobGroup"));
TriggerListener
http://www.quartz-scheduler.org/docs/cookbook/TriggerListeners.html
全局:
scheduler.addGlobalTriggerListener(new SimpleMyTriggerListener());
局部:
scheduler.addTriggerListener( triggerListener );
trigger.addTriggerListener( triggerListener.getName() );
//2.0版本的方法
Ø 监听器所有的job
Registering A TriggerListener With The Scheduler To Listen To All Triggers
scheduler.getListenerManager().addTriggerListener(myTriggerListener, allTriggers());
Ø 监听部分的job
Registering A TriggerListener With The Scheduler To Listen To A Specific Trigger
scheduler.getListenerManager()
.addTriggerListener(triggerListener, keyEquals(triggerKey("triggerName", "triggerGroup")));
Ø 监听特定组的job
scheduler.getListenerManager()
.addTriggerListener(myTriggerListener, groupEquals("myTriggerGroup"));
SchedulerListener
http://www.quartz-scheduler.org/docs/cookbook/SchedulerListeners.html
Extend TriggerListenerSupport and override methods for events you're interested in.
Ø 注册调度器监听器
Scheduler scheduler = null;
scheduler = StdSchedulerFactory.getDefaultScheduler();
//添加监听
SchedulerListener schedulerListener = new SimpleSchedulerListener();
scheduler.addSchedulerListener(schedulerListener);
scheduler.start();
//2.0版本的方法
scheduler.getListenerManager().addSchedulerListener(mySchedListener);