监听器是在scheduler事件发生时能够执行动作的对象。可以看出,TriggerListeners接收与triggers相关的事件,而JobListeners则接收与Job相关的事件。
Trigger相关的事件包括:trigger触发、trigger未触发,以及trigger完成(由trigger触发的任务被完成)。
/// <summary>
/// The interface to be implemented by classes that want to be informed when a
/// <see cref="Trigger" /> fires. In general, applications that use a
/// <see cref="IScheduler" /> will not have use for this mechanism.
/// </summary>
/// <seealso cref="IScheduler" />
/// <seealso cref="Trigger" />
/// <seealso cref="IJobListener" />
/// <seealso cref="JobExecutionContext" />
/// <author>James House</author>
public interface ITriggerListener
{
/// <summary>
/// Get the name of the <see cref="ITriggerListener" />.
/// </summary>
string Name { get; }
/// <summary>
/// Called by the <see cref="IScheduler" /> when a <see cref="Trigger" />
/// has fired, and it's associated <see cref="JobDetail" />
/// is about to be executed.
/// <p>
/// It is called before the <see cref="VetoJobExecution" /> method of this
/// interface.
/// </p>
/// </summary>
/// <param name="trigger">The <see cref="Trigger" /> that has fired.</param>
/// <param name="context">
/// The <see cref="JobExecutionContext" /> that will be passed to the <see cref="IJob" />'s<see cref="IJob.Execute" /> method.
/// </param>
void TriggerFired(Trigger trigger, JobExecutionContext context);
/// <summary>
/// Called by the <see cref="IScheduler" /> when a <see cref="Trigger" />
/// has fired, and it's associated <see cref="JobDetail" />
/// is about to be executed.
/// <p>
/// It is called after the <see cref="TriggerFired" /> method of this
/// interface.
/// </p>
/// </summary>
/// <param name="trigger">The <see cref="Trigger" /> that has fired.</param>
/// <param name="context">
/// The <see cref="JobExecutionContext" /> that will be passed to
/// the <see cref="IJob" />'s<see cref="IJob.Execute" /> method.
/// </param>
bool VetoJobExecution(Trigger trigger, JobExecutionContext context);
/// <summary>
/// Called by the <see cref="IScheduler" /> when a <see cref="Trigger" />
/// has misfired.
/// <p>
/// Consideration should be given to how much time is spent in this method,
/// as it will affect all triggers that are misfiring. If you have lots
/// of triggers misfiring at once, it could be an issue it this method
/// does a lot.
/// </p>
/// </summary>
/// <param name="trigger">The <see cref="Trigger" /> that has misfired.</param>
void TriggerMisfired(Trigger trigger);
/// <summary>
/// Called by the <see cref="IScheduler" /> when a <see cref="Trigger" />
/// has fired, it's associated <see cref="JobDetail" />
/// has been executed, and it's <see cref="Trigger.Triggered" /> method has been
/// called.
/// </summary>
/// <param name="trigger">The <see cref="Trigger" /> that was fired.</param>
/// <param name="context">
/// The <see cref="JobExecutionContext" /> that was passed to the
/// <see cref="IJob" />'s<see cref="IJob.Execute" /> method.
/// </param>
/// <param name="triggerInstructionCode">
/// The result of the call on the <see cref="Trigger" />'s<see cref="Trigger.Triggered" /> method.
/// </param>
void TriggerComplete(Trigger trigger, JobExecutionContext context, SchedulerInstruction triggerInstructionCode);
}
与任务相关的事件包括:即将被执行的任务的通知和任务已经执行完毕的通知。
The.JobListener Interface
/// <summary>
/// The interface to be implemented by classes that want to be informed when a
/// <see cref="JobDetail" /> executes. In general, applications that use a
/// <see cref="IScheduler" /> will not have use for this mechanism.
/// </summary>
/// <seealso cref="IScheduler" />
/// <seealso cref="IJob" />
/// <seealso cref="JobExecutionContext" />
/// <seealso cref="JobExecutionException" />
/// <seealso cref="ITriggerListener" />
/// <author>James House</author>
public interface IJobListener
{
/// <summary>
/// Get the name of the <see cref="IJobListener" />.
/// </summary>
string Name { get; }
/// <summary>
/// Called by the <see cref="IScheduler" /> when a <see cref="JobDetail" />
/// is about to be executed (an associated <see cref="Trigger" />
/// has occured).
/// <p>
/// This method will not be invoked if the execution of the Job was vetoed
/// by a <see cref="ITriggerListener" />.
/// </p>
/// </summary>
/// <seealso cref="JobExecutionVetoed(JobExecutionContext)" />
void JobToBeExecuted(JobExecutionContext context);
/// <summary>
/// Called by the <see cref="IScheduler" /> when a <see cref="JobDetail" />
/// was about to be executed (an associated <see cref="Trigger" />
/// has occured), but a <see cref="ITriggerListener" /> vetoed it's
/// execution.
/// </summary>
/// <seealso cref="JobToBeExecuted(JobExecutionContext)" />
void JobExecutionVetoed(JobExecutionContext context);
/// <summary>
/// Called by the <see cref="IScheduler" /> after a <see cref="JobDetail" />
/// has been executed, and be for the associated <see cref="Trigger" />'s
/// <see cref="Trigger.Triggered" /> method has been called.
/// </summary>
void JobWasExecuted(JobExecutionContext context, JobExecutionException jobException);
}
使用你自定义的监听器
创建监听器很简单,创建一个实现Quartz.ITriggerListener或(和)Quartz.IJobListener的接口。监听器然后在执行的时候注册到scheduler中,而且必须给定一个名字(或者,它们必须通过他们的Name属性来介绍自己)。监听器可以被注册为“全局”的或者“非全局”。“全局”监听器接收所有triggers/jobs产生的事件,而“非全局”监听器只接受那些通过TriggerListenerNames属性 或 JobListenerNames()方法显式指定监听器名的triggers/jobs所产生的事件。
正如上面所说的那样,监听器在运行时向scheduler注册,并且不被存储在jobs 和triggers的JobStore中。Jobs和Trigger只存储了与他们相关的监听器的名字。因此,每次应用运行的时候,都需要向scheduler重新注册监听器。
向 Scheduler中加入一个JobListener
scheduler.AddGlobalJobListener(myJobListener);
或者
scheduler.AddJobListener(myJobListener);