katit
katit

Reputation: 17915

Schedule job with multiple triggers in Quartz.net

I need to run job on 2 different schedules (morning and afternoon). I know how to run it on one schedule, but not sure how to set it so 2 schedules trigger this job

var saferWatchJobDetail = JobBuilder.Create<SaferWatchProcessor>().Build();
            var swtriggerMorning = TriggerBuilder.Create().WithCronSchedule("0 10 6 ? * MON-SUN *", cs => cs.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))).Build();
            var swtriggerAfternoon = TriggerBuilder.Create().WithCronSchedule("0 10 13 ? * MON-FRI *", cs => cs.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))).Build();
            this.scheduler.ScheduleJob(saferWatchJobDetail, swtriggerMorning);

According to documentation it seems like I need to use ScheduleJob override with ISet<ITrigger> but I'm not sure about 2 things:

  1. What is implementing ISet and how do I instantiate this object?
  2. What is other parameter replace? Not sure how should I use it. And what is the "key"? From documentation:

If any of the given job or triggers already exist (or more specifically, if the keys are not unique) and the replace parameter is not set to true then an exception will be thrown.

Upvotes: 1

Views: 4107

Answers (1)

Tjaart
Tjaart

Reputation: 4129

granaCoder's answer didn't work for me at all. It kept throwing exceptions. Not sure why.

If you want to use triggerset, which ended up working for me, this is how you do it:

        var triggerSet = new Quartz.Collection.HashSet<ITrigger>();
        var saferWatchJobDetail = JobBuilder.Create<SaferWatchProcessor>().Build();
        var swtriggerMorning = TriggerBuilder.Create().WithCronSchedule("0 10 6 ? * MON-SUN *", cs => cs.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))).Build();
        var swtriggerAfternoon = TriggerBuilder.Create().WithCronSchedule("0 10 13 ? * MON-FRI *", cs => cs.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))).Build();
        triggerSet.Add(swtriggerMorning);
        triggerSet.Add(swtriggerAfternoon);
        this.scheduler.ScheduleJob(saferWatchJobDetail, triggerSet, true);

Upvotes: 4

Related Questions