Quartz Quick Start Guide

来源:互联网 发布:sql语句面试题 编辑:程序博客网 时间:2024/05/21 09:18

And also learned this today:

http://www.quartz-scheduler.org/docs/quick_start_guide.html

更详细的是:

http://www.quartz-scheduler.org/docs/1.x/tutorial/index.html

更更详细的是官网的documentation了,啥都有。

 

一个最简单的使用scheduler的例子:

 

StdSchedulerFactory sf = new StdSchedulerFactory();
            sf.initialize(schedulerProperties); --读取quartz.properties 并初始化。也就是说我们可以在properties文件里定义scheduler。
            scheduler = sf.getScheduler();

scheduler.start();

但是在代码里直接定义也可以。下面是一个简单完整的quzrtz例子。

http://www.javacodegeeks.com/2012/07/quartz-2-scheduler-example.html

 

下面是几个重要的概念:

 

1,Jobs and Triggers

The JobDetail object is created by the Quartz client (your program) at the time the Job is added to the scheduler.

Trigger objects are used to trigger the execution (or 'firing') of jobs.

 

Many job schedulers do not have separate notions of jobs and triggers. Some define a 'job' as simply an execution time (or schedule) along with some small job identifier. Others are much like the union of Quartz's job and trigger objects. While developing Quartz, we decided that it made sense to create a separation between the schedule and the work to be performed on that schedule. This has (in our opinion) many benefits.

 

 Jobs and triggers can also be placed into 'groups' which can be useful for organizing your jobs and triggers into categories for later maintenance.

 

While a class that you implement is the actual "job", Quartz needs to be informed about various attributes that you may wish the job to have. This is done via the JobDetail class

 

下面这个例子更好的说明了,如何使用job和trigger。

JobDetail jobDetail = new JobDetail("myJob", // job name sched.DEFAULT_GROUP, // job group (you can also specify 'null' to use the default group) DumbJob.class); // the java class to execute Trigger trigger = TriggerUtils.makeDailyTrigger(8, 30); trigger.setStartTime(new Date()); trigger.setName("myTrigger"); sched.scheduleJob(jobDetail, trigger);

2,JobDataMap

The JobDataMap can be used to hold any number of (serializable) objects which you wish to have made available to the job instance when it executes. JobDataMap is an implementation of the Java Map interface

相当于存储在job details中的一些属性/状态。在job被执行期间可以被取出。

 

3,JobStore

org.quartz.jobStore.class = com.***.***.common.scheduler.XmlJobStore(需要实现了JobStore接口)

JobStore's are responsible for keeping track of all the "work data" that you give to the scheduler: jobs, triggers, calendars, etc.

也就是说,jobstore是一个数据结构,存储了一个运行期间scheduler包含的job,trigger等等。