没有使用JobChainingJobListener链接的job

本文关键字:链接 job JobChainingJobListener | 更新日期: 2023-09-27 18:11:05

我有我的石英调度程序的当前代码:

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();

(澄清一下,目前这些作业除了打印控制台之外什么都不做。)

从Quartz网站上,我发现这将添加一个对所有工作感兴趣的JobListener: scheduler.ListenerManager.AddJobListener(chain, GroupMatcher<JobKey>.AnyGroup());我不确定这是否等同于一个全局侦听器。

我还发现有人在Java中做过scheduler.addGlobalJobListener(chain);的代码。c#中有等效的方法吗?

我的代码编译,似乎运行没有错误,但Job2没有触发。Job1正确打印到控制台

没有使用JobChainingJobListener链接的job

这里的问题是您第二次拼错了键("Job1" vs "Job1"),这导致没有已知的链接到fire。下面是更新后的代码示例,删除了冗余。

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();

scheduler.addGlobalJobListener为旧API,为2的较长部分。x系列。您应该像刚才那样使用ListenerManager