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 中更多地瞭解它們。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章