Quartz.Net Tutorial–Lesson 2: Jobs and Triggers
Jobs and triggers are at the very core of the Quartz.Net scheduler. In a nutshell, jobs DO the work and triggers determine WHEN the work gets done.
Quartz.Net Jobs
Creating a Quartz.Net job is pretty straightforward. First, you must implement the IJob interface. The IJob interface is pretty simple. Here it is:
public interface IJob { void Execute(IJobExecutionContext context); }
When it is time for the job to run or be executed, the scheduler calls the job’s Execute method and passes in the context. The context contains information about the job’s execution environment. The most important part of the context is probably the JobDataMap, which is used to pass parameters to the job. You can access the context’s JobDataMap via the MergedJobDataMap property. Here is the IJobExecutionContext interface:
public interface IJobExecutionContext { IScheduler Scheduler { get; }ITrigger Trigger { get; }
ICalendar Calendar { get; }
bool Recovering { get; }
int RefireCount { get; }
JobDataMap MergedJobDataMap { get; }
IJobDetail JobDetail { get; }
IJob JobInstance { get; }
DateTimeOffset? FireTimeUtc { get; }
DateTimeOffset? ScheduledFireTimeUtc { get; }
DateTimeOffset? PreviousFireTimeUtc { get; }
DateTimeOffset? NextFireTimeUtc { get; }
string FireInstanceId { get; }
object Result { get; set; }
TimeSpan JobRunTime { get; }
void Put(object key, object objectValue);
object Get(object key); }
Quart.Net Included Jobs
The Quartz.Net distribution includes these jobs:
Quartz.Net Triggers
As mentioned earlier, triggers determine WHEN a job executes. A job can have zero or more triggers. A job with no triggers does not execute, so while this it’s possible to add a job to the scheduler and not add triggers to it, it’s not all that useful. However, being able to have more than one trigger associated with a job is quite useful, since it gives you plenty of flexibility around scheduling your job.
A trigger can only be assigned to one job. This is done by setting the JobName and JobGroup properties of the trigger to the name and group of the job you want triggered. The combination of JobName and JobGroup uniquely identify a job in Quartz.Net. In this version of Quartz.Net you can also use set the JobKey property by passing in a JobKey, which is in essence a container for the job’s name and group.
A trigger is also uniquely identified by its Name and Group properties. Names must be unique within a trigger group and name/group combinations must be unique within a Quartz.Net scheduler.
The Quartz.net distribution includes these triggers:
Identifying Jobs and Triggers
As mentioned earlier, jobs and triggers are uniquely identified within a Quartz.Net scheduler by the combination of the name and group properties. Job groups must be unique and trigger groups must be unique as well. Quartz.Net organizes jobs and triggers within these groups and therefore the job names must be unique within a job group. By the same token, trigger names must be unique within trigger groups.
JobKey
In Quartz.Net 2 you will typically use the JobKey object to uniquely identify a job. A JobKey is basically a container that holds the job name and job group in one object. Here is the class definition:
[Serializable] public sealed class JobKey : Key<JobKey> { public JobKey(string name) : base(name, null) { }public JobKey(string name, string group) : base(name, group) { } public static JobKey Create(string name) { return new JobKey(name, null); } public static JobKey Create(string name, string group) { return new JobKey(name, group); }
}
TriggerKey
In Quartz.Net 2 you will typically use the TriggerKey object to uniquely identify a trigger. A TriggerKey is basically a container that holds the trigger name and trigger group in one object. Here is the class definition:
[Serializable] public sealed class TriggerKey : Key<TriggerKey> { public TriggerKey(string name) : base(name, null) { }public TriggerKey(string name, string group) : base(name, group) { }
}
More Information
At this point you probably have enough information to go tinker around and build some jobs and triggers. If you would like to learn more about jobs and triggers, you can take a look at Lesson 3: Jobs In Detail or at Lesson 4: Triggers in Detail.