Jobs not chaining using JobChainingJobListener - c#

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.

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.

Related

Define list of triggers to execute job

I would like to execute same job in different scheduled times.
Here is one sample which I wrote:
ISchedulerFactory schedFact = new StdSchedulerFactory();
var setoftrigs = new HashSet<ITrigger>();
for (int x=0; x<=2; x++)
{
setoftrigs.Add(TriggerBuilder.Create().WithIdentity("trigger" + x.ToString(), "group1").WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(21, 44+x)).Build());
}
// get a scheduler
IScheduler sched = schedFact.GetScheduler().GetAwaiter().GetResult();
sched.Start();
// create job
IJobDetail job = JobBuilder.Create<RunJob>()
.WithIdentity("job1", "group1")
.Build();
sched.ScheduleJob(job, setoftrigs, false);
The problem is that in certain time jobs is not executed.
But if I do on this way:
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(21, 49))
.Build();
sched.ScheduleJob(job, trigger);
...it works fine.
What I want to achieve is to have a list of triggers when job should be executed.
How to do that?

Misfired Simple triggers are executing Jobs in spite of Setting misfire policy to "IGNORE"

I am using Quartz in .Net.
Irrespective of "quartz.jobStore.misfireThreshold" i set and setting misfire policy to ignore, still i see job getting executed.
Ideally this should never happen.
I have initialized both JobListener and Trigger listeners for the same
Here is my snippet
var props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" },
{"quartz.threadPool.threadCount","1" },
{"quartz.jobStore.misfireThreshold","3000" }
};
var schedulerFactory = new StdSchedulerFactory(props);
// get a scheduler
var scheduler = await schedulerFactory.GetScheduler();
//scheduler.CheckExists()
await scheduler.Start();
var job3 = JobBuilder.Create<HelloJob>()
.WithIdentity("myJob3", "group3")
.UsingJobData("jobSays", "Hello World 333333")
.UsingJobData("myFloatValue", 9.423f)
.Build();
var trigger3 = TriggerBuilder.Create()
.WithIdentity("trigger3", "group3")
.WithSimpleSchedule(x => x
.WithMisfireHandlingInstructionIgnoreMisfires())
.StartAt(DateTimeOffset.UtcNow)
.Build();
await scheduler.ScheduleJob(job3, trigger3);
.WithMisfireHandlingInstructionIgnoreMisfires() fires all triggers that were missed as soon as possible and then goes back to ordinary schedule. You should use .WithMisfireHandlingInstructionDoNothing(), trigger with explicitly set "MISFIRE_INSTRUCTION_DO_NOTHING" misfire instruction handler, meaning that all misfired executions are discarded, it simply waits for next scheduled time. A misfire occurs if a trigger "misses" its firing time because there are no available threads in Quartz’s thread pool for executing the job

C#:How to schedule multiple routine by Quartz.net at different time of the day once [duplicate]

This question already has an answer here:
Multiple triggers of same Job Quartz.NET
(1 answer)
Closed 6 years ago.
i know how to fire my one routine every day at specific time of day. here is the code.
IScheduler sched = null;
//construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
//get a scheduler
sched = schedFact.GetScheduler();
sched.Start();
IJobDetail job = JobBuilder.Create<frmMain>()
.WithIdentity("Job", "group")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithDailyTimeIntervalSchedule
(s =>
s.WithIntervalInHours(24)
.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(19, 07))
)
.Build();
sched.ScheduleJob(job, trigger);
suppose now i am in scenario that i need to trigger many routine at different time of the day once.
say routine1 should fire at 08:00, routine2 should fire at 15:00 and routine2 should fire at 18:00
now give me suggestion how could i fire different routine at different time of the day. thanks
Like stuartd stated, you need multiple triggers for your job(routine2). I would also suggest to use CronTrigger instead of SimpleTrigger. You can easily create a CronTrigger with:
var trigger1 = TriggerBuilder.Create()
.WithDescription(name)
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(15, 0))
.Build();
var trigger2 = TriggerBuilder.Create()
.WithDescription(name)
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(18, 0))
.Build();
And then just schedule your job with the 2 triggers:
sched.ScheduleJob(job, trigger1);
sched.ScheduleJob(job, trigger2);

How to chain more than 2 jobs using JobChainingJobListener Quartz.net?

I want to chain the 3 jobs, but AddJobChainLink() gets only 2 jobKeys as parameter.
scheduler = container.GetInstance<IScheduler>();
scheduler.JobFactory = container.GetInstance<IJobFactory>(); JobKey jobkey1 = new JobKey("job1", "group1");
JobKey jobkey2 = new JobKey("job2", "group2");
JobKey jobkey3 = new JobKey("job3", "group3");
var job1 = JobBuilder.Create<Type1>().WithIdentity("job1", "group1").Build();
var job2 = JobBuilder.Create<Type2>().WithIdentity("job2", "group2").Build();
var job3 = JobBuilder.Create<Type3>().WithIdentity("job3", "group3").Build();
ITrigger trigger1 = TriggerBuilder.Create().WithIdentity("trigger1", "group1").StartNow().Build();
JobChainingJobListener chain = new JobChainingJobListener("testChain");
chain.AddJobChainLink(jobkey1, jobkey2);
scheduler.ScheduleJob(job1, trigger1);
scheduler.AddJob(job2, true);
scheduler.AddJob(job3, true);
scheduler.ListenerManager.AddJobListener(chain, GroupMatcher<JobKey>.AnyGroup());
scheduler.Start();
Try this:
JobChainingJobListener chain = new JobChainingJobListener("testChain");
chain.AddJobChainLink(jobkey1, jobkey2);
chain.AddJobChainLink(jobkey2, jobkey3);
JobChainingJobListener helps you create a chain of execution for your jobs in a specific order you desire. You just need to chain each job with another in specific order.

unscheduling job in quartz

I am using Quartz to schedule job in my c# .net application. I am storing all data in database. My code is :
ISchedulerFactory schedFact = new StdSchedulerFactory(properties);
_scheduler = schedFact.GetScheduler();
_scheduler.Start();
job = JobBuilder.Create<JobTask>()
.WithIdentity("job1", "group1")
.Build();
trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithSchedule(
CronScheduleBuilder.CronSchedule("0 0/5 * * * ?"))
.Build();
_scheduler.ScheduleJob(job, trigger);
Now I would like to give user function so user can disable(unschedule) job. I have look in quartz tutorial but I can't find the way to do it in c#.
You can call following method in IScheduler
//
// Summary:
// Remove the indicated Quartz.Trigger from the scheduler.
bool UnscheduleJob(string triggerName, string groupName);
Your Job class can implement IInterruptableJob interface and implement
public void Interrupt()
{
JobKey jobKey = new JobKey(JobName, MyService.GroupName);
Scheduler.Interrupt(jobKey);
}

Categories