Jobs not chaining using JobChainingJobListener

不羁的心 提交于 2019-11-28 01:28:15

问题


I have the current code for my Quartz scheduler:

var scheduler = StdSchedulerFactory.GetDefaultScheduler();

// Job1
var Job1 = JobBuilder.Create<Test1>().WithIdentity("job1", "group1").Build();
// Job2
var Job2 = JobBuilder.Create<Test2>().WithIdentity("job2", "group2").Build();

// Triggers
ITrigger trigger1 = TriggerBuilder.Create().WithIdentity("trigger1", "group1").StartNow().Build()
ITrigger trigger2 = TriggerBuilder.Create().WithIdentity("trigger2", "group2").StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(1).WithRepeatCount(4)).Build();

// JobKeys
JobKey jobKey1 = new JobKey("Job1", "group1");
JobKey jobKey2 = new JobKey("Job2", "group2");

// Chain jobs
JobChainingJobListener chain = new JobChainingJobListener("testChain");
chain.AddJobChainLink(jobKey1, jobKey2);
scheduler.ScheduleJob(Job1, trigger1);
scheduler.AddJob(Job2, true);

// Global listener here. I am not sure what I have is correct.
scheduler.ListenerManager.AddJobListener(chain, GroupMatcher<JobKey>.AnyGroup());` 

scheduler.Start();

(For clarification, the jobs do nothing more than print to console at the moment.)

From the Quartz website, I found that this will add a JobListener that is interested in all jobs: scheduler.ListenerManager.AddJobListener(chain, GroupMatcher<JobKey>.AnyGroup()); I'm not sure that this is equivalent to a global listener.

I also found that some code where people have done scheduler.addGlobalJobListener(chain); in Java. Is there an equivalent method in c#?

My code compiles and seems to run without errors, but Job2 does not trigger. Job1 prints properly to console.


回答1:


The issue here is that you have misspelled the key the second time ("Job1" vs "job1") which causes there to be no known link to fire. Here's updated code sample with redundancies removed.

var scheduler = StdSchedulerFactory.GetDefaultScheduler();
JobKey jobKey1 = new JobKey("job1", "group1");
JobKey jobKey2 = new JobKey("job2", "group2");

var job1 = JobBuilder.Create<Test1>().WithIdentity(jobKey1).Build();
var job2 = JobBuilder.Create<Test2>().WithIdentity(jobKey2).StoreDurably(true).Build();

ITrigger trigger1 = TriggerBuilder.Create()
   .WithIdentity("trigger1", "group1")
   .StartNow()
   .Build();

JobChainingJobListener chain = new JobChainingJobListener("testChain");
chain.AddJobChainLink(jobKey1, jobKey2);
scheduler.ListenerManager.AddJobListener(chain, GroupMatcher<JobKey>.AnyGroup());

scheduler.ScheduleJob(job1, trigger1);
scheduler.AddJob(job2, true);

scheduler.Start();

The scheduler.addGlobalJobListener is old API and longer part of 2.x series. You should use the ListenerManager like you have done.



来源:https://stackoverflow.com/questions/24515470/jobs-not-chaining-using-jobchainingjoblistener

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!