Quartz 2.3.0 文档中英文参考 Lesson 2: The Quartz API, Jobs And Triggers

修改及建议:https://github.com/clxering/Quartz-Doc-Chinese-English-bilingual/blob/dev/Tutorials/Lesson-2

The Quartz API

Quartz 的 API

The key interfaces of the Quartz API are:

Quartz API 的主要接口有如下这些:

  • Scheduler - the main API for interacting with the scheduler.

用于与调度程序交互的主 API。

  • Job - an interface to be implemented by components that you wish to have executed by the scheduler.

由组件实现的接口,组件定义了要执行的内容。

  • JobDetail - used to define instances of Jobs.

用于定义作业实例。

  • Trigger - a component that defines the schedule upon which a given Job will be executed.

定义给定作业执行时间表的组件。

  • JobBuilder - used to define/build JobDetail instances, which define instances of Jobs.

用于定义/构建定义作业实例的 JobDetail 实例。

  • TriggerBuilder - used to define/build Trigger instances.

用于定义/构建触发器实例

A Scheduler’s life-cycle is bounded by it’s creation, via a SchedulerFactory and a call to its shutdown() method. Once created the Scheduler interface can be used add, remove, and list Jobs and Triggers, and perform other scheduling-related operations (such as pausing a trigger). However, the Scheduler will not actually act on any triggers (execute jobs) until it has been started with the start() method, as shown in Lesson 1.

调度程序的生命周期从 SchedulerFactory 创建起直到 shutdown() 方法的调用而止。一旦创建了 Scheduler 接口,就可以使用添加、删除和列出作业和触发器,并执行其他与调度相关的操作(如暂停触发器)。但是,在使用 start() 方法启动调度程序之前,调度程序不会实际操作任何触发器(执行作业),如第一课中所示。

Quartz provides builder classes that define a Domain Specific Language (or DSL, also sometimes referred to as a fluent interface). In the previous lesson you saw an example of it, which we present a portion of here again:

Quartz 提供了定义 Domain Specific Language(或 DSL,有时也称为 流畅接口)的 构建器 类。在上一节课中,你看到了一个例子,我们在这里再次展示:

// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
    .withIdentity("myJob", "group1") // name "myJob", group "group1"
    .build();

// Trigger the job to run now, and then every 40 seconds
Trigger trigger = newTrigger()
    .withIdentity("myTrigger", "group1")
    .startNow()
    .withSchedule(simpleSchedule()
        .withIntervalInSeconds(40)
        .repeatForever())
    .build();

// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);

The block of code that builds the job definition is using methods that were statically imported from the JobBuilder class. Likewise, the block of code that builds the trigger is using methods imported from the TriggerBuilder class - as well as from the SimpleScheduleBulder class.

作业定义的代码块使用从 JobBuilder 导入的静态方法构建。同样,触发器的代码块使用从 TriggerBuilder 类和 SimpleScheduleBulder 类导入的方法构建。

The static imports of the DSL can be achieved through import statements such as these:

DSL 的静态导入可以通过如下的 import 语句来实现:

import static org.quartz.JobBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.CalendarIntervalScheduleBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.DateBuilder.*;

The various ScheduleBuilder classes have methods relating to defining different types of schedules.

ScheduleBuilder 类的不同表现形式具有定义不同类型的时间表相关的方法。

The DateBuilder class contains various methods for easily constructing java.util.Date instances for particular points in time (such as a date that represents the next even hour - or in other words 10:00:00 if it is currently 9:43:27).

DateBuilder 类包含用 java.util.Date 实例构造特定时间点的各种方法。例如表示下一个偶数小时的日期,换句话说,如果当前是 9:43:27,则转化为 10:00:00

Jobs and Triggers

作业和触发器

A Job is a class that implements the Job interface, which has only one simple method:

Job 是实现 Job 接口的类,该接口只有一个简单的方法:

The Job Interface

Job 接口

package org.quartz;

public interface Job {

public void execute(JobExecutionContext context) throws JobExecutionException;
}

When the Job’s trigger fires (more on that in a moment), the execute(..) method is invoked by one of the scheduler’s worker threads. The JobExecutionContext object that is passed to this method provides the job instance with information about its run-time environment - a handle to the Scheduler that executed it, a handle to the Trigger that triggered the execution, the job’s JobDetail object, and a few other items.

当作业的触发器触发时(稍后将详细介绍),execute(..) 方法将由调度程序的一个工作线程调用。传递给该方法的 JobExecutionContext 对象为作业实例提供了有关其 运行时 环境的信息(执行它的调度程序的句柄、触发执行的触发器的句柄、作业的 JobDetail 对象和一些其他事项)。

