博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Quartz学习之多作业、监听
阅读量:5795 次
发布时间:2019-06-18

本文共 6772 字,大约阅读时间需要 22 分钟。

多作业例子

在这个例子中,我们将介绍如何通过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

转载地址:http://iabfx.baihongyu.com/

你可能感兴趣的文章
科普长文:迈过5G这道“认知”坎
查看>>
白话中台战略:中台是个什么鬼?
查看>>
快手服务治理平台KESS的设计理念和实战
查看>>
拼多多回应漏洞:比薅羊毛更快的是“资损200亿”谣言的传播速度
查看>>
立下“去O”Flag的AWS,悄悄修炼了哪些内功?
查看>>
《The Startup Way》作者访谈
查看>>
独家 | 亲历IJCAI 2018,深度解读北京大学SentiGAN杰出论文
查看>>
Linux内存管理
查看>>
Datical为数据库添加持续交付能力
查看>>
用 JavaScript 实现链表操作 - 01 Push & Build List
查看>>
阿里巴巴 “百川解码“第一期精彩回顾:热修复的坑和阿里的解
查看>>
thinkphp下配置和使用阿里云redis
查看>>
神坑·Python 装饰类无限递归
查看>>
JS语言核心——“词法结构及类型、值和变量”
查看>>
通过源码解析 Node.js 中高效的 timer
查看>>
AHK 中读写文件的方法
查看>>
DevOps on Android: 加速 App 从代码到上线
查看>>
Linux 性能工具 sar 示例
查看>>
python 动态类型
查看>>
[Leetcode] Largest Number 最大整数
查看>>