(02) Jobs, Triggers, and jobstriggers

Source: Internet
Author: User

(02) Jobs, Triggers, and jobstriggers

Quart APIs

The key interfaces and classes in the Quartz API are as follows:

  • IScheduler-Main API for interacting with sched;
  • IJob-interfaces inherited and implemented by components and executed by the scheduler;
  • IJobDetail-generally used to define task instances;
  • ITrigger-a component that defines the time sequence for a given task to be executed;
  • JobBuilder-generally used to define (or create) JobDetail instances. This instance is used to define task instances;
  • TriggerBuilder-usually used to define (or create) trigger instances.

In this tutorial, the following terms are used alternately to improve readability: IScheduler and Scheduler, IJob and Job, IJobDetail and JobDetail, ITrigger and Trigger.

The life cycle after the scheduler is created is limited (SchedulerFactoryYou can disable the scheduler by using the Shutdown () method. After the isched interface is created, you can add, delete, and list tasks and triggers, and perform other operations related to the scheduling Plan (such as pausing a trigger ). However, as described in "using Quartz", the scheduler takes effect on the trigger (execute the task) only after the Start () method is called ).

The "builder" class provided by Quartz defines a specialized domain language (DSL, also often referred to as "stream interface "). We have seen its example in the previous section. Now we will show it below:

// 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 preceding code block shows how to use JobBuilder to create a task definition and use stream interfaces to create a task (IJobDetail ). Similarly, a trigger is created using the stream interface and Extension Method of TriggerBuilder for the specified trigger type. The possible time plan extension method is as follows:

  • WithCalendarIntervalSchedule
  • WithCronSchedule
  • WithDailyTimeIntervalSchedule
  • WithSimpleSchedule

The DateBuilder class contains many methods that make it easy to construct a DateTimeOffset instance at a specific time point (for example, representing the next even hour time, in other words, if it is now 9:43:27, it will return 10:00:00 ).

Tasks and triggers

A task is a class that implements IJob. There is only one simple method:

IJob interface:

Namespace Quartz
{
Public interface IJob
{
Void Execute (JobExecutionContext context );
}
}

When a task trigger is triggered, a worker thread of the scheduler will call the Execute (...) method. The JobExecutionContext object passed to this method provides a task instance that contains the following Runtime Environment: A scheduler handle that runs the current task and a trigger handle that triggers the current task, the JobDetail object of the current task, and several other objects.

After the scheduler loads the task, the Quartz. NET client (our application) will create the JobDetail object. This object contains various attribute settings of the task and a JobDataMap which can be used to store the status information of the specified task type. JobDetail is the essence of the task object. We will discuss it in more detail in the next section.

A trigger is used to trigger (or "ignition") the execution of a task. When you want to schedule a task, you need to initialize a trigger and adjust its attributes according to the scheduling plan you want. The trigger may also have an associated JobDataMap, which is useful for passing parameters to the task specified in the trigger operation. Quartz comes with some different trigger types, but the most commonly used is SimpleTrigger (the interface is ISimpleTrigger) and CronTrigger (the interface is ICronTrigger ).

If you want to execute a task only once at a specified time point, or repeat the task multiple times within a specified time point and delay a specific time before each execution, SimpleTrigger is convenient. If you want a trigger to have a schedule similar to a calendar, CronTrigger will be appropriate, for example, "Every Friday afternoon" or "every 10th days ".

Why are there tasks and triggers? Many task debugging tools do not separate trigger and task concepts. Some components simply treat the task as the execution time (or plan) that comes with a single task identifier ). Some components are like the combination of Quartz tasks and triggers. However, when developing Quartz, we decided to separate the scheduling plan from the operations to be executed in the scheduling plan. In our opinion, doing so has many advantages.

For example, a task can be created and stored in the task scheduler independently of a trigger, and multiple triggers can be associated with the same task. Another advantage of loose coupling is that for the tasks retained in the scheduler, when the associated trigger expires, you do not need to redefine the task to schedule it again; you can also modify or replace a trigger without redefining the tasks associated with it.

Identifier

When the task and trigger are registered in the Z scheduler, the primary key is assigned. The primary keys (JobKey and TriggerKey) of a task and trigger are very useful for merging its organization into a group and classifying it. For example, a task can be divided into report tasks or maintenance tasks ". The primary key names of tasks or triggers in the group cannot be the same. In other words, the names and group names of tasks or triggers are the complete primary keys (or identifiers ).

Original article: Lesson 2: Jobs And Triggers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.