修改及建議: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 中更多地了解它們洒宝。