Quartz.NET教程_Lesson 2: Jobs And Triggers

課程2:作業和觸發器


The Quartz API
Quartz框架API接口

The key interfaces and classes of the Quartz API are:
Quartz框架的核心接口和類是:


IScheduler - the main API for interacting with the scheduler.
IScheduler - 主接口用於和調度器進行交互。

IJob - an interface to be implemented by components that you wish to have executed by the scheduler.
IJob - 一個需要被實現的接口用於實現你希望調度器執行的具體任務組件。

IJobDetail - used to define instances of Jobs.
IJobDetail - 用來定義作業實例的接口.

ITrigger - a component that defines the schedule upon which a given Job will be executed.
ITrigger - 一個組件用於確定調度器會執行給定的作業。

JobBuilder - used to define/build JobDetail instances, which define instances of Jobs.
JobBuilder - 用於定義/創建JobDetail實例,JobDetail會定義具體作業的實例。

TriggerBuilder - used to define/build Trigger instances.
TriggerBuilder - 用於定義/創建觸發器實例。


In this tutorial for readability’s sake following terms are used interchangeably: IScheduler and Scheduler, IJob and Job, IJobDetail and JobDetail, ITrigger and Trigger.

在這個手冊中爲了可讀性考慮,以下的專業詞詞彙可以互相表示:IScheduler(調度器接口) and Scheduler(調度器), IJob(作業接口) and Job(作業), IJobDetail(作業詳情接口) and JobDetail(作業詳情), ITrigger(觸發器接口) and Trigger(觸發器).

A Scheduler’s life-cycle is bounded by it’s creation, via a SchedulerFactory and a call to its Shutdown() method. Once created the IScheduler 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()方法結束。一旦被創建調度器接口可以用來新增,移除和列出所有的作業和觸發器,並且能夠進行調度相關的操作(例如暫停一個觸發器)。無論如何,調度器之後在通過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框架提供“建造者”類,用於定義領域特定語言()。在之前的課程中,你看到一個其實例,這裏我們重新把這部分列出來:

    // define the job and tie it to our HelloJob class
    IJobDetail job = JobBuilder.Create<HelloJob>()
        .WithIdentity("myJob", "group1") // name "myJob", group "group1"
        .Build();

    // Trigger the job to run now, and then every 40 seconds
    ITrigger trigger = TriggerBuilder.Create()
        .WithIdentity("myTrigger", "group1")
        .StartNow()
        .WithSimpleSchedule(x => x
            .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 JobBuilder using fluent interface to create the product, IJobDetail. Likewise, the block of code that builds the trigger is using TriggerBuilder’s fluent interface and extension methods that are specific to given trigger type. Possible schedule extension methods are:
這個創建作業定義的代碼塊用了JobBuilder通過連貫接口的形式創建了目標對象,IJobDetail.同樣的,創建觸發器的代碼塊同樣使用了TriggerBuilder的連貫接口和特殊觸發器的拓展方法。可能的調度器拓展方法如下:

WithCalendarIntervalSchedule
WithCronSchedule
WithDailyTimeIntervalSchedule
WithSimpleSchedule

The DateBuilder class contains various methods for easily constructing DateTimeOffset 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).
The DateBuilder(數據建造者)類包含了很多的方法,用來輕鬆的創建DateTimeOffset實例表示某一個特定的時間點(例如一個日期,表示下一個整點的時間 - 換而言之,如果當前時間是9:43:27,則取10:00:00)。

Jobs and Triggers
作業和觸發器


A Job is a class that implements the IJob interface, which has only one simple method:
一個作業就是一個實現了IJob接口的類,該接口中只有一個簡單的方法:

IJob Interface
    namespace Quartz
    {
        public interface IJob
        {
            void Execute(JobExecutionContext context);
        }
    }

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.NET 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.NET客戶端創建。其包含了很多作業的屬性設置,以及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 (interface ISimpleTrigger) and CronTrigger (interface ICronTrigger).
觸發器對象用來觸發作業的執行。當你希望調度一個作業,你需要實例化一個觸發器,並且將給定的屬性提供給調度過程。觸發器也有和其相關聯的JobDataMap對象 - 這對於傳遞參數給觸發器觸發的特定作業很有用。Quartz包含了很多不同類型的觸發器,但是最爲常用的類是SimpleTrigger (接口ISimpleTrigger) and CronTrigger (接口ICronTrigger)

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.”
SimpleTrigger用起來很容易上手,如果你需要的是“一次性”的執行任務(在給定的時間執行單個作業進行單次執行),或者如果你需要在特定的時間執行作業,並且需要重複N次,在任務執行間隔T的時間。CronTrigger則在你希望進行日曆式的觸發任務時,會非常的有用 - 例如你希望在“每個星期五的中午“每個月10號的10:15”。


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調度器之後,會被賦予用於身份標識的鍵。這些鍵值被作業和觸發器放置在“groups”組當中,對於你管理作業和觸發器,並將其分類的過程時,非常的有用,比如,分類成“報表作業”和“維護作業”。作業或者觸發器的鍵的名稱部分,必須唯一 - 或者換句話說,作業或者觸發器的完整的鍵(或者身份標識)是名稱和組的混合。


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


碼字不易,與君共勉!:

碼字不易,與君共勉!

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