在Quartz框架提供了JobListener接口,可在任务执行前、任务被拒绝及任务执行完成后实现对任务的拦截,该接口的声明如下:
public interface JobListener { /**返回监听器名*/ String getName(); /**任务执行前对任务拦截*/ void jobToBeExecuted(JobExecutionContext context); /**任务被否决对任务拦截*/ void jobExecutionVetoed(JobExecutionContext context); /**任务执行完成对任务进行拦截*/ void jobWasExecuted(JobExecutionContext context,JobExecutionException jobException); }
下面简单实现了一个JobListener监听任务执行时间
public class SimpleJobListener implements JobListener { private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public String getName() { return getClass().getSimpleName(); } /** * 任务执行前被调用[任务执行加入任务提交时间] * */ public void jobToBeExecuted(JobExecutionContext context) { String job = context.getJobDetail().getJobClass().getSimpleName(); JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); long start = System.currentTimeMillis(); String date = SDF.format(new Date()); jobDataMap.put("time", start); jobDataMap.put("date", date); System.out.println("[" + job + "]在[" + date + "]加入Quartz~~"); } @Override public void jobExecutionVetoed(JobExecutionContext context) { String job = context.getJobDetail().getJobClass().getSimpleName(); System.out.println("[" + job + "]在[" + SDF.format(new Date()) + "]被Quartz否决~~"); } @Override public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) { String job = context.getJobDetail().getJobClass().getSimpleName(); JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); long start = jobDataMap.getLong("time"); String date = jobDataMap.getString("date"); System.out.println("[" + job + "]在[" + date + "]添加,[" + SDF.format(new Date()) + "]执行完成,耗时[" + (System.currentTimeMillis() - start) + "] ms"); } }
任务执行前加入JobListener
public class SchedulerDemo { private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static SchedulerFactory schedulerFactory ; static { try { Properties props = new Properties(); props.load(SchedulerDemo.class.getResourceAsStream("quartz.properties")); schedulerFactory = new StdSchedulerFactory(props); } catch (Exception e) { System.out.println("[StdSchedulerFactory] init error"); } } public static class HelloQuartzJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Hello Quartz! - executing its Job at [" + SDF.format(new Date()) + "] by [" + context.getTrigger().getDescription() + "]"); } } public String formatJobName(Trigger trigger , String name) { return trigger.getClass().getSimpleName() + "_" + name + "_" + System.currentTimeMillis(); } public String formatTriggerName(Class<? extends Trigger> clazz) { return clazz.getSimpleName() + "_" + System.currentTimeMillis(); } public void addJobListenerToJob(Scheduler scheduler, JobListener listener, Matcher<JobKey> matcher) throws Exception { scheduler.getListenerManager().addJobListener(listener, matcher); } /** * JobDetail的构造通过JobBuilder构建,JobBuilder在内部调用JobDetail的实现类JobDetailImpl * */ public void run(Trigger trigger) throws SchedulerException { try { Scheduler scheduler = schedulerFactory.getScheduler(); String jobName = formatJobName(trigger,"HelloQuartz"); String jobGroup = "demo"; JobDetail jobDetail = JobBuilder.newJob(HelloQuartzJob.class) .withIdentity(jobName, jobGroup) .build(); /**添加监听器*/ Matcher<JobKey> matcher = KeyMatcher.keyEquals(new JobKey(jobName,jobGroup)); addJobListenerToJob(scheduler, new SimpleJobListener(), matcher); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); } catch (Exception e) { System.out.println("Quartz运行异常[" + e.getMessage() + "]"); } } /** * Trigger的构造通过TriggerBuilder创建 * */ public Trigger getSimpleTrigger() { SimpleTrigger simpleTrigger = (SimpleTrigger) TriggerBuilder.newTrigger() .withIdentity(formatTriggerName(SimpleTrigger.class), "demo") .withDescription("SimpleTrigger") .startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) .withRepeatCount(5)) .build(); return simpleTrigger; } public static void main(String []args) throws SchedulerException { SchedulerDemo schedulerDemo = new SchedulerDemo(); schedulerDemo.run(schedulerDemo.getSimpleTrigger()); } }