Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling schedule with ScheduleJobs

Tags:

c#

quartz.net

I've been trying to figure out how to call the ScheduleJobs methods within Quartz.Net, but struggling to create the correct parameters it's expecting.

ScheduleJobs Parameters

Here's what I've tried:

IJobDetail jobDetail = JobBuilder.Create<ReportJob>()
    .WithIdentity("theJob")
    .Build();
ITrigger everydayTrigger = TriggerBuilder.Create()
    .WithIdentity("everydayTrigger")
    // fires 
    .WithCronSchedule("0 0 12 1/1 * ?")
    // start immediately
    .StartAt(DateBuilder.DateOf(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year))
    .Build();
ITrigger yearlyTrigger = TriggerBuilder.Create()
    .WithIdentity("yearlyTrigger")
    // fires 
    .WithCronSchedule("0 0 12 1 1 ? *")
    // start immediately
    .StartAt(DateBuilder.DateOf(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year))
    .Build();
var dictionary = new Dictionary<IJobDetail, ISet<ITrigger>>();
dictionary.Add(jobDetail, new HashSet<ITrigger>() { everydayTrigger, yearlyTrigger });
sched.ScheduleJobs((dictionary, true); // is a cast possible here to IDictionary?

Other attempts;

sched.ScheduleJobs(new [] { jobDetail, new[] { afternoonTrigger } }, true);

&&

sched.ScheduleJobs(new[] { new KeyValuePair<IJobDetail, ITrigger>()}.ToDictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>(), true);
like image 785
wonea Avatar asked Feb 15 '23 11:02

wonea


1 Answers

Make sure you use the collections from Quartz's namespace, here's a version of your code that compiles with Quartz 2.x:

IJobDetail jobDetail = JobBuilder.Create<ReportJob>()
    .WithIdentity("theJob")
    .Build();
ITrigger everydayTrigger = TriggerBuilder.Create()
    .WithIdentity("everydayTrigger")
    // fires 
    .WithCronSchedule("0 0 12 1/1 * ?")
    // start immediately
    .StartAt(DateBuilder.DateOf(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year))
    .Build();
ITrigger yearlyTrigger = TriggerBuilder.Create()
    .WithIdentity("yearlyTrigger")
    // fires 
    .WithCronSchedule("0 0 12 1 1 ? *")
    // start immediately
    .StartAt(DateBuilder.DateOf(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year))
    .Build();
var dictionary = new Dictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>>();
dictionary.Add(jobDetail, new Quartz.Collection.HashSet<ITrigger>()
                            {
                                everydayTrigger,
                                yearlyTrigger
                            });
sched.ScheduleJobs(dictionary, true);

Please not that for Quartz 3 you should use the BCL types, like System.Collections.Generic.HashSet<T>, there's no need to use Quartz's custom types anymore.

like image 186
Marko Lahma Avatar answered Feb 19 '23 06:02

Marko Lahma