Quartz.NET的任务除了继承IJob之外,还可以继承IInterruptableJob。IInterruptableJob为可中断任务,相对于IJob多了个Interrupt接口实现,用于捕获中断事件,
当某个任务中断时,可调用Interrupt进行实际的业务处理。
具体代码如下:
public class DumbInterruptableJob : IInterruptableJob
{
private ILog log = LogManager.GetLogger(typeof(DumbInterruptableJob));
private bool interrupted;
private JobKey jobKey;
public virtual void Execute(IJobExecutionContext context)
{
jobKey = context.JobDetail.Key;
log.InfoFormat("---- {0} 执行时间 {1}", jobKey, DateTime.Now.ToString());
try
{
for (int i = 0; i < 4; i++)
{
try
{
Thread.Sleep(10 * 1000);
}
catch (Exception ignore)
{
Console.WriteLine(ignore.StackTrace);
}
if (interrupted)
{
log.InfoFormat("--- {0} -- 已被中断",