代码之家  ›  专栏  ›  技术社区  ›  Carl Hörberg

Quartz.net中长期运行的作业

  •  1
  • Carl Hörberg  · 技术社区  · 15 年前

    我在Quartz.net上有一个工作,它经常触发,有时运行很长时间,如果该工作已经在运行,我该如何取消触发?

    5 回复  |  直到 11 年前
        1
  •  2
  •   Marko Lahma    15 年前

    更标准的方法是使用InterruptableJob,请参见 http://quartznet.sourceforge.net/faq.html#howtostopjob . 当然,这只是另一种表达“如果”!乔布林)

        2
  •  1
  •   Paul    15 年前

    你能不能在作业开始时设置某种全局变量(jobrunning=true),并在作业完成后将其恢复为false?

    然后当触发触发器时,只需运行代码if(jobrunning==false)

        3
  •  0
  •   Austin Salonen gmlacrosse    15 年前

    您的应用程序可以在启动时从作业列表中删除自己,并在关闭时插入自己。

        4
  •  0
  •   pirho    11 年前

    现在,您可以在触发器中使用“withmisfirehandlinginstructionignoremissfires”,并在作业中使用[disallowConcurrentExecution]属性。

        5
  •  0
  •   granadaCoder    11 年前

    这是我的实现(使用马尔可尔之前给出的链接建议)。

    我只是想保存一些输入。

    我在quartz.net是个新手,所以在下面加一列盐吧。

    public class AnInterruptableJob : IJob, IInterruptableJob
    {
    
        private bool _isInterrupted = false;
    
        private int MAXIMUM_JOB_RUN_SECONDS = 10;
    
        /// <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)
        {
    
    
            /* See http://aziegler71.wordpress.com/2012/04/25/quartz-net-example/ */
    
            JobKey key = context.JobDetail.Key;
    
            JobDataMap dataMap = context.JobDetail.JobDataMap;
    
            int timeOutSeconds = dataMap.GetInt("TimeOutSeconds");
            if (timeOutSeconds <= 0)
            {
                timeOutSeconds = MAXIMUM_JOB_RUN_SECONDS;
            }
    
            Timer t = new Timer(TimerCallback, context, timeOutSeconds * 1000, 0);
    
    
            Console.WriteLine(string.Format("AnInterruptableJob Start : JobKey='{0}', timeOutSeconds='{1}' at '{2}'", key, timeOutSeconds, DateTime.Now.ToLongTimeString()));
    
    
            try
            {
                Thread.Sleep(TimeSpan.FromSeconds(7));
            }
            catch (ThreadInterruptedException)
            {
            }
    
    
            if (_isInterrupted)
            {
                Console.WriteLine("Interrupted.  Leaving Excecute Method.");
                return;
            }
    
            Console.WriteLine(string.Format("End AnInterruptableJob (should not see this) : JobKey='{0}', timeOutSeconds='{1}' at '{2}'", key, timeOutSeconds, DateTime.Now.ToLongTimeString()));
    
        }
    
    
        private void TimerCallback(Object o)
        {
            IJobExecutionContext context = o as IJobExecutionContext;
    
            if (null != context)
            {
                context.Scheduler.Interrupt(context.FireInstanceId);
            }
        }
    
        public void Interrupt()
        {
            _isInterrupted = true;
            Console.WriteLine(string.Format("AnInterruptableJob.Interrupt called at '{0}'", DateTime.Now.ToLongTimeString()));
        }
    }