Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jobs not chaining using JobChainingJobListener

Tags:

c#

quartz.net

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.

like image 733
user2424607 Avatar asked Dec 26 '22 07:12

user2424607


1 Answers

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.

like image 88
Marko Lahma Avatar answered Jan 05 '23 19:01

Marko Lahma