a blog for those who code

Saturday 16 July 2016

How to schedule multiple jobs in Quartz.Net

In this post we will be discussing about scheduling multiple jobs in Quartz.Net. In our previous post we have discussed about creating scheduled jobs in Asp.Net application. Now lets say you want to schedule two jobs which will run independently at different time of the day. For this scenario you have to create multiple jobs with different identity.

Schedule Multiple Jobs

Now if you want to schedule multiple jobs in your application you can simply call Scheduler.ScheduleJob (IScheduler) passing the job and trigger you have created as shown below :

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

IJobDetail job1 = JobBuilder.Create<FirstJob>()

ITrigger trigger1 = TriggerBuilder.Create()
         .WeeklyOnDayAndHourAndMinute(DayOfWeek.Tuesday, 9, 0)

IJobDetail job2 = JobBuilder.Create<SecondJob>()

ITrigger trigger2 = TriggerBuilder.Create()
         .WeeklyOnDayAndHourAndMinute(DayOfWeek.Wednesday, 9, 0)

scheduler.ScheduleJob(job1, trigger1);
scheduler.ScheduleJob(job2, trigger2);

We have created a job using Quartz.Net and pass our FirstJob and SecondJob instance and build it. Next is to create a trigger which will run the above jobs at specific time of the day or could be anything. We will create a trigger using TriggerBuilder. To know more about TimeZoneInfo.FindSystemTimeZonebyID you can refer How to get Timezone ID's in C#.

Another example of a trigger is shown below which will trigger the job once every 100 Minutes.

ITrigger trigger = TriggerBuilder.Create()

Now both the job and trigger has been created and both of them have been registered with our scheduler.

Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.

No comments:

Post a Comment