所有方法图:
1.Create、Build
Create:创建一个TriggerBuilder
Build:生成Trigger
var trigger = TriggerBuilder.Create().Build();
底层实现
////// Create a new TriggerBuilder with which to define a /// specification for a Trigger. /// /// /// /// the new TriggerBuilder public static TriggerBuilder Create() { return new TriggerBuilder(); } /// /// Produce the . /// /// /// /// a Trigger that meets the specifications of the builder. public ITrigger Build() { if (scheduleBuilder == null) { scheduleBuilder = SimpleScheduleBuilder.Create(); } IMutableTrigger trig = scheduleBuilder.Build(); trig.CalendarName = calendarName; trig.Description = description; trig.StartTimeUtc = startTime; trig.EndTimeUtc = endTime; if (key == null) { key = new TriggerKey(Guid.NewGuid().ToString(), null); } trig.Key = key; if (jobKey != null) { trig.JobKey = jobKey; } trig.Priority = priority; if (!jobDataMap.IsEmpty) { trig.JobDataMap = jobDataMap; } return trig; }
2.StartAt、EndAt、StartNow
StartAt:设置触发器应该开始的时间
EndAt:设置触发器结束时间
StartNow:将触发器的启动时间设置为当前时刻
var trigger = TriggerBuilder.Create() .StartNow() //.StartAt(DateTimeOffset.Now) .EndAt(DateTimeOffset.Now.AddSeconds(5)) .WithSimpleSchedule(x=>x.RepeatForever().WithIntervalInSeconds(1)) .Build();
注:DateTime和DateTimeOffset的区别
DateTimeOffset是跨时区的,也就是按照UTC时间来执行
底层实现:
////// Set the time the Trigger should start at - the trigger may or may /// not fire at this time - depending upon the schedule configured for /// the Trigger. However the Trigger will NOT fire before this time, /// regardless of the Trigger's schedule. /// /// /// /// the start time for the Trigger. /// the updated TriggerBuilder /// /// public TriggerBuilder StartAt(DateTimeOffset startTimeUtc) { startTime = startTimeUtc; return this; } /// /// Set the time the Trigger should start at to the current moment - /// the trigger may or may not fire at this time - depending upon the /// schedule configured for the Trigger. /// /// /// /// the updated TriggerBuilder /// public TriggerBuilder StartNow() { startTime = SystemTime.UtcNow(); return this; } /// /// Set the time at which the Trigger will no longer fire - even if it's /// schedule has remaining repeats. /// /// /// /// the end time for the Trigger. If null, the end time is indefinite. /// the updated TriggerBuilder /// /// public TriggerBuilder EndAt(DateTimeOffset? endTimeUtc) { endTime = endTimeUtc; return this; }
3.ForJob
ForJob:指定对应关系的JobKey,四个重载方法,底层都是指定一个JobKey
var job = JobBuilder.CreateForAsync() //.StoreDurably() .WithIdentity("myJob", "jobGroup") .SetJobData(new JobDataMap(dict)) .UsingJobData("Password","123456") .Build(); //trigger WithIntervalInSeconds(1)间隔1m RepeatForever重复 //var trigger = TriggerBuilder.Create().WithSimpleSchedule(x => // x.WithIntervalInSeconds(1) // //.RepeatForever() // ).Build();. var trigger = TriggerBuilder.Create() .StartNow() .ForJob("myJob","jobGroup") //.StartAt(DateTimeOffset.Now) .EndAt(DateTimeOffset.Now.AddSeconds(5)) .WithSimpleSchedule(x=>x.RepeatForever().WithIntervalInSeconds(1)) .Build(); //scheduler.ListenerManager.AddJobListener(new CustomJobListener(),GroupMatcher .AnyGroup()); //scheduler.ListenerManager.AddTriggerListener(); await scheduler.ScheduleJob(job, trigger); //await Monitor(); var jobGroups =await scheduler.GetJobGroupNames(); Console.WriteLine(jobGroups.FirstOrDefault());
底层实现
////// Set the identity of the Job which should be fired by the produced /// Trigger. /// /// /// /// the identity of the Job to fire. /// the updated TriggerBuilder /// public TriggerBuilder ForJob(JobKey jobKey) { this.jobKey = jobKey; return this; } /// /// Set the identity of the Job which should be fired by the produced /// Trigger - a will be produced with the given /// name and default group. /// /// /// /// the name of the job (in default group) to fire. /// the updated TriggerBuilder /// public TriggerBuilder ForJob(string jobName) { jobKey = new JobKey(jobName, null); return this; } /// /// Set the identity of the Job which should be fired by the produced /// Trigger - a will be produced with the given /// name and group. /// /// /// /// the name of the job to fire. /// the group of the job to fire. /// the updated TriggerBuilder /// public TriggerBuilder ForJob(string jobName, string jobGroup) { jobKey = new JobKey(jobName, jobGroup); return this; } /// /// Set the identity of the Job which should be fired by the produced /// Trigger, by extracting the JobKey from the given job. /// /// /// /// the Job to fire. /// the updated TriggerBuilder /// public TriggerBuilder ForJob(IJobDetail jobDetail) { JobKey k = jobDetail.Key; if (k.Name == null) { throw new ArgumentException("The given job has not yet had a name assigned to it."); } jobKey = k; return this; }
4.UsingJobData
UsingJobData:添加Job数据,底层是key-vaule的JobDataMap,和JobBuilder里面的一样
await Task.Run(() => { //var userName=context.MergedJobDataMap.GetString("UserName"); //var password = context.MergedJobDataMap.GetString("Password"); //Console.WriteLine(userName); //Console.WriteLine(password); var name = context.Trigger.JobDataMap.GetString("name"); Console.WriteLine(name); //Console.WriteLine("Hello World !"); });
5.WithPriority
WithPriority:设置触发器的优先级。当多个触发器具有相同的启动时间,调度程序将启动优先级最高的,默认5,数字大的优先级高
var trigger1 = TriggerBuilder.Create() //.StartNow() //.ForJob("myJob", "jobGroup") .StartAt(DateBuilder.EvenSecondDateAfterNow()) .UsingJobData("name", "trigger1") .WithPriority(1) .EndAt(DateTimeOffset.Now.AddMinutes(1)) .WithSimpleSchedule(x => x.RepeatForever().WithIntervalInSeconds(1)) .Build(); var trigger2 = TriggerBuilder.Create() //.StartNow() //.ForJob("myJob", "jobGroup") .StartAt(DateBuilder.EvenSecondDateAfterNow()) .UsingJobData("name", "trigger2") .WithPriority(10) .EndAt(DateTimeOffset.Now.AddMinutes(1)) .WithSimpleSchedule(x => x.RepeatForever().WithIntervalInSeconds(1)) .Build(); HashSettriggers = new HashSet (); triggers.Add(trigger1); triggers.Add(trigger2); await scheduler.ScheduleJob(job, triggers,true);
底层实现
public TriggerBuilder WithPriority(int priority) { this.priority = priority; return this; }
private int priority = TriggerConstants.DefaultPriority;
public static class TriggerConstants { ////// The default value for priority. /// public const int DefaultPriority = 5; }
6.WithSchedule
所有ScheduleBuilder的生成接口,默认有:SimpleScheduleBuilder、CalendarIntervalScheduleBuilder、CronScheduleBuilder、DailyTimeIntervalScheduleBuilder
表示可以扩展自己的实现类
////// Set the that will be used to define the /// Trigger's schedule. /// /// /// The particular used will dictate /// the concrete type of Trigger that is produced by the TriggerBuilder. /// /// the SchedulerBuilder to use. /// the updated TriggerBuilder /// /// /// /// public TriggerBuilder WithSchedule(IScheduleBuilder scheduleBuilder) { this.scheduleBuilder = scheduleBuilder; return this; }
7.WithIdentity
设置TriggerKey,底层就是实现TriggerKey
var trigger2 = TriggerBuilder.Create() //.StartNow() //.ForJob("myJob", "jobGroup") .StartAt(DateBuilder.EvenSecondDateAfterNow()) .UsingJobData("name", "trigger2") .WithPriority(10) .EndAt(DateTimeOffset.Now.AddMinutes(1)) .WithIdentity(new TriggerKey("myTrigger")) .WithSimpleSchedule(x => x.RepeatForever().WithIntervalInSeconds(1)) .Build();
////// Use a with the given name and default group to /// identify the Trigger. /// /// /// If none of the 'withIdentity' methods are set on the TriggerBuilder, /// then a random, unique TriggerKey will be generated. /// /// the name element for the Trigger's TriggerKey /// the updated TriggerBuilder /// /// public TriggerBuilder WithIdentity(string name) { key = new TriggerKey(name, null); return this; } /// /// Use a TriggerKey with the given name and group to /// identify the Trigger. /// /// /// If none of the 'withIdentity' methods are set on the TriggerBuilder, /// then a random, unique TriggerKey will be generated. /// /// the name element for the Trigger's TriggerKey /// the group element for the Trigger's TriggerKey /// the updated TriggerBuilder /// /// public TriggerBuilder WithIdentity(string name, string group) { key = new TriggerKey(name, group); return this; } /// /// Use the given TriggerKey to identify the Trigger. /// /// /// If none of the 'withIdentity' methods are set on the TriggerBuilder, /// then a random, unique TriggerKey will be generated. /// /// the TriggerKey for the Trigger to be built /// the updated TriggerBuilder /// /// public TriggerBuilder WithIdentity(TriggerKey key) { this.key = key; return this; }
8.WithDesciption
添加说明
var trigger2 = TriggerBuilder.Create() //.StartNow() //.ForJob("myJob", "jobGroup") .StartAt(DateBuilder.EvenSecondDateAfterNow()) .UsingJobData("name", "trigger2") .WithPriority(10) .EndAt(DateTimeOffset.Now.AddMinutes(1)) .WithIdentity(new TriggerKey("myTrigger")) .WithDescription("description") .WithSimpleSchedule(x => x.RepeatForever().WithIntervalInSeconds(1)) .Build();
9.WithDailyTimeIntervalSchedule、WithSimpleSchedule、WithCronSchedule、WithCalendarIntervalSchedule
一些ScheduleBuilder的实现类