Pregunta

Tengo un trabajo en Quartz.Net que se activa con bastante frecuencia y, a veces, se ejecuta durante mucho tiempo, ¿cómo cancelo el activador si el trabajo ya se está ejecutando?

¿Fue útil?

Solución

La forma más estándar es usar IInterruptableJob, consulte http://quartznet.sourceforge.net /faq.html#howtostopjob . Por supuesto, esta es solo otra forma de decir si (! JobRunning) ...

Otros consejos

¿No podría simplemente establecer algún tipo de variable global (jobRunning = true) cuando se inicia el trabajo y revertirlo a falso una vez que haya terminado?

Luego, cuando se dispare el disparador, simplemente ejecute su código si (jobRunning == false)

Su aplicación podría eliminarse de la lista de trabajos en el inicio e insertarse en el cierre.

Hoy en día, se puede usar " WithMisfireHandlingInstructionIgnoreMisfires " en su activador y use el atributo [DisallowConcurrentExecution] en su trabajo.

Esta fue mi implementación (utilizando las sugerencias en el enlace que MarkoL proporcionó anteriormente).

Sólo intento guardar algo de escritura.

Soy bastante nuevo en Quartz.NET, así que toma la siguiente información con un tren de sal.

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()));
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top