Quartz.NET - 在作业完成完全执行后调用函数 [英] Quartz.NET - Call a function after the Job has completed Full Execution
问题描述
我们正在使用 Quartz.Net 在 Windows 服务中按计划触发作业.我有一种情况,我必须从 Start DateTime
到 End DateTime
每 5 分钟
触发一次作业.
We are using Quartz.Net to trigger jobs on a schedule in a Windows Service
.
I have a situation where I have to Trigger a job every 5 minutes
from Start DateTime
till End DateTime
.
作业完成后,我们需要计算Next Start DateTime
和Next End DateTime
并保存到DB -
After the job is completed we need to calculate the Next Start DateTime
and Next End DateTime
and save to the DB -
为此,我尝试覆盖具有方法的 JobListener
:JobWasExecuted
For this I tried to override the JobListener
which has a method: JobWasExecuted
public class xPTJobListener : JobListenerSupport
{
public override string Name { get { return "xPTJobListener"; } }
public override void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
{
var dataMap = context.MergedJobDataMap;
var schedule = (MyDTO)dataMap["Schedule"];
using (var logger = new xPTLogger())
{
logger.LogMessage(MessageType.Information, string.Format("Inside JobWasExecuted() - [{0}] - ", schedule.Id));
}
base.JobWasExecuted(context, jobException);
}
}
以及TriggerListener
public class xPTTriggerListener : TriggerListenerSupport
{
public override string Name { get { return "xPTTriggerListener"; } }
public override void TriggerComplete(ITrigger trigger, IJobExecutionContext context, SchedulerInstruction triggerInstructionCode)
{
var dataMap = context.MergedJobDataMap;
var schedule = (MyDTO)dataMap["Schedule"];
using (var logger = new xPTLogger())
{
logger.LogMessage(MessageType.Information, string.Format("Inside Trigger Complete - [{0}] - ", schedule.Id));
}
base.TriggerComplete(trigger, context, triggerInstructionCode);
}
}
但是上述方法的问题是每次调用作业时都会执行它们.
But the problem with above methods is that they are both executed each time the job is called.
因此,如果我有一个从 12:01 AM
开始运行并每 5 秒在 12:02 AM
结束的作业 - 这两种方法都会被调用 12 次
So if I have a Job which runs from 12:01 AM
and ends 12:02 AM
every 5 seconds - Both these methods get called 12 times
我需要的是在 One Job 迭代结束后只调用一次方法 - (在 Job 执行 12 次之后)?
What I need is to call a method only once after the One Job iteration has Ended - (After the Job is executed 12 Times)?
如何在 Quartz 中执行此操作?
How do I do this in Quartz?
编辑
创建触发器
public static ITrigger GenerateTrigger(RouteMonitorScheduleDTO routeSchedule, double fGmtOffset, xPTLogger logger)
{
ITrigger trigger = null;
switch (routeSchedule.ScheduleInfo.PeriodType)
{
case PeriodTypeEnum.Once:
trigger = TriggerBuilder.Create()
.WithIdentity(string.Format("trigger_{0}", routeSchedule.RouteScheduleId), DefaultGroup)
.StartAt(routeSchedule.DepartureDateTime)
.WithSimpleSchedule(s => s.WithIntervalInMinutes(5))
.EndAt(routeSchedule.ArrivalDateTime.AddMinutes(5))
.Build();
break;
case PeriodTypeEnum.Daily:
case PeriodTypeEnum.WeekDays:
case PeriodTypeEnum.Weekly:
case PeriodTypeEnum.Monthly:
var schedule = routeSchedule.ScheduleInfo;
var cronExpresion = xPTCronBuilder.GenerateCronExpression(
schedule.PeriodType,
schedule.ScheduleStringValue,
fGmtOffset,
routeSchedule.DepartureDateTime,
routeSchedule.ArrivalDateTime.AddMinutes(5), 5);
trigger = TriggerBuilder.Create()
.WithIdentity(string.Format("trigger_{0}", routeSchedule.RouteScheduleId), DefaultGroup)
.WithCronSchedule(cronExpresion)
.Build();
break;
}
return trigger;
}
编辑使用 Cron 触发:
EDIT Trigger with Cron:
trigger = TriggerBuilder.Create()
.WithIdentity(string.Format("trigger_{0}", 1), "Group1")
.WithCronSchedule("0 0-45/5 7-7 ? * TUE,WED *").Build();
从上面的 cron 表达式可以看出,它将在 7 AM 到 7:45 AM
每 5 分钟
每 Tuesday
和 <代码>周三代码>.
As you can see from above cron expression it will run from 7 AM to 7:45 AM
every 5 minutes
every Tuesday
and Wednesday
.
所以 1 次迭代是 Tuesday
的 7AM to 7:45 AM
,下一次是 的
.我需要在每次迭代完成后调用一个函数.7 AM to 7:45
周三
So 1 iteration is 7AM to 7:45 AM
on Tuesday
, the Next is 7 AM to 7:45
on Wednesday
. I need to call a function after each Iteration has completed.
因此,假设在 周二上午 7:45
触发 Last Trigger 时 - 我需要调用该函数.
So let's say that when the Last Trigger is fired for 7:45 AM on Tuesday
- I need to call the function.
推荐答案
如果我正确理解您的问题,您可以通过以下方式使用 ITriggerListener 执行此操作:
If I understand your problem correctly, you can do this using ITriggerListener in the following way:
public class xPTTriggerListener : TriggerListenerSupport {
public override string Name
{
get { return "xPTTriggerListener"; }
}
public override void TriggerComplete(ITrigger trigger, IJobExecutionContext context, SchedulerInstruction triggerInstructionCode) {
if (triggerInstructionCode == SchedulerInstruction.DeleteTrigger) {
// means this trigger won't be fired again - now recalculate your dates in database
}
base.TriggerComplete(trigger, context, triggerInstructionCode);
}
}
这篇关于Quartz.NET - 在作业完成完全执行后调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!