您现在的位置是:首页 > 文章详情

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

日期:2023-12-05点击:58

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,系统设计如下:

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 转载请注明来源

 

原文链接:https://my.oschina.net/u/4090830/blog/10319229
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章