Quartz.net 动态添加、修改任务运行周期 弥补错误策略
基础Demo
通过反射的方式动态添加Job、Trigger
Assembly assembly = Assembly.LoadFrom("QuartzFramework.exe");
Type type= assembly.GetType("QuartzFramework.SecondJob");
IJobDetail jobDetail = JobBuilder.Create(type).WithIdentity(type.Name, "动态Job").Build();
Action<CronScheduleBuilder> action = x => { x.WithMisfireHandlingInstructionDoNothing(); };
ITrigger trigger = TriggerBuilder.Create().WithIdentity($"{type.Name}Trigger", "动态Job")
.StartNow()
.WithCronSchedule("0/1 * * * * ? ", action)
.WithDescription("动态Trigger")
.Build();
scheduler.ScheduleJob(jobDetail, trigger);
//暂停、恢复触发器
if (scheduler.GetTriggerState(data[0]).GetAwaiter().GetResult() == TriggerState.Paused)
{
ICronTrigger cronTrigger = (ICronTrigger)scheduler.GetTrigger(data[0]).GetAwaiter().GetResult();
ITrigger trigger = cronTrigger.GetTriggerBuilder().WithCronSchedule("0/2 * * * * ? ", x => x.WithMisfireHandlingInstructionDoNothing()).Build();
scheduler.RescheduleJob(trigger.Key, trigger);
//scheduler.ResumeTrigger(data[0]);
}
else
{
scheduler.PauseTrigger(data[0]);
}
简单Demo运行后可以正常操作!