Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz.NET - Trigger at specific hours of the day

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?

like image 906
mondul Avatar asked Apr 09 '14 18:04

mondul


People also ask

How do you trigger Quartz scheduler?

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.

How does Quartz trigger work?

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.

How do you schedule multiple jobs using Quartz?

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


Video Answer


1 Answers

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 ...
like image 73
Marko Lahma Avatar answered Oct 21 '22 18:10

Marko Lahma