Quartz(入门详解)PPT参考幻灯片
- 格式:ppt
- 大小:190.40 KB
- 文档页数:29


详细解析Quartz的底层实现原理和逻辑(英文介绍):Quartz is a fully-featured, open-source job scheduling library that can beintegrated into virtually any Java application. It provides a robust and flexible way to schedule jobs (tasks) to run at specific times or intervals, enabling developers to automate various tasks like sending emails, generating reports, or performing data synchronization.At its core, Quartz relies on a few key components to facilitate its schedulingcapabilities:1.Job: This represents the task or piece of work that needs to be performed. Developerscreate job classes by implementing the Job interface and defining the business logic to be executed within the execute method.2.JobDetail: This contains the metadata about the job, such as its name, group, and anyjob-specific data (known as job data map) that might be required during execution. It essentially provides the context for the job.3.Trigger: A trigger determines the scheduling rules for a job. It defines when the jobshould be executed, whether it's a one-time event or a recurring one, and any associated scheduling parameters (like delay, repeat interval, etc.). Quartz supports various types of triggers, including simple triggers for basic scheduling needs and cron triggers for more complex scheduling patterns.4.Scheduler: This is the heart of the Quartz framework. The scheduler is responsible formanaging the lifecycle of jobs and triggers, as well as coordinating the execution of jobs based on their associated triggers. When a trigger's scheduling criteria are met, thescheduler selects an available worker thread from its thread pool to execute thecorresponding job.Quartz maintains a persistent store (typically a database) to ensure the reliability and durability of scheduled jobs. This allows the scheduler to recover fromfailures or system restarts, ensuring that no jobs are missed or executedincorrectly.The scheduling process in Quartz follows a simple yet effective logic:1.Developers define jobs by implementing the Job interface and creating job classes.2.They create JobDetail instances to provide metadata and context for the jobs.3.Developers define triggers to specify when and how the jobs should be executed.4.The scheduler is initialized and started, typically during application startup.5.As the scheduler runs, it constantly evaluates the triggers to determine if any of them aredue for execution.6.When a trigger's conditions are met, the scheduler selects a worker thread, associates itwith the job, and executes the job's business logic.7.After job execution, the scheduler updates the job and trigger states in the persistent store,along with any relevant job execution details.This underlying implementation and logic make Quartz a powerful and versatile tool for managing scheduled tasks in Java applications.详细解析Quartz的底层实现原理和逻辑(中文介绍):Quartz是一个功能齐全、开源的作业调度库,几乎可以集成到任何Java应用中。
Quartz的使用说明Quartz是一个完全由java编写的开源作业调度框架,具体的介绍可到/quartz/官方网站查看。
Quartz的几个核心的接口和类为:Job接口:自己写的"定时程序"实现此接口的voidexecute(JobExecutionContext arg0)方法,Job还有一类为有状态的StatefulJob接口,如果我们需要在上一个作业执行完后,根据其执行结果再进行下次作业的执行,则需要实现此接口。
Trigger抽象类:调度类(Scheduler)在时间到时调用此类,再由trigger类调用指定的定时程序。
Quertz中提供了两类触发器为:SimpleTrigger,CronTrigger。
前者用于实现比较简单的定时功能,例如几点开始,几点结束,隔多长时间执行,共执行多少次等,后者提供了使用表达式来描述定时功能,因此适用于比较复杂的定时描述,例如每个月的最后一个周五,每周的周四等。
JobDetail类:具体某个定时程序的详细描述,包括Name,Group,JobDataMap等。
JobExecutionContext类:定时程序执行的run-time的上下文环境,用于得到当前执行的Job的名字,配置的参数等。
JobDataMap类:用于描述一个作业的参数,参数可以为任何基本类型例如String,float等,也可为某个对象的引用.JobListener,TriggerListener接口:用于监听触发器状态和作业扫行状态,在特写状态执行相应操作。
JobStore类:在哪里执行定进程序,可选的有在内存中,在数据库中。
简单的定时程序:时钟调制类:import org.apache.log4j.Logger;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.impl.StdSchedulerFactory;public class QuartzManage {Logger log = Logger.getLogger(QuartzManage.class);public void startup(){try {log.debug("-----Initializing Scheduler------------"); SchedulerFactory sf = new StdSchedulerFactory();Scheduler sched = sf.getScheduler();sched.start();log.debug("Started Scheduler--------------");} catch (SchedulerException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}实现Job类:import java.util.Date;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.quartz.StatefulJob;public class TestJob implements Job {@Overridepublic void execute(JobExecutionContext arg0) throws JobExecutionException {// TODO Auto-generated method stubSystem.out.print("#################33" + new Date());try {Thread.currentThread().sleep(4000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}执行上面这个类基本实现了一个简单的定时程序。