How to reschedule Quartz job scheduler in C# dynamically from database?

丶灬走出姿态 提交于 2019-12-01 06:43:24

问题


I have included Quartz.net library in my C#.Net website to send emails automatically based on some conditions. I have started the job to with RepeatForever(). I am getting the settings for the scheduler from database. Now when the job has already started is there anyway to check if the settings for the scheduler in the database have changed so i should also update them in my method so that scheduler runs with the new settings dynamically? So far I have done this:

public void StartJob()
    {
        var reminderSettings = GetReminderSettings();  //getting these settings from database
        var count = GetUnpaidInvoicesListCount();
        var intervalBtwnReminders = 0;
        if (reminderSettings?.RemindersGap != null)
        {
            intervalBtwnReminders = (int)(reminderSettings.RemindersGap);         
        }

        ISchedulerFactory schedFact = new StdSchedulerFactory();

        IScheduler sched = schedFact.GetScheduler();
        sched.Start();

        // create job
        IJobDetail job = JobBuilder.Create<EmailJob>()
                .WithIdentity("job1", "group1")
                .Build();

        // create trigger
        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("trigger1", "group1")
            .WithSimpleSchedule(x =>
            {
                 x.WithIntervalInMinutes(intervalBtwnReminders ).RepeatForever();
            })
            .Build();

        sched.ScheduleJob(job, trigger);
    }

回答1:


I wrote a set of methods to make it more easy. You can't edit an existing trigger with your new interval, but you can replace your existing trigger with a new one. The CreateTrigger method creates a new Trigger based on a old one and transfers the TimesTriggered count.

/// <summary>
/// Create a new trigger based on a existing trigger.
/// </summary>
/// <param name="oldTrigger">the existing trigger</param>
/// <param name="startNow">indicates if the trigger should start immediately after schedule</param>
/// <param name="interval">the interval for the new trigger</param>
/// <returns>Returns the new trigger</returns>
public ITrigger CreateTrigger(ITrigger oldTrigger, bool startNow = false, TimeSpan? interval = null)
{
    var builder = oldTrigger.GetTriggerBuilder();
    if (startNow)
        builder = builder.StartNow();
    if (interval.HasValue)
        builder = builder.WithSimpleSchedule(s => s
            .WithInterval(interval.Value)
            .RepeatForever());

    var newTrigger = builder.Build();
    var simpleTrigger = newTrigger as ISimpleTrigger;
    if(simpleTrigger != null)
    {
        var trigger = oldTrigger as ISimpleTrigger;
        if (trigger != null)
            simpleTrigger.TimesTriggered = trigger.TimesTriggered;
    }

    return newTrigger;
}

How to use it

In this example, the Scheduler is already running.

// get the trigger
var trigger = Scheduler.GetTrigger(new TriggerKey("trigger1", "group1"));

// get your settings and set the interval
var reminderSettings = GetReminderSettings();
var intervalBtwnReminders = 0;
if (reminderSettings?.RemindersGap != null)
{
    intervalBtwnReminders = (int)(reminderSettings.RemindersGap);         
}

// reschedule the job with a new trigger and start it immediately.
// if you don't want that it starts now, pass 'false' for the 'startNow' parameter
Scheduler.RescheduleJob(trigger.Key, CreateTrigger(trigger, true, TimeSpan.FromMinutes(intervalBtwnReminders)));


来源:https://stackoverflow.com/questions/41589865/how-to-reschedule-quartz-job-scheduler-in-c-sharp-dynamically-from-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!