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.
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();
scheduler.Start();
IJobDetail job1 = JobBuilder.Create<FirstJob>()
.WithIdentity("firstJob")
.Build();
ITrigger trigger1 = TriggerBuilder.Create()
.WithIdentity("firstTrigger")
.WithSchedule(CronScheduleBuilder
.WeeklyOnDayAndHourAndMinute(DayOfWeek.Tuesday, 9, 0)
.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("UTC")))
.Build();
IJobDetail job2 = JobBuilder.Create<SecondJob>()
.WithIdentity("secondJob")
.Build();
ITrigger trigger2 = TriggerBuilder.Create()
.WithIdentity("secondTrigger")
.WithSchedule(CronScheduleBuilder
.WeeklyOnDayAndHourAndMinute(DayOfWeek.Wednesday, 9, 0)
.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("UTC")))
.Build();
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()
.WithSimpleSchedule(a=>a.WithIntervalInMinutes(100).RepeatForever()).Build();
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.
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();
scheduler.Start();
IJobDetail job1 = JobBuilder.Create<FirstJob>()
.WithIdentity("firstJob")
.Build();
ITrigger trigger1 = TriggerBuilder.Create()
.WithIdentity("firstTrigger")
.WithSchedule(CronScheduleBuilder
.WeeklyOnDayAndHourAndMinute(DayOfWeek.Tuesday, 9, 0)
.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("UTC")))
.Build();
IJobDetail job2 = JobBuilder.Create<SecondJob>()
.WithIdentity("secondJob")
.Build();
ITrigger trigger2 = TriggerBuilder.Create()
.WithIdentity("secondTrigger")
.WithSchedule(CronScheduleBuilder
.WeeklyOnDayAndHourAndMinute(DayOfWeek.Wednesday, 9, 0)
.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("UTC")))
.Build();
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()
.WithSimpleSchedule(a=>a.WithIntervalInMinutes(100).RepeatForever()).Build();
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.
Related articles
- Install Tools for Apache Cordova in Visual Studio 2015
- CodeLobster PHP Edition - Free PHP IDE
- HTML5 Responsive Frameworks for Web Developers
- Online Javascript Editors for Developers
- What is Apache Cordova and Ionic Framework
- Basic Introduction to AngularJS
- HTML5 Syntax's Simplicity
- Starting with TypeScript
- Getting Started with BootStrap
- IonicLab for Windows
No comments:
Post a Comment