有没有人知道是否可以在Quartz.net中安排可变间隔的工作? 例如,我有一个10到20的间隔,我需要安排一个作业,每n秒执行一次,其中n是每次作业执行后刷新的10到20之间的随机数。
感谢。
答案 0 :(得分:0)
以这种方式为第一次火力建立你的触发器:
var triggerKey = new TriggerKey("simpleTrigger", "simpleTriggerGroup");
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey)
.StartNow()
.Build();
然后,在您的作业类的Execute方法中修改触发器:
public void Execute(IJobExecutionContext context)
{
Console.WriteLine(string.Format("{0} hello this is a test", DateTime.Now.ToString("r")));
var newInterval = new Random().Next(20, 30);
// retrieve the trigger
var oldTrigger = context.Scheduler.GetTrigger(new TriggerKey("simpleTrigger", "simpleTriggerGroup"));
// obtain a builder that would produce the trigger
var tb = oldTrigger.GetTriggerBuilder();
// update the schedule associated with the builder, and build the new trigger
var newTrigger = tb.StartAt(DateTime.Now.AddSeconds(newInterval)).Build();
context.Scheduler.RescheduleJob(oldTrigger.Key, newTrigger);
Console.WriteLine("Trigger fired... changed interval to {0}", newInterval);
}
作业将以不同的间隔执行。