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.

用于與調(diào)度程序交互的主 API竭鞍。

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

由組件實現(xiàn)的接口,組件定義了要執(zhí)行的內(nèi)容。

  • JobDetail - used to define instances of Jobs.

用于定義作業(yè)實例呢蔫。

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

定義給定作業(yè)執(zhí)行時間表的組件攀唯。

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

用于定義/構(gòu)建定義作業(yè)實例的 JobDetail 實例低飒。

  • TriggerBuilder - used to define/build Trigger instances.

用于定義/構(gòu)建觸發(fā)器實例

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.

調(diào)度程序的生命周期從 SchedulerFactory 創(chuàng)建起直到 shutdown() 方法的調(diào)用而止霹崎。一旦創(chuàng)建了 Scheduler 接口胯陋,就可以使用添加蕾各、刪除和列出作業(yè)和觸發(fā)器扒磁,并執(zhí)行其他與調(diào)度相關(guān)的操作(如暫停觸發(fā)器)。但是式曲,在使用 start() 方法啟動調(diào)度程序之前妨托,調(diào)度程序不會實際操作任何觸發(fā)器(執(zhí)行作業(yè))缸榛,如第一課中所示。

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兰伤,有時也稱為 流暢接口)的 構(gòu)建器 類内颗。在上一節(jié)課中,你看到了一個例子敦腔,我們在這里再次展示:

// 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.

作業(yè)定義的代碼塊使用從 JobBuilder 導(dǎo)入的靜態(tài)方法構(gòu)建均澳。同樣,觸發(fā)器的代碼塊使用從 TriggerBuilder 類和 SimpleScheduleBulder 類導(dǎo)入的方法構(gòu)建符衔。

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

DSL 的靜態(tài)導(dǎo)入可以通過如下的 import 語句來實現(xiàn):

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 類的不同表現(xiàn)形式具有定義不同類型的時間表相關(guān)的方法找前。

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 實例構(gòu)造特定時間點的各種方法。例如表示下一個偶數(shù)小時的日期判族,換句話說躺盛,如果當(dāng)前是 9:43:27,則轉(zhuǎn)化為 10:00:00

Jobs and Triggers

作業(yè)和觸發(fā)器

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

Job 是實現(xiàn) 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.

