c# - 我的工作不会中断 IScheduler.Interrupt

标签 c# quartz.net

我正在尝试实现一个 IInterruptableJob 接口(interface),以便我可以停止我的工作。

这就是我的工作实现的样子(取自 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);                   

我是在job调度的时候断点取到job key,保存key然后测试中断。从数据库交叉验证。

但即使我发送了中断,工作仍在继续并且触发器仍在触发。我从 qrtz_simple_triggers 检查了这一点 table.TIMES_TRIGGERED 即使在发送中断后也总是递增。

我不确定我在做什么错。

最佳答案

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

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

关于c# - 我的工作不会中断 IScheduler.Interrupt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24015835/

相关文章:

asp.net - 通过 Web 服务的 Quartz.Net 负载平衡

c# - Quartz.Net 2.0.1 触发器

Quartz.net 配置可使用 XML,但在使用代码配置时会引发数据库连接错误

c# - 为什么 ItemsSource 不绑定(bind)到我的自定义 CollectionChanged 事件

c# - linq-to-sql 查询结果到 c# 字典 - 如何

c# - 为证书提供 EC 私钥以在 HttpClient C# 中使用

c# - 使用 Quartz.NET 调度 Cron 作业

c# - 为什么Regex的Matches函数这么快?

c# - 传递一个 byte[]

quartz-scheduler - 使用 Quartz.net 配置 ADOJobStore