I have an array of hours when a single job needs to be run on the current day, something like this:
["00:05", "01:42", "04:21", "17:57"]
As you can see are arbitrary hours so I cannot use a Cron schedule. I've been searching on how to add multiple hours to a trigger, or how to use multiple triggers on the same job and I haven't found any ways to achieve this.
So, how can I run the same job at the times specified by the array?
All the Jobs registered in the Quartz Scheduler are uniquely identified by the JobKey which is composed of a name and group . You can fire the job which has a given JobKey immediately by calling triggerJob(JobKey jobKey) of your Scheduler instance. // Create a new Job JobKey jobKey = JobKey.
Quartz scheduler allows an enterprise to schedule a job at a specified date and time. It allows us to perform the operations to schedule or unschedule the jobs. It provides operations to start or stop or pause the scheduler. It also provides reminder services.
If you want to schedule multiple jobs in your console application you can simply call Scheduler. ScheduleJob (IScheduler) passing the job and the trigger you've previously created: IJobDetail firstJob = JobBuilder. Create<FirstJob>() .
Having multiple triggers for the job is the key.
var job = JobBuilder.Create<TheJobType>()
.StoreDurably(true)
.WithIdentity("the-job-all-are-going-to-execute")
.Build();
scheduler.AddJob(job, false);
var trigger1 = TriggerBuilder.Create()
.ForJob(job)
.WithIdentity("trigger1")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(0, 5))
.Build();
scheduler.ScheduleJob(trigger1);
var trigger2 = TriggerBuilder.Create()
.ForJob(job)
.WithIdentity("trigger2")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(1, 42))
.Build();
scheduler.ScheduleJob(trigger2);
.... etc ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With