Quartz .NET 不触发触发器,为什么?
我在 .NET 应用程序中使用 Quartz。起初,我是在Windows服务中使用它,但它不起作用,所以我将其移至普通项目中进行测试。这是 Main 中的代码:
ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler sched = schedFact.GetScheduler();
JobDetail jobDetail = new JobDetail("JobPrueba", null, typeof(JobPrueba));
Trigger trigger = TriggerUtils.MakeMinutelyTrigger(1, 3);
trigger.StartTimeUtc = DateTime.Now;
trigger.Name = "TriggerPrueba";
sched.Start();
sched.ScheduleJob(jobDetail, trigger);
这是 JobPrueba:
class JobPrueba : IStatefulJob
{
public JobPrueba() { }
public void Execute(JobExecutionContext context)
{
const string fic = @"C:\prueba.txt";
string texto = DateTime.Now.ToString();
System.IO.StreamWriter sw = new System.IO.StreamWriter(fic, true);
sw.WriteLine(texto);
sw.Close();
System.Console.WriteLine("Hello world");
}
}
它根本没有做任何事情,当执行 Main 中的最后一行时,程序永远不会结束,但它不会写入文件,也不会在控制台中打印 Hello World。
有谁知道我做错了什么?
I'm using Quartz in my .NET application. At first, I was using it in a windows service but it didn't work, so I moved it to a normal project to test it. This is the code in Main:
ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler sched = schedFact.GetScheduler();
JobDetail jobDetail = new JobDetail("JobPrueba", null, typeof(JobPrueba));
Trigger trigger = TriggerUtils.MakeMinutelyTrigger(1, 3);
trigger.StartTimeUtc = DateTime.Now;
trigger.Name = "TriggerPrueba";
sched.Start();
sched.ScheduleJob(jobDetail, trigger);
and this is JobPrueba:
class JobPrueba : IStatefulJob
{
public JobPrueba() { }
public void Execute(JobExecutionContext context)
{
const string fic = @"C:\prueba.txt";
string texto = DateTime.Now.ToString();
System.IO.StreamWriter sw = new System.IO.StreamWriter(fic, true);
sw.WriteLine(texto);
sw.Close();
System.Console.WriteLine("Hello world");
}
}
It's not doing anything at all, when last line in Main is executed, the program never ends, but it doesn't write in file nor print Hello World in console.
Does anyone know what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现这是时代的问题。我住在一个时间为 UTC + 2 的国家/地区,因此当我将触发器的 StartTimeUtc 设置为 DateTime.Now 而不是 UtcNow 时,触发器直到两个小时后才需要触发,我认为它必须触发就在代码被执行的那一刻。
此外,我设置了一个计时器来记录发生的情况,并在该日志中打印了当前时间(使用 DateTime.Now)和触发器的 StartTimeUtc,显然它们是相同的,并且后来的 DateTime.Now 大于 StartTimeUtc 。如果我打印了 DateTime.UtcNow 我就会看到这个问题。
I've discovered that it's a problem of times. I live in a country which time is UTC + 2, so when I set the StartTimeUtc of the trigger to DateTime.Now, instead of UtcNow, the trigger didn't have to fire until two hours later, and I thought it has to fire in the very moment the code was executed.
Besides, I set a timer in order to log what was happening, and in that log I printed current time (with DateTime.Now) and the StartTimeUtc of the trigger, and obviously they were the same and later DateTime.Now was greater than StartTimeUtc. If I had printed DateTime.UtcNow I had seen the problem.