今天遇到一个问题,调度器在启动时无法立刻开始执行任务,代码如下:
var trigger = TriggerBuilder.Create() .StartNow() //此处无效 .WithCronSchedule("0 0/30 10-20 * * ?") .Build();
F12 可以看到 StartNow() 注释如下:
//// 摘要:// Set the time the Trigger should start at to the current moment - the trigger// may or may not fire at this time - depending upon the schedule configured// for the Trigger.//// 返回结果:// the updated TriggerBuilderpublic TriggerBuilder StartNow();
在 stockoverflow 上查找结果如下
地址:http://stackoverflow.com/questions/19219560/quartz-trigger-builder-startnow-not-firing-the-trigger-during-the-start
由上述得知,当使用 Cron 表达式时,StartNow 方法不会起任何效果,Cron 有其自己的执行时间。目前看来 StartNow 应该只适用于 SimpleTrigger 触发器。
解决方法:
1.如上图,增加一个仅有 StartNow 的触发器来立刻触发 Job
2.可在scheduler.Start() 启动前手动调用执行 Job 一次
新闻热点
疑难解答