我想知道是否可以使用每周触发器触发一次,然后转到每周计划
Trigger trigger = TriggerUtils.MakeWeeklyTrigger(
"TriggerName",
dayOfweek,
timeOfDay.Hours,
timeOfDay.Minutes);
所以我基本上想要当我的服务第一次启动我的触发器来触发并运行它的工作时。之后,我希望它能够根据用户设置每周工作一次(因此,可能在首次运行后将用户设置为星期一早上5点)。
答案 0 :(得分:3)
您可以将多个触发器与一个作业相关联。一个人会立即开火,只开一次,另一个人每周开火:
var jobDetail = new JobDetail("MyJob", "MyGroup", typeof(T));
// run in ten seconds, only once.
var once = new SimpleTrigger("Once",
null,
DateTime.UtcNow.AddSeconds(10),
null,
0,
TimeSpan.Zero);
// run weekly, start in 5 minutes to give 'once' trigger some time to run
Trigger weekly = TriggerUtils.MakeWeeklyTrigger(
"Weekly",
dayOfweek,
timeOfDay.Hours,
timeOfDay.Minutes);
weekly.StartTimeUtc = DateTime.UtcNow.Add(TimeSpan.FromMinutes(5));
once.JobName = jobDetail.Name;
weekly.JobGroup = jobDetail.Group;
once.JobName = jobDetail.Name;
weekly.JobGroup = jobDetail.Group;
_quartzScheduler.ScheduleJob(jobDetail, once);
_quartzScheduler.ScheduleJob(weekly);
或者您可以安排立即触发以及何时触发每周重新安排:
private static volatile Boolean firstRun = false;
public void Execute(JobExecutionContext context) {
if(firstRun) {
Trigger weekly = TriggerUtils.MakeWeeklyTrigger(
"Weekly",
dayOfweek,
timeOfDay.Hours,
timeOfDay.Minutes);
context.Scheduler.RescheduleJob("Once", "MyGroup", weekly);
firstRun = false;
}
}
另一种选择是在启动时运行代码而不涉及Quartz。只需使用ThreadPool。然后让Quartz只处理每周计划。如果您的代码不依赖于Quartz,这可能是一个好主意。