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;
currentCoun开发者_开发知识库t++;
context.Scheduler.TriggerJob(jobName, "jobGroup");
}
}
} }
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.
精彩评论