如何创造条件,即使工作需要花费更多的时间每N秒运行一次石英工作(How to create quar

2019-09-02 20:42发布

我正在努力实现:我有一个触发其次将触发每5个secods和状态的工作,有时超过5秒(7例)和我现在有什么需要

start: 00:00:00
end  : 00:00:07
start: 00:00:07 < right after previous has finished

我想要的是 :

start: 00:00:00
it should run at 00:00:05 but it hasn't
end  : 00:00:07 
start: 00:00:10 (5 seconds after previous, successive or not)

我曾尝试quartz.net版本2和1。
工作:

    [PersistJobDataAfterExecution]
    [DisallowConcurrentExecution]
    public class StatefulJob : IJob (or IStatefulJob in 1.x version)
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("StateFull START " + DateTime.Now.ToString());
            Thread.Sleep(7000);
            Console.WriteLine("StateFull END " + DateTime.Now.ToString());
        }
    }

触发:

var trigger1 = TriggerBuilder
                .Create()
                .WithSimpleSchedule(x => 
                    x.WithIntervalInSeconds(timeout)
                    .RepeatForever()
                .Build();

编辑我曾尝试使用WithMisfireHandlingInstructionIgnoreMisfires()但missfires发生因调度是关机,或者因为没有可用的线程,在我的情况-工作不执行,因为我用StatefulJob。 也许我错了,但行为保持不变。

EDIT2好,用“跑步”解旗标工作在单线程应用程序完美。 但是,如果我运行在几个线程这项工作(不同PARAMS),它不会那么的工作,是有可能实现的行为像我想用石英?

Answer 1:

如果你让你做什么工作同时运行,但修改它什么也不做,如果作业已经运行,如类似。

public class StatefulJob : IJob 
{
    private static bool Running;

    public void Execute(IJobExecutionContext context)
    {                       
        if (Running)
            return;

        Running = true;                                     
        try
        {
            Console.WriteLine(" StateFull START " + DateTime.Now.ToString());
            Thread.Sleep(7000);
            Console.WriteLine(" StateFull END " + DateTime.Now.ToString());
        }
        finally
        {
            Running = false;
        }

    }            
}


文章来源: How to create quartz job that will runs every N seconds even if job takes more time