监听器
JobListener
@Log4j
public class MyJobListener implements JobListener {
@Override
public String getName() {
return "my-job-listener";
}
@Override
public void jobToBeExecuted(JobExecutionContext context) {
log.info("----任务执行之前进入此方法----");
}
@Override
public void jobExecutionVetoed(JobExecutionContext context) {
log.info("----任务被TriggerListener拒绝执行之后进入此方法----");
}
@Override
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
log.info("----任务执行之后进入此方法----");
}
}
TriggerListener
@Log4j
public class MyTriggerListener implements TriggerListener {
@Override
public String getName() {
return "my-trigger-listener";
}
@Override
public void triggerFired(Trigger trigger, JobExecutionContext context) {
log.info("任务即将被执行前,触发调用");
}
@Override
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
log.info("如果返回true,则否决掉本次jobDetail的执行");
return false;
}
@Override
public void triggerMisfired(Trigger trigger) {
log.info("如果任务misfired(未执行),则调用本方法");
}
@Override
public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
log.info("触发执行结束时调用");
}
}
调度
JobDetail jobDetail = JobBuilder.newJob(MySpringQuartzJob.class)
.withIdentity("job-2")
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger-2")
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(30))
.startNow()
.build();
scheduler.getListenerManager().addJobListener(
new MyJobListener(), KeyMatcher.keyEquals(jobDetail.getKey())
);
scheduler.getListenerManager().addTriggerListener(
new MyTriggerListener(),
AndMatcher.and(
KeyMatcher.keyEquals(trigger.getKey()),
GroupMatcher.groupEquals(Key.DEFAULT_GROUP)
)
);
scheduler.scheduleJob(jobDetail, trigger);
Matcher
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/cb958396cbb6d0d16f47a9f4a6735829.png)
JobDetail jobDetail = JobBuilder.newJob(MySpringQuartzJob.class)
.withIdentity("job-2")
.requestRecovery()
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger-2")
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(30))
.startNow()
.modifiedByCalendar("holidays")
.withPriority(5)
.build();
scheduler.getListenerManager().addJobListener(
new MyJobListener(), KeyMatcher.keyEquals(jobDetail.getKey())
);
scheduler.getListenerManager().addTriggerListener(
new MyTriggerListener(),
AndMatcher.and(
KeyMatcher.keyEquals(trigger.getKey()),
GroupMatcher.groupEquals(Key.DEFAULT\_GROUP)
)
);
scheduler.scheduleJob(jobDetail, trigger);