当前位置:  开发笔记 > 编程语言 > 正文

如何从数据库动态重新安排C#中的Quartz作业调度程序?

如何解决《如何从数据库动态重新安排C#中的Quartz作业调度程序?》经验,为你挑选了1个好方法。



1> Rabban..:

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

/// 
/// Create a new trigger based on a existing trigger.
/// 
/// the existing trigger
/// indicates if the trigger should start immediately after schedule
/// the interval for the new trigger
/// Returns the new trigger
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;
}

如何使用它

在此示例中,Scheduler已在运行.

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

推荐阅读
携手相约幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有