Calling schedule with ScheduleJobs

感情迁移 提交于 2019-12-09 01:08:32

问题


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.

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);

回答1:


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

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);


来源:https://stackoverflow.com/questions/20396550/calling-schedule-with-schedulejobs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!