所有方法圖:
1.Create、Build
Create:建立一個TriggerBuilder
Build:生成Trigger
var trigger = TriggerBuilder.Create().Build();
底層實現
/// <summary> /// Create a new TriggerBuilder with which to define a /// specification for a Trigger. /// </summary> /// <remarks> /// </remarks> /// <returns>the new TriggerBuilder</returns> public static TriggerBuilder Create() { return new TriggerBuilder(); } /// <summary> /// Produce the <see cref="ITrigger" />. /// </summary> /// <remarks> /// </remarks> /// <returns>a Trigger that meets the specifications of the builder.</returns> 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時間來執行
底層實現:
/// <summary> /// 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. /// </summary> /// <remarks> /// </remarks> /// <param name="startTimeUtc">the start time for the Trigger.</param> /// <returns>the updated TriggerBuilder</returns> /// <seealso cref="ITrigger.StartTimeUtc" /> /// <seealso cref="DateBuilder" /> public TriggerBuilder StartAt(DateTimeOffset startTimeUtc) { startTime = startTimeUtc; return this; } /// <summary> /// 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. /// </summary> /// <remarks> /// </remarks> /// <returns>the updated TriggerBuilder</returns> /// <seealso cref="ITrigger.StartTimeUtc" /> public TriggerBuilder StartNow() { startTime = SystemTime.UtcNow(); return this; } /// <summary> /// Set the time at which the Trigger will no longer fire - even if it's /// schedule has remaining repeats. /// </summary> /// <remarks> /// </remarks> /// <param name="endTimeUtc">the end time for the Trigger. If null, the end time is indefinite.</param> /// <returns>the updated TriggerBuilder</returns> /// <seealso cref="ITrigger.EndTimeUtc" /> /// <seealso cref="DateBuilder" /> public TriggerBuilder EndAt(DateTimeOffset? endTimeUtc) { endTime = endTimeUtc; return this; }
3.ForJob
ForJob:指定對應關係的JobKey,四個過載方法,底層都是指定一個JobKey
var job = JobBuilder.CreateForAsync<FirstJob>() //.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<JobKey>.AnyGroup()); //scheduler.ListenerManager.AddTriggerListener(); await scheduler.ScheduleJob(job, trigger); //await Monitor(); var jobGroups =await scheduler.GetJobGroupNames(); Console.WriteLine(jobGroups.FirstOrDefault());
底層實現
/// <summary> /// Set the identity of the Job which should be fired by the produced /// Trigger. /// </summary> /// <remarks> /// </remarks> /// <param name="jobKey">the identity of the Job to fire.</param> /// <returns>the updated TriggerBuilder</returns> /// <seealso cref="ITrigger.JobKey" /> public TriggerBuilder ForJob(JobKey jobKey) { this.jobKey = jobKey; return this; } /// <summary> /// Set the identity of the Job which should be fired by the produced /// Trigger - a <see cref="JobKey" /> will be produced with the given /// name and default group. /// </summary> /// <remarks> /// </remarks> /// <param name="jobName">the name of the job (in default group) to fire.</param> /// <returns>the updated TriggerBuilder</returns> /// <seealso cref="ITrigger.JobKey" /> public TriggerBuilder ForJob(string jobName) { jobKey = new JobKey(jobName, null); return this; } /// <summary> /// Set the identity of the Job which should be fired by the produced /// Trigger - a <see cref="JobKey" /> will be produced with the given /// name and group. /// </summary> /// <remarks> /// </remarks> /// <param name="jobName">the name of the job to fire.</param> /// <param name="jobGroup">the group of the job to fire.</param> /// <returns>the updated TriggerBuilder</returns> /// <seealso cref="ITrigger.JobKey" /> public TriggerBuilder ForJob(string jobName, string jobGroup) { jobKey = new JobKey(jobName, jobGroup); return this; } /// <summary> /// Set the identity of the Job which should be fired by the produced /// Trigger, by extracting the JobKey from the given job. /// </summary> /// <remarks> /// </remarks> /// <param name="jobDetail">the Job to fire.</param> /// <returns>the updated TriggerBuilder</returns> /// <seealso cref="ITrigger.JobKey" /> 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(); HashSet<ITrigger> triggers = new HashSet<ITrigger>(); 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 { /// <summary> /// The default value for priority. /// </summary> public const int DefaultPriority = 5; }
6.WithSchedule
所有ScheduleBuilder的生成介面,預設有:SimpleScheduleBuilder、CalendarIntervalScheduleBuilder、CronScheduleBuilder、DailyTimeIntervalScheduleBuilder
表示可以擴充套件自己的實現類
/// <summary> /// Set the <see cref="IScheduleBuilder" /> that will be used to define the /// Trigger's schedule. /// </summary> /// <remarks> /// <para>The particular <see cref="IScheduleBuilder" /> used will dictate /// the concrete type of Trigger that is produced by the TriggerBuilder.</para> /// </remarks> /// <param name="scheduleBuilder">the SchedulerBuilder to use.</param> /// <returns>the updated TriggerBuilder</returns> /// <seealso cref="IScheduleBuilder" /> /// <seealso cref="SimpleScheduleBuilder" /> /// <seealso cref="CronScheduleBuilder" /> /// <seealso cref="CalendarIntervalScheduleBuilder" /> 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();
/// <summary> /// Use a <see cref="TriggerKey" /> with the given name and default group to /// identify the Trigger. /// </summary> /// <remarks> /// <para>If none of the 'withIdentity' methods are set on the TriggerBuilder, /// then a random, unique TriggerKey will be generated.</para> /// </remarks> /// <param name="name">the name element for the Trigger's TriggerKey</param> /// <returns>the updated TriggerBuilder</returns> /// <seealso cref="TriggerKey" /> /// <seealso cref="ITrigger.Key" /> public TriggerBuilder WithIdentity(string name) { key = new TriggerKey(name, null); return this; } /// <summary> /// Use a TriggerKey with the given name and group to /// identify the Trigger. /// </summary> /// <remarks> /// <para>If none of the 'withIdentity' methods are set on the TriggerBuilder, /// then a random, unique TriggerKey will be generated.</para> /// </remarks> /// <param name="name">the name element for the Trigger's TriggerKey</param> /// <param name="group">the group element for the Trigger's TriggerKey</param> /// <returns>the updated TriggerBuilder</returns> /// <seealso cref="TriggerKey" /> /// <seealso cref="ITrigger.Key" /> public TriggerBuilder WithIdentity(string name, string group) { key = new TriggerKey(name, group); return this; } /// <summary> /// Use the given TriggerKey to identify the Trigger. /// </summary> /// <remarks> /// <para>If none of the 'withIdentity' methods are set on the TriggerBuilder, /// then a random, unique TriggerKey will be generated.</para> /// </remarks> /// <param name="key">the TriggerKey for the Trigger to be built</param> /// <returns>the updated TriggerBuilder</returns> /// <seealso cref="TriggerKey" /> /// <seealso cref="ITrigger.Key" /> 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的實現類