Quartz.Net系列(九):Trigger之DailyTimeIntervalScheduleBuilder详解

1.介绍

中文意义就是每日时间间隔计划生成

2.API讲解

(1)WithInterval、WithIntervalInHours、WithIntervalInMinutes、WithIntervalInSeconds

WithInterval:指定要生成触发器的时间单位和间隔。

WithIntervalInHours:指定要生成触发器的间隔按小时来

WithIntervalInMinutes:指定要生成触发器的间隔按分钟来

WithIntervalInSeconds:指定要生成触发器的间隔按秒来

和前面的SimpleSceduleBuilder、CalendarIntervalScheduleBuilder一样的

 

(2)OnDaysOfTheWeek、OnMondayThroughFriday、OnSaturdayAndSunday、OnEveryDay

 

OnDaysOfTheWeek:设置触发器一周中的哪几天

OnMondayThroughFriday:从星期一到星期五

OnSaturdayAndSunday:周六和周日

OnEveryDay:每天

每天10:00到23:10.00的每一分钟执行一次

  var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                  w => w.OnEveryDay() //设置每天
                      .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(10, 00)) //设置每天开始于几点
                      .EndingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(23, 10, 0)) //设置每日结束于几点
                      .WithIntervalInMinutes(1)//间隔分钟
                ).Build();

一周当中的星期二和星期三每秒执行一次

            List dayOfWeeks = new List();

            dayOfWeeks.Add(DayOfWeek.Wednesday);

            dayOfWeeks.Add(DayOfWeek.Tuesday);

            trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                 w=>w.OnDaysOfTheWeek(dayOfWeeks)//.OnDaysOfTheWeek(new DayOfWeek[2] { DayOfWeek.Wednesday,DayOfWeek.Tuesday})
                     .WithIntervalInSeconds(1)
                ).Build();

 

Quartz.Net系列(九):Trigger之DailyTimeIntervalScheduleBuilder详解_第1张图片

 

 

 源码实现

  /// 
        /// Set the trigger to fire on the given days of the week.
        /// 
        /// a Set containing the integers representing the days of the week, defined by  - .
        /// 
        /// the updated DailyTimeIntervalScheduleBuilder
        public DailyTimeIntervalScheduleBuilder OnDaysOfTheWeek(IReadOnlyCollection onDaysOfWeek)
        {
            if (onDaysOfWeek == null || onDaysOfWeek.Count == 0)
            {
                throw new ArgumentException("Days of week must be an non-empty set.");
            }

            foreach (DayOfWeek day in onDaysOfWeek)
            {
                if (!AllDaysOfTheWeek.Contains(day))
                {
                    throw new ArgumentException("Invalid value for day of week: " + day);
                }
            }

            daysOfWeek = new HashSet(onDaysOfWeek);
            return this;
        }

        /// 
        /// Set the trigger to fire on the given days of the week.
        /// 
        /// a variable length list of week days representing the days of the week
        /// the updated DailyTimeIntervalScheduleBuilder
        public DailyTimeIntervalScheduleBuilder OnDaysOfTheWeek(params DayOfWeek[] onDaysOfWeek)
        {
            return OnDaysOfTheWeek((IReadOnlyCollection) onDaysOfWeek);
        }

        /// 
        /// Set the trigger to fire on the days from Monday through Friday.
        /// 
        /// the updated DailyTimeIntervalScheduleBuilder
        public DailyTimeIntervalScheduleBuilder OnMondayThroughFriday()
        {
            daysOfWeek = new HashSet(MondayThroughFriday);
            return this;
        }

        /// 
        /// Set the trigger to fire on the days Saturday and Sunday.
        /// 
        /// the updated DailyTimeIntervalScheduleBuilder
        public DailyTimeIntervalScheduleBuilder OnSaturdayAndSunday()
        {
            daysOfWeek = new HashSet(SaturdayAndSunday);
            return this;
        }

        /// 
        /// Set the trigger to fire on all days of the week.
        /// 
        /// the updated DailyTimeIntervalScheduleBuilder
        public DailyTimeIntervalScheduleBuilder OnEveryDay()
        {
            daysOfWeek = new HashSet(AllDaysOfTheWeek);
            return this;
        }

(3)StartingDailyAt、EndingDailyAt

StartingDailyAt:开始时间于

