在 TriggerComplete 事件中运行石英作业
我的程序应该一次运行最多 (N) 个作业。如果需要运行更多作业,则将其存储在临时存储中,并在完成当前正在运行的作业之一后,我将根据触发器无法启动的程度及其优先级来选择另一个触发器,然后
在以下位置 触发其作业初始化阶段,我创建了例如 5 个具有 5 个相应触发器的作业,并将其添加到调度程序中,直到第二个作业运行时一切都很好,但是触发器侦听器的 TriggerComplete 没有触发以拾取另一个要运行的作业,您能告诉我哪里错了吗?? <代码>
public class CrawlerTriggerListener : ITriggerListener
{
private int maxConcurrentCrawling = 1;
private int currentCount = 0;
private object syncLock = new object();
private Dictionary fireDic = new Dictionary();
公共字符串名称{获取{返回“监听器”; } }
public void TriggerFired(触发器触发器,JobExecutionContext上下文)
{
if (fireDic.Count == 0)
{
IScheduler sched = context.Scheduler;
string[] triggerNameList = sched.GetTriggerNames("triggerGroup");
foreach(触发器名称列表中的字符串触发器名称)
{
MissfiredInfomissedInfo = new MissfiredInfo();
MissedInfo.TriggerName = 触发器名称;
MissedInfo.Priority = sched.GetTrigger(triggerName, "triggerGroup").Priority;
fireDic.Add(triggerName,missedInfo);
}
}
}
public bool VetoJobExecution(触发触发器,JobExecutionContext上下文)
{
锁(同步锁)
{
if (currentCount < maxConcurrentCrawling)
{
当前计数++;
fireDic[trigger.Name].FailCount = 0;
fireDic[trigger.Name].LastFireTime = DateTime.UtcNow;
返回假;
}
别的
{
fireDic[trigger.Name].LastFireTime = DateTime.UtcNow;
fireDic[trigger.Name].FailCount++;
返回真;
}
}
}
public void TriggerMisfired(触发器触发器){ }
public void TriggerComplete(触发器触发器,JobExecutionContext上下文,SchedulerInstruction触发器InstructionCode)
{
锁(同步锁)
{
当前计数--;
var validCandidate = new Dictionary<string, int>();
foreach (KeyValuePair<string, MissfiredInfo> fireDicItem in fireDic)
if (fireDicItem.Value.FailCount > 0)
validCandidate.Add(fireDicItem.Key, fireDicItem.Value.FailCount * 73 + fireDicItem.Value.Priority);
if (validCandidate.Count > 0)
{
var sorted = (from entry in validCandidate orderby entry.Value ascending select entry);
string triggerName = sorted.First().Key;
fireDic[triggerName].LastFireTime = DateTime.UtcNow;
fireDic[triggerName].FailCount = 0;
string jobName = context.Scheduler.GetTrigger(triggerName, "triggerGroup").JobName;
currentCount++;
context.Scheduler.TriggerJob(jobName, "jobGroup");
}
}
}
}
my program should run maximum (N) job at a time. if there is more job needs to be run it is store in temp storage and after completing one of the currently running job then i'll pick another trigger base on how much the trigger fails to start and it's priority, and then fire its job
at initialization phase, I create for example 5 job with 5 corresponding trigger and add it to scheduler everything's fine until second job is running but TriggerComplete of the trigger listener is not firing for picking up another job to run, could you please tell me where im wrong ??
public class CrawlerTriggerListener : ITriggerListener { private int maxConcurrentCrawling = 1; private int currentCount = 0; private object syncLock = new object(); private Dictionary fireDic = new Dictionary();public string Name { get { return "listener"; } }
public void TriggerFired(Trigger trigger, JobExecutionContext context) { if (fireDic.Count == 0) { IScheduler sched = context.Scheduler; string[] triggerNameList = sched.GetTriggerNames("triggerGroup"); foreach (string triggerName in triggerNameList) { MissfiredInfo missedInfo = new MissfiredInfo(); missedInfo.TriggerName = triggerName; missedInfo.Priority = sched.GetTrigger(triggerName, "triggerGroup").Priority; fireDic.Add(triggerName, missedInfo); } } } public bool VetoJobExecution(Trigger trigger, JobExecutionContext context) { lock (syncLock) { if (currentCount < maxConcurrentCrawling) { currentCount++; fireDic[trigger.Name].FailCount = 0; fireDic[trigger.Name].LastFireTime = DateTime.UtcNow; return false; } else { fireDic[trigger.Name].LastFireTime = DateTime.UtcNow; fireDic[trigger.Name].FailCount++; return true; } } } public void TriggerMisfired(Trigger trigger) { }
public void TriggerComplete(Trigger trigger, JobExecutionContext context, SchedulerInstruction triggerInstructionCode) { lock (syncLock) { currentCount--;
var validCandidate = new Dictionary<string, int>(); foreach (KeyValuePair<string, MissfiredInfo> fireDicItem in fireDic) if (fireDicItem.Value.FailCount > 0) validCandidate.Add(fireDicItem.Key, fireDicItem.Value.FailCount * 73 + fireDicItem.Value.Priority); if (validCandidate.Count > 0) { var sorted = (from entry in validCandidate orderby entry.Value ascending select entry); string triggerName = sorted.First().Key; fireDic[triggerName].LastFireTime = DateTime.UtcNow; fireDic[triggerName].FailCount = 0; string jobName = context.Scheduler.GetTrigger(triggerName, "triggerGroup").JobName; currentCount++; context.Scheduler.TriggerJob(jobName, "jobGroup"); } }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,再说一次,我不确定您在哪里实例化 TriggerListener,但您可能想验证是否将 TriggerListener 添加到 Scheduler。
http://quartznet.sourceforge.net/tutorial/lesson_7.html
查看调度程序实例有一个“添加”(或注册)侦听器的方法。如果您不这样做,事件将永远不会触发。
Okay, so again, I'm not sure where you are instantiating the TriggerListener, but you might want to verify that you are adding the TriggerListener to the Scheduler.
http://quartznet.sourceforge.net/tutorial/lesson_7.html
See that the scheduler instance has a method for "adding" (or registering) listeners. If you don't do that, the events will never fire.