目錄:html
一. Quartz的APIexpress
二.Trigger 的使用async
三.使用 JobDataMap 來往Job中傳值ide
四. Calendars ui
五.SimpleTriggerthis
六.CronTriggerspa
能夠對照 這一篇 quartz 的代碼來印證.net
// define the job and tie it to our HelloJob class IJobDetail job = JobBuilder.Create<HelloJob>() .WithIdentity("myJob", "group1") // name "myJob", group "group1" .Build(); // Trigger the job to run now, and then every 40 seconds ITrigger trigger = TriggerBuilder.Create() .WithIdentity("myTrigger", "group1") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(40) .RepeatForever()) .Build(); // Tell quartz to schedule the job using our trigger await sched.scheduleJob(job, trigger);
這裏除了 WithSimpleSchedule 擴展方法,還有以下:code
在JobDataMap中設置值htm
// define the job and tie it to our DumbJob class IJobDetail job = JobBuilder.Create<DumbJob>() .WithIdentity("myJob", "group1") // name "myJob", group "group1" .UsingJobData("jobSays", "Hello World!") .UsingJobData("myFloatValue", 3.141f) .Build();
從JobDataMap中獲取值
public class DumbJob : IJob { public async Task Execute(IJobExecutionContext context) { JobKey key = context.JobDetail.Key; JobDataMap dataMap = context.JobDetail.JobDataMap; string jobSays = dataMap.GetString("jobSays"); float myFloatValue = dataMap.GetFloat("myFloatValue"); await Console.Error.WriteLineAsync("Instance " + key + " of DumbJob says: " + jobSays + ", and val is: " + myFloatValue); } }
還有從 JobExecutionContext’s merged JobDataMap 中獲取值的方式
public class DumbJob : IJob { public async Task Execute(IJobExecutionContext context) { JobKey key = context.JobDetail.Key; JobDataMap dataMap = context.MergedJobDataMap; // Note the difference from the previous example string jobSays = dataMap.GetString("jobSays"); float myFloatValue = dataMap.GetFloat("myFloatValue"); IList<DateTimeOffset> state = (IList<DateTimeOffset>)dataMap["myStateData"]; state.Add(DateTimeOffset.UtcNow); await Console.Error.WriteLineAsync("Instance " + key + " of DumbJob says: " + jobSays + ", and val is: " + myFloatValue); } }
或者,能夠經過注入 data map 的值到類中
public class DumbJob : IJob { public string JobSays { private get; set; } public float FloatValue { private get; set; } public async Task Execute(IJobExecutionContext context) { JobKey key = context.JobDetail.Key; JobDataMap dataMap = context.MergedJobDataMap; // Note the difference from the previous example IList<DateTimeOffset> state = (IList<DateTimeOffset>)dataMap["myStateData"]; state.Add(DateTimeOffset.UtcNow); await Console.Error.WriteLineAsync("Instance " + key + " of DumbJob says: " + JobSays + ", and val is: " + FloatValue); } }
ICalendar 接口
namespace Quartz { public interface ICalendar { string Description { get; set; } ICalendar CalendarBase { set; get; } bool IsTimeIncluded(DateTimeOffset timeUtc); DateTime GetNextIncludedTimeUtc(DateTimeOffset timeUtc); ICalendar Clone(); } }
示例:
HolidayCalendar cal = new HolidayCalendar(); cal.AddExcludedDate(someDate); await sched.AddCalendar("myHolidays", cal, false); ITrigger t = TriggerBuilder.Create() .WithIdentity("myTrigger") .ForJob("myJob") .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(9, 30)) // execute job daily at 9:30 天天9:30執行 .ModifiedByCalendar("myHolidays") // but not on holidays 不包含假期 .Build(); // .. schedule job with trigger ITrigger t2 = TriggerBuilder.Create() .WithIdentity("myTrigger2") .ForJob("myJob2") .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(11, 30)) // execute job daily at 11:30 .ModifiedByCalendar("myHolidays") // but not on holidays .Build(); // .. schedule job with trigger2
用來在日曆上的某一段時間沒必要執行任務時,可使用這種方式
這裏列舉一些Simple Trigger 的使用示例
1.創建一個在某個時間點的觸發器,不重複執行
// trigger builder creates simple trigger by default, actually an ITrigger is returned ISimpleTrigger trigger = (ISimpleTrigger) TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartAt(myStartTime) // some Date .ForJob("job1", "group1") // identify job with name, group strings .Build();
2.創建一個某個時間點的觸發器,而且每10秒執行一次,執行10次
trigger = TriggerBuilder.Create() .WithIdentity("trigger3", "group1") .StartAt(myTimeToStartFiring) // if a start time is not given (if this line were omitted), "now" is implied .WithSimpleSchedule(x => x .WithIntervalInSeconds(10) .WithRepeatCount(10)) // note that 10 repeats will give a total of 11 firings .ForJob(myJob) // identify job with handle to its JobDetail itself .Build();
3.創建一個觸發器,在以後每5分鐘執行一次
trigger = (ISimpleTrigger) TriggerBuilder.Create() .WithIdentity("trigger5", "group1") .StartAt(DateBuilder.FutureDate(5, IntervalUnit.Minute)) // use DateBuilder to create a date in the future .ForJob(myJobKey) // identify job with its JobKey .Build();
4.創建一個觸發器,每5分鐘執行一次,知道22:00
trigger = TriggerBuilder.Create() .WithIdentity("trigger7", "group1") .WithSimpleSchedule(x => x .WithIntervalInMinutes(5) .RepeatForever()) .EndAt(DateBuilder.DateOf(22, 0, 0)) .Build();
5.創建一個觸發器,將在下一個整點執行,每2個小時執行一次
trigger = TriggerBuilder.Create() .WithIdentity("trigger8") // because group is not specified, "trigger8" will be in the default group .StartAt(DateBuilder.EvenHourDate(null)) // get the next even-hour (minutes and seconds zero ("00:00")) .WithSimpleSchedule(x => x .WithIntervalInHours(2) .RepeatForever()) // note that in this example, 'forJob(..)' is not called // - which is valid if the trigger is passed to the scheduler along with the job .Build(); await scheduler.scheduleJob(trigger, job);
CronExpression 用於配置CronTrigger 的實例,Cron-Expression 是一個由7個 sub-expression 組成的字符串,
這7個 sub-expression 由空格分隔開,表明的含義:
這裏的1-7個,每一個都是一個field ;
例如: 「0 0 12 ? * WED」 表示每一個週三的12點
1.單獨的 sub-expression 能夠包含 ranges and/or (即某個範圍,與,或) ,
例如前面的 week 屬性 的WED,能夠替換爲 「MON-FRI」 , 「MON, WED, FRI」 , 「MON-WED,SAT」
2.通配符能夠被用來表示可能的值,所以前面例子中的 Month 處的 * ,意味着每月;而 Day-of-Month 處若是使用 * ,表示 這周的天天
3.全部的field 都有一個有效值的集合可被指定。
4. / 符號,能夠用來指定值的增量。例如 0/15 表示在Minutes field 處,它意味着每15分鐘,從0分鐘開始;若是 ‘3/20’ ,表示在Minutes field 處,每20分鐘,從3分鐘處開始;
5.? 符號,用於day-of-month 和 day-of-week 處。用於指定沒有特定值。當你須要在這兩個field中的其中一個指定值,而另外一個不指定時,是有用的;
6. L 符號,用在 day-of-month 和 day-of-week 處。這個符號是 last 的縮寫,在兩個不一樣的field (指day-of-month 和day-of-week )有不一樣的含義。
例如,
L 用在day-of-month處意味着月份的最後一個天;
L單獨用在day-of-week 處,表示 7 或者 SAT ,
可是若是用在另外一個值的後面,則意味着 the last xxx day of the month ,
例如,6L或者FRIL 這兩個都意味着 the last friday of the month 即這個月的最後一個週五;
當使用L時,不要指定 lists 或者範圍值,由於你將獲得使人困惑的結果。
7.W 符號,指定最接近給定日期的周(星期)。例如,當你在 day-of-month 處指定15W時,意味着最接近這個月15號的周(星期)。
8. # 符號,指定月份的第幾個周(星期),例如, 在day-of-week 處的 6#3 或者 FRI#3 意味着月份的第三個週五
1>.每5分鐘執行一次
"0 0/5 * * * ?"
2>.每5分鐘,而且這分鐘的10秒開始執行(i.e. 10:00:10 am, 10:05:10 am, etc)
"10 0/5 * * * ?"
3>.每週三和週五的10:30,11:30,12:30,13:30 執行
" 0 30 10-13 ? * WED,FRI"
4>.在每月的5號和20號,在8點和10點之間,每30分鐘執行一次(10:00不執行,在8:00,8:30,9:00,9:30 執行)
"0 0/30 8-9 5,20 * ?"
在天天上午8:00到17:00之間,每隔1秒執行一次
trigger = TriggerBuilder.Create() .WithIdentity("trigger3", "group1") .WithCronSchedule("0 0/2 8-17 * * ?") .ForJob("myJob", "group1") .Build();
在天天10:42am執行
// we use CronScheduleBuilder's static helper methods here trigger = TriggerBuilder.Create() .WithIdentity("trigger3", "group1") .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(10, 42)) .ForJob(myJobKey) .Build();
或者
trigger = TriggerBuilder.Create() .WithIdentity("trigger3", "group1") .WithCronSchedule("0 42 10 * * ?") .ForJob("myJob", "group1") .Build();
創建一個觸發器,在每週三10:42執行,而且指定一個TimeZone而不是系統默認的
trigger = TriggerBuilder.Create() .WithIdentity("trigger3", "group1") .WithSchedule(CronScheduleBuilder .WeeklyOnDayAndHourAndMinute(DayOfWeek.Wednesday, 10, 42) .InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time"))) .ForJob(myJobKey) .Build();
或
trigger = TriggerBuilder.Create() .WithIdentity("trigger3", "group1") .WithCronSchedule("0 42 10 ? * WED", x => x .InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time"))) .ForJob(myJobKey) .Build();
還有一些監聽的listener 等其餘知識,這裏沒講,
能夠參考網址
https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/index.html