Quartz.NET is a pure .NET library written in C# and is a port of very popular open source Java job scheduling framework. let's see how we can create Triggers and jobs dynamically
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class JobsAndTriggers
{
public string jobIdentityKey { get; set; }
public string TriggerIdentityKey { get; set; }
public string jobData_MethodName { get; set; }
public int ScheduleIntervalInSec { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<JobsAndTriggers> LstobjJobsAndTriggers = new List<JobsAndTriggers>();
LstobjJobsAndTriggers.Add(new JobsAndTriggers { jobIdentityKey = "JOB1", TriggerIdentityKey = "TRIGGER1", jobData_MethodName = "JOB1_METHOD()", ScheduleIntervalInSec = 5 });
LstobjJobsAndTriggers.Add(new JobsAndTriggers { jobIdentityKey = "JOB2", TriggerIdentityKey = "TRIGGER2", jobData_MethodName = "JOB2_METHOD()", ScheduleIntervalInSec = 10 });
TestDemoJob1(LstobjJobsAndTriggers).GetAwaiter().GetResult();
Console.ReadKey();
}
public static async Task TestDemoJob1(List<JobsAndTriggers> lstJobsAndTriggers)
{
IScheduler scheduler;
IJobDetail job =null;
ITrigger trigger = null;
var schedulerFactory = new StdSchedulerFactory();
scheduler = schedulerFactory.GetScheduler().Result;
scheduler.Start().Wait();
foreach(var JobandTrigger in lstJobsAndTriggers)
{
// int ScheduleIntervalInSec = 1;//job will run every minute
JobKey jobKey = JobKey.Create(JobandTrigger.jobIdentityKey);
job = JobBuilder.Create<DemoJob1>().
WithIdentity(jobKey)
.UsingJobData("MethodName", JobandTrigger.jobData_MethodName)
.Build();
trigger = TriggerBuilder.Create()
.WithIdentity(JobandTrigger.TriggerIdentityKey)
.StartNow()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(JobandTrigger.ScheduleIntervalInSec).WithRepeatCount(1)
// .RepeatForever()
)
.Build();
await scheduler.ScheduleJob(job, trigger);
}
}
}
public class DemoJob1 : IJob
{
public Task Execute(IJobExecutionContext context)
{
//simple task, the job just prints current datetime in console
//return Task.Run(() => {
// //Console.WriteLine(DateTime.Now.ToString());
//});
JobKey key = context.JobDetail.Key;
JobDataMap dataMap = context.JobDetail.JobDataMap;
string MethodName = dataMap.GetString("MethodName");
Console.WriteLine(DateTime.Now.ToString() +" "+ MethodName);
return Task.FromResult(0);
}
}
}