我最近开始使用 Quartz.NET,到目前为止,它真的很有帮助。现在,我正在尝试使用它来创建一个使用 NthIncludedDayTrigger 每月运行一次的作业(我想使用 NthIncludedDayTrigger,因为最终我将指定一个日历来排除周末/节假日)。
为了熟悉代码,我设置了一个简单的控制台应用程序来创建一个 NthIncludedDayTrigger ,其中第一次触发时间将是从现在开始的 15 秒:
static void Main(string[] args)
{
IScheduler scheduler = StdSchedulerFactory.DefaultScheduler;
scheduler.Start();
var jobDetail = new JobDetail("Job name", "Group name", typeof(SomeIJobImplementation));
var trigger = new NthIncludedDayTrigger();
trigger.Name = "Trigger name";
trigger.MisfireInstruction = MisfireInstruction.NthIncludedDayTrigger.DoNothing;
trigger.IntervalType = NthIncludedDayTrigger.IntervalTypeMonthly;
//I'm using the following while experimenting with the code (AddHour(1) to account for BST):
trigger.FireAtTime = DateTime.UtcNow.AddHours(1).AddSeconds(15).ToString("HH:mm:ss");
//I'm using the following while experimenting with the code:
trigger.N = DateTime.Today.Day;
Console.WriteLine("Started, press any key to stop ...");
Console.ReadKey();
scheduler.Shutdown(false);
}
...
public class SomeIJobImplementation : IJob
{
public void Execute(JobExecutionContext context)
{
Logger.Write(String.Format(
"Job executed called at {0}",
DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss")), null, 1,
TraceEventType.Information);
}
}
运行此程序会导致作业在一分钟内多次执行(大约每秒一次)。我正在使用 ADO.NET 作业存储,并且可以在我的数据库中看到 QRTZ_TRIGGERS.NEXT_FIRE_TIME 设置为上次执行时间,即似乎没有计划再次运行。
我希望上面的代码运行一次作业(大约 15 秒后),然后安排作业在一个月内再次运行。
也许问题只是我在试验期间使用 Quartz.NET 的方式,或者我的期望是错误的?无论哪种方式,我都会非常感谢任何帮助/建议来解释我观察到的行为,以及我需要改变什么以获得我想要的行为。