Frage

Ich habe einen Job in Quartz.Net, die recht häufig auslöst, und manchmal für lange läuft, wie löse ich den Auslöser, wenn der Auftrag bereits ausgeführt wird?

War es hilfreich?

Lösung

Je mehr Standardmethode ist IInterruptableJob zu verwenden, finden Sie unter http://quartznet.sourceforge.net /faq.html#howtostopjob . Natürlich ist dies nur eine andere Art zu sagen, wenn (! JobRunning) ...

Andere Tipps

Könnten Sie nicht nur eine Art globale Variable (jobRunning = true), wenn die Arbeit beginnt und es zu falschen zurückkehren, sobald es fertig ist?

Dann, wenn der Trigger ausgelöst, nur der Code ausgeführt werden, wenn (jobRunning == false)

Ihre App konnte sich beim Start aus der Jobliste entfernen und sich einfügen beim Herunterfahren.

Heute könnte man "WithMisfireHandlingInstructionIgnoreMisfires" in Ihrem Trigger verwenden und [DisallowConcurrentExecution] Attribut in Ihrem Job.

Dies war meine Implementierung (die Vorschläge auf den Link verwenden, die Markol gab früher).

Ich versuche nur etwas Tipp zu speichern.

Ich bin ziemlich neu bei Quartz.NET, so mit einem Zuge von Salz die unten nehmen.

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()));
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top