我们是能对Job和Trigger创建自己的监听器的.
MyTriggerListener:
package com;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.TriggerListener;
/** *//**
*
* @author zdw
*
*/
public class MyTriggerListener implements TriggerListener
{
public String getName()
{
return "myTriggerListener";
}
public void triggerComplete(Trigger trigger, JobExecutionContext jeContext,
int arg2)
{
System.out.println("triggerName:" + trigger.getName());
System.out.println("complete");
}
public void triggerFired(Trigger arg0, JobExecutionContext arg1)
{
System.out.println("fire");
System.out.println("trigger" + arg0.getGroup());
}
public void triggerMisfired(Trigger arg0)
{
System.out.println("triggerMisfired");
}
public boolean vetoJobExecution(Trigger arg0, JobExecutionContext arg1)
{
System.out.println("vetoJobException");
return false;
}
}
MyJobDetailListener:
package com;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
/** *//**
*
* @author zdw
*
*/
public class MyJobDetailListener implements JobListener
{
/** *//**
* 注意name是必须的
*/
public String getName()
{
return "myJobListener";
}
public void jobExecutionVetoed(JobExecutionContext arg0)
{
System.out.println("jobExceution");
}
public void jobToBeExecuted(JobExecutionContext arg0)
{
System.out.println("jobTobeExecuted");
}
public void jobWasExecuted(JobExecutionContext arg0,
JobExecutionException arg1)
{
System.out.println("job was executed");
}
}
上面两个类只要注意name是必须的就可以了.
package com;
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
/** *//**
*
* @author zdw
*
*/
public class QuartzTest
{
public static void main(String args[])
{
// 得到可用客户端处理调度程序的工厂
SchedulerFactory schedFact = new StdSchedulerFactory();
// 高度程序接口,控制JobDeail和Trigger
try
{
Scheduler sched = schedFact.getScheduler();
// 表示给定工作类的详细信息
JobDetail jobDetail = new JobDetail("myJb", null, StringTest.class);
// 也可以分别设置
jobDetail.setName("angel");
jobDetail.setGroup("007");
jobDetail.setJobClass(StringTest.class);
jobDetail.getJobDataMap().put("jobSays", "I will be success!!");
// 触发器
Trigger trigger = TriggerUtils.makeSecondlyTrigger();
// 设置开始时间,这时设置的是每一秒执行一次
trigger.setStartTime(TriggerUtils
.getEvenSecondDateBefore(new Date()));
// 必须设置,不能为空
trigger.setName("firstTrigger");
// 使用自己的监听器
MyTriggerListener triggerListener = new MyTriggerListener();
sched.addTriggerListener(triggerListener);
sched.addJobListener(new MyJobDetailListener());
// 匹配(必须操作)
trigger.addTriggerListener("myTriggerListener");
jobDetail.addJobListener("myJobListener");
// 为调度程序设置具体工作和触发器
sched.scheduleJob(jobDetail, trigger);
// fire
sched.start();
} catch (SchedulerException e)
{
e.printStackTrace();
}
}
}
注意:
// 匹配(必须操作)
trigger.addTriggerListener("myTriggerListener");
jobDetail.addJobListener("myJobListener");
这两步是必须的,官方文档没有提及,而且必须在scheduler添加完相应的监听器之后.
这时我们看到输出:
vetoJobException
jobTobeExecuted
execute at Fri May 09 09:24:11 CST 2008
instName:angel instGroup:007 jobSays:I will be success!!
job was executed
triggerName:firstTrigger
complete
fire
triggerDEFAULT
vetoJobException
jobTobeExecuted
execute at Fri May 09 09:24:12 CST 2008
instName:angel instGroup:007 jobSays:I will be success!!
job was executed
triggerName:firstTrigger
complete
我们自己的监听器已经被执行了.