質問

Quartz.Netには非常に頻繁にトリガーするジョブがあり、時には長時間実行されますが、ジョブが既に実行されている場合、トリガーをキャンセルするにはどうすればよいですか?

役に立ちましたか?

解決

より標準的な方法は、IInterruptableJobを使用することです。 http://quartznet.sourceforge.netを参照してください。 /faq.html#howtostopjob 。もちろん、これはif(!jobRunning)...

他のヒント

ジョブの開始時に何らかのグローバル変数(jobRunning = true)を設定し、終了したらfalseに戻すだけではどうですか?

トリガーが起動したら、if(jobRunning == false)のコードを実行するだけです

アプリは、起動時にジョブリストから自分自身を削除し、シャットダウン時に自分自身を挿入できます。

最近では、「WithMisfireHandlingInstructionIgnoreMisfires」を使用できます。トリガーで、ジョブで[DisallowConcurrentExecution]属性を使用します。

これは私の実装でした(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()));
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top