大家好,欢迎来到IT知识分享网。
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://haidsoft.com/108670.html