如何创建石英作业,即使作业花费更多时间,该作业也将每N秒运行一次 [英] How to create quartz job that will runs every N seconds even if job takes more time

查看:124
本文介绍了如何创建石英作业,即使作业花费更多时间,该作业也将每N秒运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现的目标: 我还有第二个触发器,它每5秒触发一次,并且执行有状态的工作,有时需要5秒钟以上的时间(例如7秒钟),这与我现在拥有的

What I'm trying to achieve: I have a secondly trigger that fires every 5 secods, and stateful job, that takes sometimes more than 5 seconds (7 for example) and what I have now

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.
工作:

I have tried quartz.net version 2 and 1.
Job:

    [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(),但是由于调度程序被关闭,或者因为没有可用的线程,所以发生失火,在我的情况下-作业不执行,因为我使用StatefulJob.也许我错了,但是行为保持不变.

EDIT I have tried to use WithMisfireHandlingInstructionIgnoreMisfires(), but missfires happens due to scheduler being shutdown, or because there are no available threads, In my case - jobs does not execute because I use StatefulJob. Maybe I'm wrong, but behavior stays the same.

EDIT2 好的,带有"running"标志的解决方案在单线程应用程序中可以完美地工作.但是,如果我在几个线程(具有不同的参数)中运行此作业,它将无法正常工作 因此,是否有可能实现我想要使用石英的行为?

EDIT2 Ok, solution with 'running' flag works perfect in single threaded app. But if I runs this job in few threads (with different params) it would not work So, is it possible to achieve behavior like I want using quartz ?

推荐答案

如果让您的作业同时运行怎么办,但是如果该作业已经在运行,则对其进行任何修改,例如修改.

What if you let your job run concurrently, but amend it to do nothing if the job is already running, eg something like.

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;
        }

    }            
}

这篇关于如何创建石英作业,即使作业花费更多时间,该作业也将每N秒运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