The JobDetail object is created by the Quartz client (your program) at the time the Job is added to the scheduler. It contains various property settings for the Job, as well as a JobDataMap, which can be used to store state information for a given instance of your job class. It is essentially the definition of the job instance, and is discussed in further detail in the next lesson.

JobDetail 对象是在将作业添加到调度程序时由 Quartz 客户端(你的程序)创建的。它包含作业的各种属性设置,以及一个 JobDataMap,它可用于存储作业类的给定实例的状态信息。它本质上是作业实例的定义,并将在下一课中进行更详细的讨论。

Trigger objects are used to trigger the execution (or ‘firing’) of jobs. When you wish to schedule a job, you instantiate a trigger and ‘tune’ its properties to provide the scheduling you wish to have. Triggers may also have a JobDataMap associated with them - this is useful to passing parameters to a Job that are specific to the firings of the trigger. Quartz ships with a handful of different trigger types, but the most commonly used types are SimpleTrigger and CronTrigger.

触发器对象用于触发作业的执行(或 触发)。当你希望调度作业时,你可以实例化一个触发器并 使能 其属性,以提供你想要的调度。触发器还可能有一个与它们相关联的 JobDataMap(这对于将参数传递给特定于触发器的触发的作业非常有用)。Quartz 有几种不同的触发类型,但最常用的类型是 SimpleTrigger 和 CronTrigger

SimpleTrigger is handy if you need ‘one-shot’ execution (just single execution of a job at a given moment in time), or if you need to fire a job at a given time, and have it repeat N times, with a delay of T between executions. CronTrigger is useful if you wish to have triggering based on calendar-like schedules - such as every Friday, at noon or at 10:15 on the 10th day of every month.

如果你需要 一次性 执行(仅在给定时刻单个执行一个作业),或者你需要在给定时间触发一个作业,并让它重复 N 次,两次执行之间的延迟为 T,那么 SimpleTrigger 就非常方便。如果你希望根据类似日历的时间表进行触发,例如 每周五中午每月10日的10:15 ,CronTrigger 是非常有用的。

Why Jobs AND Triggers? 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.

什么是工作和触发器?许多作业调度程序没有作业和触发器的单独概念。有些人将 作业 定义为简单的执行时间(或调度)以及一些小型作业标识符。其他的则很像 Quartz 的工作和触发器对象的结合。在开发 Quartz 时,我们决定在日程安排和在该日程安排上执行的工作之间创建一个分离是有意义的。(在我们看来)这有许多好处。

For example, Jobs can be created and stored in the job scheduler independent of a trigger, and many triggers can be associated with the same job. Another benefit of this loose-coupling is the ability to configure jobs that remain in the scheduler after their associated triggers have expired, so that that it can be rescheduled later, without having to re-define it. It also allows you to modify or replace a trigger without having to re-define its associated job.

例如,可以独立于触发器而在作业调度程序中创建和存储作业,而且许多触发器可以与同一作业关联。这种松耦合的另一个好处是能够配置在关联触发器过期后仍留在调度器中的作业,以便以后可以重新调度,而不必重新定义它。它还允许你修改或替换触发器,而不必重新定义其关联的作业。

Identities

标识符

Jobs and Triggers are given identifying keys as they are registered with the Quartz scheduler. The keys of Jobs and Triggers (JobKey and TriggerKey) allow them to be placed into ‘groups’ which can be useful for organizing your jobs and triggers into categories such as reporting jobs and maintenance jobs. The name portion of the key of a job or trigger must be unique within the group - or in other words, the complete key (or identifier) of a job or trigger is the compound of the name and group.

当作业和触发器注册到 Quartz 调度器时,它们被赋予一个标识符 key。作业和触发器的 key(JobKey 和 TriggerKey)允许将它们放置到 中,这对于将作业和触发器组织到诸如 报告作业维护作业 等类别中非常有用。作业或触发器的 key 名部分在组中必须是唯一的。换句话说,作业或触发器的完整 key(或标识符)是名称和组的复合。

You now have a general idea about what Jobs and Triggers are, you can learn more about them in Lesson 3: More About Jobs & JobDetails and Lesson 4: More About Triggers.

现在你对 Jobs 和 Triggers 有了一个大致的了解,你可以在第三课:more about Jobs & JobDetails 和第四课:more about Triggers 中更多地了解它们。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容