Quartz核心原理之架构及基本元素介绍 | 京东物流技术团队

京东云开发者
• 阅读 187

1 什么是Quartz

Quartz是一个作业调度框架,它可以与J2EE和J2SE应用相结合,也可以单独使用。它能够创建多个甚至数万个jobs这样复杂的程序,jobs可以做成标准的java组件或EJBS。Quartz很容易上手,创建一个任务仅需实现Job接口,该接口只有一个方法void execute(JobExecutionContext context) throws JobExecutionException;在java实现类添加作业逻辑,当配置好Job实现类并设置调度时间表后,Quartz将会监控任务的剩余时间,当调度程序确定需要通知需要执行该任务的时候,Quartz将会调用Job实现类的execute方法执行任务。

2 系统设计

Quartz框架的原理主要是通过将Job注册到调度器,再通过触发器策略执行Job,系统设计如下:

Quartz核心原理之架构及基本元素介绍 | 京东物流技术团队

3 核心元素介绍

Quartz框架的核心是调度器scheduler,核心的组件包括Job(任务)、JobDetail(任务描述)、Trigger(触发器)。调度器负责管理Quartz应用运行时环境。调度器不是靠自己做所有的工作,而是依赖框架内一些非常重要的部件。Quartz不仅仅是线程和线程管理。为确保可伸缩性,Quartz采用了基于多线程的架构。启动时,框架初始化一套worker线程,这套线程被调度器用来执行预定的作业。这就是Quartz怎样能并发运行多个作业的原理。Quartz依赖一套松耦合的线程池管理部件来管理线程环境。

3.1 Job

Job是一个接口,只有一个方法void execute(JobExecutionContext context) throws JobExecutionException。作业类需要实现接口中的execute方法,JobExecutionContext提供了调度的上下文信息,每一次执行Job都会重新创建一个Job对象实例。如下:

public interface Job {

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * 
     * Interface.
     * 
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    /**
     * <p>
     * Called by the <code>{@link Scheduler}</code> when a <code>{@link Trigger}</code>
     * fires that is associated with the <code>Job</code>.
     * </p>
     * 
     * <p>
     * The implementation may wish to set a 
     * {@link JobExecutionContext#setResult(Object) result} object on the 
     * {@link JobExecutionContext} before this method exits.  The result itself
     * is meaningless to Quartz, but may be informative to 
     * <code>{@link JobListener}s</code> or 
     * <code>{@link TriggerListener}s</code> that are watching the job's 
     * execution.
     * </p>
     * 
     * @throws JobExecutionException
     *           if there is an exception while executing the job.
     */
    void execute(JobExecutionContext context)
        throws JobExecutionException;

}
public class ClosePayJob implements Job{
public void execute(JobExecutionContext context)  throws JobExecutionException{
//业务逻辑
}
}

3.2 JobDetail

Quartz框架在每次执行Job时,都会重新创建一个Job对象实例,所以它需要Job类的信息以及其他相关信息,以便能够在运行时通过newInstance()的反射机制实例化Job。因此需要通过一个类来描述Job的实现类及其它相关的静态信息,比如Job名字、描述、关联监听器等信息。JobDetail接口包含了能够创建Job类的信息载体,用来保存任务的详细信息。如下代码定义

public interface JobDetail extends Serializable, Cloneable {

    public JobKey getKey();

    /**
     * <p>
     * Return the description given to the <code>Job</code> instance by its
     * creator (if any).
     * </p>
     * 
     * @return null if no description was set.
     */
    public String getDescription();

    /**
     * <p>
     * Get the instance of <code>Job</code> that will be executed.
     * </p>
     */
    public Class<? extends Job> getJobClass();

    /**
     * <p>
     * Get the <code>JobDataMap</code> that is associated with the <code>Job</code>.
     * </p>
     */
    public JobDataMap getJobDataMap();

    /**
     * <p>
     * Whether or not the <code>Job</code> should remain stored after it is
     * orphaned (no <code>{@link Trigger}s</code> point to it).
     * </p>
     * 
     * <p>
     * If not explicitly set, the default value is <code>false</code>.
     * </p>
     * 
     * @return <code>true</code> if the Job should remain persisted after
     *         being orphaned.
     */
    public boolean isDurable();

    /**
     * @see PersistJobDataAfterExecution
     * @return whether the associated Job class carries the {@link PersistJobDataAfterExecution} annotation.
     */
    public boolean isPersistJobDataAfterExecution();

    /**
     * @see DisallowConcurrentExecution
     * @return whether the associated Job class carries the {@link DisallowConcurrentExecution} annotation.
     */
    public boolean isConcurrentExectionDisallowed();

