本文介绍了在作业完成完全执行后调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们正在使用Quartz.Net在Windows服务中按计划触发作业。
我遇到从Start DateTime
到End DateTime
每隔5 minutes
必须触发作业的情况。
作业完成后,我们需要计算Next Start DateTime
和Next End DateTime
并保存到数据库-
为此,我尝试重写JobListener
,它有一个方法: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
中的TriggerComplete
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);
}
}
但上述方法的问题在于,每次调用作业时都会执行它们。
因此,如果我有一个作业,该作业从12:01 AM
运行并每隔5秒结束12:02 AM
,则这两个方法都被调用12 times
我需要的是在一个作业迭代结束后只调用一次方法-(在作业执行12次之后)?
如何在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的触发器:
trigger = TriggerBuilder.Create()
.WithIdentity(string.Format("trigger_{0}", 1), "Group1")
.WithCronSchedule("0 0-45/5 7-7 ? * TUE,WED *").Build();
从上面的cron表达式可以看到,它将从7 AM to 7:45 AM
每5 minutes
每Tuesday
和Wednesday
运行。
Tuesday
,下一次是7 AM to 7:45
,在Wednesday
。我需要在每次迭代完成后调用一个函数。假设当为7:45 AM on Tuesday
触发最后一个触发器时,我需要调用该函数。
推荐答案
如果我正确理解您的问题,您可以通过以下方式使用ITriggerListener完成此操作:
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);
}
}
这篇关于在作业完成完全执行后调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!