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


码字不易,与君共勉!:

码字不易,与君共勉!

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