实际上可以使用触发组来执行此操作。您只需要暂停一个特定的触发器组,然后创建一个触发器属于该组的作业。
// pauses the group, if it isn't already
scheduler.PauseTriggers(GroupMatcher<TriggerKey>.GroupEquals("PausedGroup"));
var job = JobBuilder.Create<MyJob>().WithIdentity(jobName).Build();
var trigger = TriggerBuilder.Create()
.ForJob(job)
.WithIdentity(Guid.NewGuid().ToString(), "PausedGroup")
.WithSimpleSchedule(schedule => schedule.WithIntervalInMinutes(1).RepeatForever())
.Build();
scheduler.ScheduleJob(job, trigger);
然后要恢复作业,请使用不同的组重新安排每个触发器:
foreach (var oldTrigger in scheduler.GetTriggers(new JobKey(jobName)))
{
// clone the old trigger with new group name
var newTrigger = oldTrigger.GetTriggerBuilder()
.WithIdentity(oldTrigger.Key.Name) // uses default trigger group name
.Build();
// not a typo, the method to reschedule a trigger is called "RescheduleJob" for some reason
scheduler.RescheduleJob(oldTrigger.Key, newTrigger);
}