EndingDailyAt:结束时间于

源码实现

        /// 
        /// The TimeOfDay for this trigger to start firing each day.
        /// 
        /// 
        /// the updated DailyTimeIntervalScheduleBuilder
        public DailyTimeIntervalScheduleBuilder StartingDailyAt(TimeOfDay timeOfDayUtc)
        {
            startTimeOfDayUtc = timeOfDayUtc ?? throw new ArgumentException("Start time of day cannot be null!");
            return this;
        }

        /// 
        /// The TimeOfDay for this trigger to end firing each day.
        /// 
        /// 
        /// the updated DailyTimeIntervalScheduleBuilder
        public DailyTimeIntervalScheduleBuilder EndingDailyAt(TimeOfDay timeOfDayUtc)
        {
            endTimeOfDayUtc = timeOfDayUtc;
            return this;
        }

(4)EndingDailyAfterCount

EndingDailyAfterCount:使用count、interval和StarTimeOfDay计算并设置EndTimeOfDay。

源码实现

       /// 
        /// Calculate and set the EndTimeOfDay using count, interval and StarTimeOfDay. This means
        /// that these must be set before this method is call.
        /// 
        /// 
        /// the updated DailyTimeIntervalScheduleBuilder
        public DailyTimeIntervalScheduleBuilder EndingDailyAfterCount(int count)
        {
            if (count <= 0)
            {
                throw new ArgumentException("Ending daily after count must be a positive number!");
            }

            if (startTimeOfDayUtc == null)
            {
                throw new ArgumentException("You must set the StartDailyAt() before calling this EndingDailyAfterCount()!");
            }

            DateTimeOffset today = SystemTime.UtcNow();
            DateTimeOffset startTimeOfDayDate = startTimeOfDayUtc.GetTimeOfDayForDate(today).Value;
            DateTimeOffset maxEndTimeOfDayDate = TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59).GetTimeOfDayForDate(today).Value;

            //apply proper offsets according to timezone
            TimeZoneInfo targetTimeZone = timeZone ?? TimeZoneInfo.Local;
            startTimeOfDayDate = new DateTimeOffset(startTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(startTimeOfDayDate.DateTime, targetTimeZone));
            maxEndTimeOfDayDate = new DateTimeOffset(maxEndTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(maxEndTimeOfDayDate.DateTime, targetTimeZone));

            TimeSpan remainingMillisInDay = maxEndTimeOfDayDate - startTimeOfDayDate;
            TimeSpan intervalInMillis;
            if (intervalUnit == IntervalUnit.Second)
            {
                intervalInMillis = TimeSpan.FromSeconds(interval);
            }
            else if (intervalUnit == IntervalUnit.Minute)
            {
                intervalInMillis = TimeSpan.FromMinutes(interval);
            }
            else if (intervalUnit == IntervalUnit.Hour)
            {
                intervalInMillis = TimeSpan.FromHours(interval);
            }
            else
            {
                throw new ArgumentException("The IntervalUnit: " + intervalUnit + " is invalid for this trigger.");
            }

            if (remainingMillisInDay < intervalInMillis)
            {
                throw new ArgumentException("The startTimeOfDay is too late with given Interval and IntervalUnit values.");
            }

            long maxNumOfCount = remainingMillisInDay.Ticks / intervalInMillis.Ticks;
            if (count > maxNumOfCount)
            {
                throw new ArgumentException("The given count " + count + " is too large! The max you can set is " + maxNumOfCount);
            }

            TimeSpan incrementInMillis = TimeSpan.FromTicks((count - 1) * intervalInMillis.Ticks);
            DateTimeOffset endTimeOfDayDate = startTimeOfDayDate.Add(incrementInMillis);

            if (endTimeOfDayDate > maxEndTimeOfDayDate)
            {
                throw new ArgumentException("The given count " + count + " is too large! The max you can set is " + maxNumOfCount);
            }

            DateTime cal = SystemTime.UtcNow().Date;
            cal = cal.Add(endTimeOfDayDate.TimeOfDay);
            endTimeOfDayUtc = TimeOfDay.HourMinuteAndSecondOfDay(cal.Hour, cal.Minute, cal.Second);
            return this;
        }

 

你可能感兴趣的:(Quartz.Net系列(九):Trigger之DailyTimeIntervalScheduleBuilder详解)