多作业例子
在这个例子中,我们将介绍如何通过Quartz API 多个作业。在Quartz调度框架中,每个作业将被连接到一个唯一的触发,并且由调度器运行它。
备注说明:在 Quartz 中,一个触发器触发多个作业是不可以的。
第一步:创建3个作业,JobA,JobB和JobC。
JobA.class
import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;public class JobA implements Job{ @Override public void execute(JobExecutionContext context) throws JobExecutionException { // TODO Auto-generated method stub System.out.println("Job A is runing //every 5 seconds "); }}
JobB.class
import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;public class JobB implements Job{ @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { // TODO Auto-generated method stub System.out.println("Job B is runing"); }}
JobC.class
import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;public class JobC implements Job{ @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { // TODO Auto-generated method stub System.out.println("Job C is runing"); }}
第二步:使用QuartzAPI声明上述3个作业,分配它们到特定触发器并调度它。
import org.quartz.CronScheduleBuilder;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.JobKey;import org.quartz.Scheduler;import org.quartz.Trigger;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;// 这里引入自己的Job 的地址import com.demo.quartz.job.JobA;import com.demo.quartz.job.JobB;import com.demo.quartz.job.JobC;public class CronTriggerMultiJob { public static void main(String[] args) throws Exception { JobKey jobKeyA = new JobKey("JobA", "group1"); JobDetail jobA = JobBuilder.newJob(JobA.class).withIdentity(jobKeyA) .build(); JobKey jobKeyB = new JobKey("JobB", "group1"); JobDetail jobB = JobBuilder.newJob(JobB.class).withIdentity(jobKeyB) .build(); JobKey jobKeyC = new JobKey("JobC", "group1"); JobDetail jobC = JobBuilder.newJob(JobC.class).withIdentity(jobKeyC) .build(); Trigger trigger1 = TriggerBuilder .newTrigger() .withIdentity("dummyTriggerName1", "group1") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .build(); Trigger trigger2 = TriggerBuilder .newTrigger() .withIdentity("dummyTriggerName2", "group1") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .build(); Trigger trigger3 = TriggerBuilder .newTrigger() .withIdentity("dummyTriggerName3", "group1") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .build(); Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(jobA, trigger1); scheduler.scheduleJob(jobB, trigger2); scheduler.scheduleJob(jobC, trigger3); }}
输出的结果如下:
Job A is runing //every 5 secondsJob B is runingJob C is runingJob A is runing //every 5 secondsJob B is runingJob C is runing
监听例子
在这个例子中,我们回学到如何创建一个 JobListener, 跟踪运行工作状态在作业完成。
第一步:创建一个作业,并在作业中抛出一个异常(JobExecutionException)
HelloJob.java
import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;public class HelloJob implements Job{ @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { // TODO Auto-generated method stub System.out.println("Hello Quartz!"); throw new JobExecutionException("Testing Exception"); }}
第二步:创建监听
HelloJobListener.java
import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.quartz.JobListener;public class HelloJobListener implements JobListener { public static final String LISTENER_NAME = "dummyJobListenerName"; @Override public String getName() { return LISTENER_NAME; // 必须要返回一个监听的名字 } /** * 当 job 执行的时候会调用这个方法 */ @Override public void jobExecutionVetoed(JobExecutionContext context) { String jobName = context.getJobDetail().getKey().toString(); System.out.println("jobToBeExecuted"); System.out.println("Job : " + jobName + " is going to start..."); } @Override public void jobToBeExecuted(JobExecutionContext context) { System.out.println("jobExecutionVetoed"); } /** * job 执行后运行 */ @Override public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) { System.out.println("jobWasExecuted"); String jobName = context.getJobDetail().getKey().toString(); System.out.println("Job : " + jobName + " is finished..."); if (!jobException.getMessage().equals("")) { System.out.println("Exception thrown by: " + jobName + " Exception: " + jobException.getMessage()); } }}
第三步:测试执行
HelloJobListenerTest.java
import org.quartz.CronScheduleBuilder;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.JobKey;import org.quartz.Scheduler;import org.quartz.Trigger;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;import org.quartz.impl.matchers.KeyMatcher;public class HelloJobListenerTest { public static void main(String[] args) throws Exception { // 新建一个jobKey JobKey jobKey = new JobKey("dummyJobName", "group1"); // name dummyJobName group group1 JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity(jobKey) .build(); Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("dummyTriggerName", "group1") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) // 每5秒执行一次 .build(); Scheduler scheduler = new StdSchedulerFactory().getScheduler(); // 添加 监听到 jobKey scheduler.getListenerManager().addJobListener(new HelloJobListener(), KeyMatcher.keyEquals(jobKey)); scheduler.start(); scheduler.scheduleJob(job, trigger); }}
输出结果
jobExecutionVetoedHello Quartz!五月 26, 2018 3:23:15 下午 org.quartz.core.JobRunShell run信息: Job group1.dummyJobName threw a JobExecutionException: org.quartz.JobExecutionException: Testing Exception at com.wq.study.quartz.HelloJob.execute(HelloJob.java:14) at org.quartz.core.JobRunShell.run(JobRunShell.java:202) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)jobWasExecutedJob : group1.dummyJobName is finished...Exception thrown by: group1.dummyJobName Exception: Testing Exception