    /**
     * <p>
     * Instructs the <code>Scheduler</code> whether or not the <code>Job</code>
     * should be re-executed if a 'recovery' or 'fail-over' situation is
     * encountered.
     * </p>
     * 
     * <p>
     * If not explicitly set, the default value is <code>false</code>.
     * </p>
     * 
     * @see JobExecutionContext#isRecovering()
     */
    public boolean requestsRecovery();

    public Object clone();

    /**
     * Get a {@link JobBuilder} that is configured to produce a 
     * <code>JobDetail</code> identical to this one.
     */
    public JobBuilder getJobBuilder();

}

3.3 Trigger

触发器(org.quartz.Trigger)抽象类的几个主要属性和JobDetail差不多,这里就不说明了,主要注意的是misfireInstruction这个属性,misfireInstruction这个属性的是触发器错失执行(misfire)后的一个错失触发机制标识。当线程池中没有可用的线程执行任务时,就会错过触发时间,Trigger抽象类中默认错失触发机制是常量0(聪明的策略)。派生类有它们自己的错失触发机制。最常用的两种是SimpleTrigger和CronTrigger。

3.3.1 SimpleTrigger

指定从某一个时间开始,以一定的时间间隔(单位是毫秒)执行的任务。

它适合的任务类似于:9:00 开始,每隔1小时,执行一次。

它的属性有:

  • repeatInterval 重复间隔
  • repeatCount 重复次数。实际执行次数是 repeatCount+1。因为在startTime的时候一定会执行一次。** 下面有关repeatCount 属性的都是同理

3.3.2 CronTrigger

适合于更复杂的任务,它支持类型于Linux Cron的语法(并且更强大)。基本上它覆盖了其他Trigger的绝大部分能力—— 当然,也更难理解。

它适合的任务类似于:每天0:00,9:00,18:00各执行一次。

它的属性只有:Cron表达式。

4 总结

本次文章只是对Quartz的架构和基本元素做了简单的介绍,后面我们会深入分析Quartz的核心类。

作者:京东物流 贾永强

来源:京东云开发者社区 自猿其说Tech 转载请注明来源

点赞
收藏
评论区
推荐文章
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Peter20 Peter20
3年前
mysql中like用法
like的通配符有两种%(百分号):代表零个、一个或者多个字符。\(下划线):代表一个数字或者字符。1\.name以"李"开头wherenamelike'李%'2\.name中包含"云",“云”可以在任何位置wherenamelike'%云%'3\.第二个和第三个字符是0的值wheresalarylike'\00%'4\
Stella981 Stella981
2年前
Nginx + lua +[memcached,redis]
精品案例1、Nginxluamemcached,redis实现网站灰度发布2、分库分表/基于Leaf组件实现的全球唯一ID(非UUID)3、Redis独立数据监控,实现订单超时操作/MQ死信操作SelectPollEpollReactor模型4、分布式任务调试Quartz应用
Wesley13 Wesley13
2年前
mysql中时间比较的实现
MySql中时间比较的实现unix\_timestamp()unix\_timestamp函数可以接受一个参数,也可以不使用参数。它的返回值是一个无符号的整数。不使用参数,它返回自1970年1月1日0时0分0秒到现在所经过的秒数,如果使用参数,参数的类型为时间类型或者时间类型的字符串表示,则是从1970010100:00:0
Easter79 Easter79
2年前
SpringBoot2.0高级案例(06):整合 QuartJob ,实现定时器实时管理
一、QuartJob简介1、一句话描述Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大。2、核心API(1)、Scheduler代表一个Quartz的独立运行容器,Scheduler将Trigger绑定到特定JobDetail,这样当Tri
Stella981 Stella981
2年前
Quartz开源作业调度框架原理及使用Quartz实现定时订单测试
QuartzQuartz是OpenSymphony开源组织在Jobscheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或EJBs。Quartz
Wesley13 Wesley13
2年前
Java日期时间API系列29
  Java开发过程中经常会用到定时任务job的场景,比如定时处理数据报表等问题,开源作业调度框架也非常多,常用的开源作业调度框架有:SpringTask、Quartz和xxljob等。各个框架的具体使用不再这里讨论,这里主要讨论一下其中cron表达式的计算应用,xktime中的应用。1.SpringTask中cron表达式的计算应用
Stella981 Stella981
2年前
SpringBoot2.0高级案例(06):整合 QuartJob ,实现定时器实时管理
一、QuartJob简介1、一句话描述Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大。2、核心API(1)、Scheduler代表一个Quartz的独立运行容器,Scheduler将Trigger绑定到特定JobDetail,这样当Tri
Easter79 Easter79
2年前
Spring的业务层和Web层
任务调度  quartz框架  quartz框架实现了Spring的任务调度,用户可以随意的定义触发器调度时间表,并将触发器和任务进行映射。quartz通过调度器、触发器和任务实现任务调度。  Job:主要用来设计任务实现的逻辑,并且只有一个方法execute。  JobDetail:主要用来通过newInst
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_