如何使Quartz.NET进程同步?
我有一个预定的工作,每 5 分钟重复一次。运行良好。
但我遇到的情况是,我的第一份工作没有在 5 分钟内完成,而第二份工作正在开始(按计划 5 分钟)。
我不想这样做,一次应该只执行一项工作。我怎样才能做到这一点?
这是我的代码:
ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler sched = schedFact.GetScheduler();
Trigger emailTrigger = TriggerUtils.MakeMinutelyTrigger(5);
emailTrigger.StartTimeUtc = TriggerUtils.GetEvenMinuteDate(DateTime.UtcNow);
emailTrigger.Name = "EmailTrigger";
JobDetail emailJobDetail = new JobDetail("EmailJob", null, typeof(EmailJob));
sched.ScheduleJob(emailJobDetail, emailTrigger);
sched.Start();
I have a scheduled job that have repeat interval for every 5 mins. It's working fine.
But I have a situation in where my first job is not completing in 5 mins and a second job is starting (as it scheduled for 5 mins).
I don't want to do that, only one job should be working at a time. How can I do that?
This is my code:
ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler sched = schedFact.GetScheduler();
Trigger emailTrigger = TriggerUtils.MakeMinutelyTrigger(5);
emailTrigger.StartTimeUtc = TriggerUtils.GetEvenMinuteDate(DateTime.UtcNow);
emailTrigger.Name = "EmailTrigger";
JobDetail emailJobDetail = new JobDetail("EmailJob", null, typeof(EmailJob));
sched.ScheduleJob(emailJobDetail, emailTrigger);
sched.Start();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Quartz.NET 2.x
实现
IJob
并使用[DisallowConcurrentExecution]
属性装饰您的作业类。请阅读DisallowConcurrentExecutionAttribute
的 API 文档以获取更多信息。Quartz.NET 1.x
让作业类实现
IStatefulJob
而不是IJob
。阅读IStatefulJob
的 API 文档了解更多信息。http://www.quartz-scheduler.net/documentation/faq.html#howtopreventconcurrentfire
Quartz.NET 2.x
Implement
IJob
and also decorate your job class with[DisallowConcurrentExecution]
attribute. Read the API documentation forDisallowConcurrentExecutionAttribute
for more information.Quartz.NET 1.x
Make the job class implement
IStatefulJob
rather thanIJob
. Read the API documentation forIStatefulJob
for more information.http://www.quartz-scheduler.net/documentation/faq.html#howtopreventconcurrentfire
我使用过 Quartz.net 一点,但从未研究过在作业之间强制执行串行处理。根据我的经验,Quartz 更适合“并行”计划处理,如果运行时间较长,作业可能会重叠,所以我不确定它是否支持您所需要的。
解决问题的一个简单方法可能是使用可由任何作业线程访问的同步变量(例如锁、互斥体、信号量或全局布尔值等)。当一个新作业开始时,它应该检查锁,如果它是空闲的,抓住它,并保持它直到它完成。如果另一个作业醒来,并且发现前一个作业仍在运行,则新作业可以直接退出,并等待调度程序在下一个时间间隔再次尝试。您也可以让新作业等待前一个作业完成,但如果这样做,您将面临作业堆积等待执行的风险,并且系统永远不会“赶上”。
I've used Quartz.net a bit, but never investigated enforcing serial processing between jobs. From my experience, Quartz is more intended for "parallel" scheduled processing, where jobs can overlap if they run long, so I'm not sure if it supports what you need.
A simple solution to your problem might be to use a synchronization variable that can be accessed by any of the job threads (e.g. a lock, mutex, semaphore, or global boolean, etc.). When a new job starts up, it should check the lock, and if it's free, grab it, and hold it until it's finished. If another job wakes up, and sees that a previous job is still running, the new job can just exit, and wait for the scheduler to try again on the next interval. You could also have the new job wait for the previous to finish, but if you do that, you run the risk of jobs piling up waiting to execute, and the system never "catching up."