Quartz.Net 中长时间运行的作业

发布于 2024-08-04 13:22:23 字数 68 浏览 5 评论 0原文

我在 Quartz.Net 中有一个工作,它触发得非常频繁,有时会运行很长时间,如果该工作已经在运行,我该如何取消触发器?

I've a job in Quartz.Net which triggers quite frequently, and sometimes runs for long, how do i cancel the trigger if the job is already running?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痕至 2024-08-11 13:22:23

更标准的方法是使用 IInterruptableJob,请参阅 http://quartznet.sourceforge.net/faq。 html#howtostopjob 。当然,这只是 if (!jobRunning)... 的另一种说法。

The more standard way is to use IInterruptableJob, see http://quartznet.sourceforge.net/faq.html#howtostopjob . Of course this is just another way to say if (!jobRunning)...

嗫嚅 2024-08-11 13:22:23

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

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

Could you not just set some sort of global variable (jobRunning=true) when the job starts and revert it to false once it's finished?

Then when the trigger fires, just run your code if(jobRunning==false)

温柔嚣张 2024-08-11 13:22:23

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

Nowadays you could use "WithMisfireHandlingInstructionIgnoreMisfires" in your trigger and use [DisallowConcurrentExecution] attribute in your job.

孤城病女 2024-08-11 13:22:23

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

Your app could remove itself from the job list on startup and insert itself on shutdown.

黑白记忆 2024-08-11 13:22:23

这是我的实现(使用 MarkoL 之前给出的链接中的建议)。

我只是想节省一些打字时间。

我是 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()));
    }
}

This was my implementation (using the suggestions at the link that MarkoL gave earlier).

I'm just trying to save some typing.

I'm pretty new at Quartz.NET, so take the below with a train of salt.

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()));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文