我在我的 C#.Net 网站中包含了 Quartz.net 库,以根据某些条件自动发送电子邮件。我已经开始使用 RepeatForever() 进行工作。我正在从数据库获取调度程序的设置。现在,当作业已经开始时,无论如何要检查数据库中调度程序的设置是否已更改,因此我还应该在我的方法中更新它们,以便调度程序以新设置动态运行?到目前为止,我已经这样做了:

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);
    }

最佳答案

我写了一组方法来使它更容易。
您无法使用新间隔编辑现有触发器,但可以用新触发器替换现有触发器。CreateTrigger 方法基于旧触发器创建新触发器并传输 TimesTriggered 计数。

/// <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;
}

如何使用它

在此示例中,调度程序已在运行。
// 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)));

关于c# - 如何从数据库动态重新安排 C# 中的 Quartz 作业调度程序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41589865/

10-11 01:48