當(dāng)作業(yè)的觸發(fā)器觸發(fā)時(稍后將詳細介紹)槽惫,execute(..) 方法將由調(diào)度程序的一個工作線程調(diào)用。傳遞給該方法的 JobExecutionContext 對象為作業(yè)實例提供了有關(guān)其 運行時 環(huán)境的信息(執(zhí)行它的調(diào)度程序的句柄辩撑、觸發(fā)執(zhí)行的觸發(fā)器的句柄界斜、作業(yè)的 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 對象是在將作業(yè)添加到調(diào)度程序時由 Quartz 客戶端(你的程序)創(chuàng)建的槐臀。它包含作業(yè)的各種屬性設(shè)置锄蹂,以及一個 JobDataMap,它可用于存儲作業(yè)類的給定實例的狀態(tài)信息水慨。它本質(zhì)上是作業(yè)實例的定義得糜,并將在下一課中進行更詳細的討論。

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.

觸發(fā)器對象用于觸發(fā)作業(yè)的執(zhí)行(或 觸發(fā))晰洒。當(dāng)你希望調(diào)度作業(yè)時朝抖,你可以實例化一個觸發(fā)器并 使能 其屬性,以提供你想要的調(diào)度谍珊。觸發(fā)器還可能有一個與它們相關(guān)聯(lián)的 JobDataMap(這對于將參數(shù)傳遞給特定于觸發(fā)器的觸發(fā)的作業(yè)非常有用)治宣。Quartz 有幾種不同的觸發(fā)類型,但最常用的類型是 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.

如果你需要 一次性 執(zhí)行(僅在給定時刻單個執(zhí)行一個作業(yè))砌滞,或者你需要在給定時間觸發(fā)一個作業(yè)侮邀,并讓它重復(fù) N 次,兩次執(zhí)行之間的延遲為 T贝润,那么 SimpleTrigger 就非常方便绊茧。如果你希望根據(jù)類似日歷的時間表進行觸發(fā),例如 每周五中午每月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.

什么是工作和觸發(fā)器鹏秋?許多作業(yè)調(diào)度程序沒有作業(yè)和觸發(fā)器的單獨概念。有些人將 作業(yè) 定義為簡單的執(zhí)行時間(或調(diào)度)以及一些小型作業(yè)標(biāo)識符亡笑。其他的則很像 Quartz 的工作和觸發(fā)器對象的結(jié)合侣夷。在開發(fā) Quartz 時,我們決定在日程安排和在該日程安排上執(zhí)行的工作之間創(chuàng)建一個分離是有意義的仑乌。(在我們看來)這有許多好處百拓。

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.

例如,可以獨立于觸發(fā)器而在作業(yè)調(diào)度程序中創(chuàng)建和存儲作業(yè)绝骚,而且許多觸發(fā)器可以與同一作業(yè)關(guān)聯(lián)耐版。這種松耦合的另一個好處是能夠配置在關(guān)聯(lián)觸發(fā)器過期后仍留在調(diào)度器中的作業(yè)祠够,以便以后可以重新調(diào)度压汪,而不必重新定義它。它還允許你修改或替換觸發(fā)器古瓤,而不必重新定義其關(guān)聯(lián)的作業(yè)止剖。

Identities

標(biāo)識符

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.

當(dāng)作業(yè)和觸發(fā)器注冊到 Quartz 調(diào)度器時,它們被賦予一個標(biāo)識符 key落君。作業(yè)和觸發(fā)器的 key(JobKey 和 TriggerKey)允許將它們放置到 中穿香,這對于將作業(yè)和觸發(fā)器組織到諸如 報告作業(yè)維護作業(yè) 等類別中非常有用。作業(yè)或觸發(fā)器的 key 名部分在組中必須是唯一的绎速。換句話說皮获,作業(yè)或觸發(fā)器的完整 key(或標(biāo)識符)是名稱和組的復(fù)合。

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.

現(xiàn)在你對 Jobs 和 Triggers 有了一個大致的了解纹冤,你可以在第三課:more about Jobs & JobDetails 和第四課:more about Triggers 中更多地了解它們洒宝。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市萌京,隨后出現(xiàn)的幾起案子雁歌,更是在濱河造成了極大的恐慌,老刑警劉巖知残,帶你破解...
    沈念sama閱讀 216,324評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件靠瞎,死亡現(xiàn)場離奇詭異,居然都是意外死亡求妹,警方通過查閱死者的電腦和手機乏盐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,356評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來制恍,“玉大人父能,你說我怎么就攤上這事“扇ぃ” “怎么了法竞?”我有些...
    開封第一講書人閱讀 162,328評論 0 353
  • 文/不壞的土叔 我叫張陵耙厚,是天一觀的道長。 經(jīng)常有香客問我岔霸,道長薛躬,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,147評論 1 292
  • 正文 為了忘掉前任呆细,我火速辦了婚禮型宝,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘絮爷。我一直安慰自己趴酣,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,160評論 6 388
  • 文/花漫 我一把揭開白布坑夯。 她就那樣靜靜地躺著岖寞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪柜蜈。 梳的紋絲不亂的頭發(fā)上仗谆,一...
    開封第一講書人閱讀 51,115評論 1 296
  • 那天,我揣著相機與錄音淑履,去河邊找鬼隶垮。 笑死,一個胖子當(dāng)著我的面吹牛秘噪,可吹牛的內(nèi)容都是我干的狸吞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,025評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了担败?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,867評論 0 274
  • 序言:老撾萬榮一對情侶失蹤暖侨,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后崇渗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體字逗,經(jīng)...
    沈念sama閱讀 45,307評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,528評論 2 332
  • 正文 我和宋清朗相戀三年宅广,在試婚紗的時候發(fā)現(xiàn)自己被綠了葫掉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,688評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡跟狱,死狀恐怖俭厚,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情驶臊,我是刑警寧澤挪挤,帶...
    沈念sama閱讀 35,409評論 5 343
  • 正文 年R本政府宣布叼丑,位于F島的核電站,受9級特大地震影響扛门,放射性物質(zhì)發(fā)生泄漏鸠信。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,001評論 3 325
  • 文/蒙蒙 一论寨、第九天 我趴在偏房一處隱蔽的房頂上張望星立。 院中可真熱鬧,春花似錦葬凳、人聲如沸绰垂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,657評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽劲装。三九已至,卻和暖如春荐健,著一層夾襖步出監(jiān)牢的瞬間酱畅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,811評論 1 268
  • 我被黑心中介騙來泰國打工江场, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人窖逗。 一個月前我還...
    沈念sama閱讀 47,685評論 2 368
  • 正文 我出身青樓址否,卻偏偏與公主長得像,于是被迫代替她去往敵國和親碎紊。 傳聞我的和親對象是個殘疾皇子佑附,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,573評論 2 353

推薦閱讀更多精彩內(nèi)容