我的工作不会在IScheduler.Interrupt中断

时间:2014-06-03 12:58:01

标签: c# quartz.net

我正在尝试实施一个IInterruptableJob界面,以便我可以停止工作。

这就是我的工作实现方式(取自https://github.com/quartznet/quartznet/blob/master/src/Quartz.Examples/example7/InterruptExample.cs

public class HelloJob : IInterruptableJob
    {
        // logging services
        private static readonly ILog Log = LogManager.GetLogger(typeof(HelloJob));
        // has the job been interrupted?
        private bool _interrupted;
        // job name 
        private JobKey _jobKey;

        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" />
        /// fires that is associated with the <see cref="IJob" />.
        /// </summary>
        public virtual void Execute(IJobExecutionContext context)
        {
            _jobKey = context.JobDetail.Key;
            Log.InfoFormat("---- {0} executing at {1}", _jobKey, DateTime.Now.ToString("r"));

            try
            {
                // main job loop... see the JavaDOC for InterruptableJob for discussion...
                // do some work... in this example we are 'simulating' work by sleeping... :)

                for (int i = 0; i < 4; i++)
                {
                    try
                    {
                        Thread.Sleep(10 * 1000);
                    }
                    catch (Exception ignore)
                    {
                        Console.WriteLine(ignore.StackTrace);
                    }

                    // periodically check if we've been interrupted...
                    if (_interrupted)
                    {
                        Log.InfoFormat("--- {0}  -- Interrupted... bailing out!", _jobKey);
                        throw  new JobExecutionException("Interrupt job"); 
                        //return;
                        // could also choose to throw a JobExecutionException 
                        // if that made for sense based on the particular  
                        // job's responsibilities/behaviors
                    }
                }
            }
            finally
            {
                Log.InfoFormat("---- {0} completed at {1}", _jobKey, DateTime.Now.ToString("r"));
            }
        }

        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a user
        /// interrupts the <see cref="IJob" />.
        /// </summary>
        public virtual void Interrupt()
        {
            Log.Info("---  -- INTERRUPTING --");
            _interrupted = true;
        }

    }

这是我的主要方法。

private static void Main(string[] args)
        {
            try
            {
                LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter
                {
                    Level = LogLevel.Info
                };

                // Grab the Scheduler instance from the Factory 
                IScheduler scheduler = GetScheduler();

                //scheduler.Interrupt(@"group1.69decadb-2385-4c30-8d19-22088601670c");
                Guid guid = Guid.NewGuid();

                IJobDetail job = JobBuilder.Create<HelloJob>()
                    .WithIdentity(guid.ToString(), "group1")
                    .Build();

                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("trigger1", "group1")
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInSeconds(10)
                        .RepeatForever()
                        .WithMisfireHandlingInstructionNextWithRemainingCount())
                    .Build();

                var key = job.Key;
                // Tell quartz to schedule the job using our trigger
                scheduler.ScheduleJob(job, trigger);

            }
            catch (SchedulerException se)
            {
                Console.WriteLine(se);
            }

            Console.WriteLine("Press any key to close the application");
            Console.ReadKey();
        }

以正确的间隔时间解雇工作。但是,当我发送一个中断时,它不会停止工作:(

我试图用

来阻止这项工作
IScheduler scheduler = GetScheduler();
var jobkey = new JobKey("69decadb-2385-4c30-8d19-22088601670c","group1");

scheduler.Interrupt(jobkey);                   

我通过在作业调度时使用断点来获取作业密钥,保存密钥然后测试中断。交叉验证数据库。

但即使我发送中断,这项工作仍在继续,触发仍然会引发。即使在发送中断后,我也会从qrtz_simple_triggers table.TIMES_TRIGGERED始终以增量检查此内容。

我不确定我在做什么。

1 个答案:

答案 0 :(得分:3)

中断意味着用于运行工作。它并没有将整个工作置于暂停状态。无论是否在处理过程中被中断,都会触发作业(计数器增量)。中断意味着长时间运行的作业需要发出停止信号 - 下一次运行将按计划进行。

您可能正在寻找的是IScheduler.PauseJob(JobKey),这将暂停所有作业触发器,从而停止执行,直到再次恢复触发器。