Quartz.Net系列(十):Trigger之CronScheduleBuilder和Cron表達式詳解


1.使用

            var scheduler =await StdSchedulerFactory.GetDefaultScheduler();

            await scheduler.Start();

            var job = JobBuilder.Create<FirstJob>().Build();

            var trigger = TriggerBuilder.Create().WithCronSchedule("* * * * * ?").Build();

            await scheduler.ScheduleJob(job, trigger);

2.效果圖

 

 3.源碼解析

        public static TriggerBuilder WithCronSchedule(this TriggerBuilder triggerBuilder, string cronExpression)
        {
            CronScheduleBuilder builder = CronScheduleBuilder.CronSchedule(cronExpression);
            return triggerBuilder.WithSchedule(builder);
        }

 

 protected virtual int StoreExpressionVals(int pos, string s, int type)
        {
            int incr = 0;
            int i = SkipWhiteSpace(pos, s);
            if (i >= s.Length)
            {
                return i;
            }
            char c = s[i];
            if (c >= 'A' && c <= 'Z' && !s.Equals("L") && !s.Equals("LW") && !regex.IsMatch(s))
            {
                string sub = s.Substring(i, 3);
                int sval;
                int eval = -1;
                if (type == Month)
                {
                    sval = GetMonthNumber(sub) + 1;
                    if (sval <= 0)
                    {
                        throw new FormatException($"Invalid Month value: '{sub}'");
                    }
                    if (s.Length > i + 3)
                    {
                        c = s[i + 3];
                        if (c == '-')
                        {
                            i += 4;
                            sub = s.Substring(i, 3);
                            eval = GetMonthNumber(sub) + 1;
                            if (eval <= 0)
                            {
                                throw new FormatException(
                                    $"Invalid Month value: '{sub}'");
                            }
                        }
                    }
                }
                else if (type == DayOfWeek)
                {
                    sval = GetDayOfWeekNumber(sub);
                    if (sval < 0)
                    {
                        throw new FormatException($"Invalid Day-of-Week value: '{sub}'");
                    }
                    if (s.Length > i + 3)
                    {
                        c = s[i + 3];
                        if (c == '-')
                        {
                            i += 4;
                            sub = s.Substring(i, 3);
                            eval = GetDayOfWeekNumber(sub);
                            if (eval < 0)
                            {
                                throw new FormatException(
                                    $"Invalid Day-of-Week value: '{sub}'");
                            }
                        }
                        else if (c == '#')
                        {
                            try
                            {
                                i += 4;
                                nthdayOfWeek = Convert.ToInt32(s.Substring(i), CultureInfo.InvariantCulture);
                                if (nthdayOfWeek < 1 || nthdayOfWeek > 5)
                                {
                                    throw new Exception();
                                }
                            }
                            catch (Exception)
                            {
                                throw new FormatException(
                                    "A numeric value between 1 and 5 must follow the '#' option");
                            }
                        }
                        else if (c == '/')
                        {
                            try
                            {
                                i += 4;
                                everyNthWeek = Convert.ToInt32(s.Substring(i), CultureInfo.InvariantCulture);
                                if (everyNthWeek < 1 || everyNthWeek > 5)
                                {
                                    throw new Exception();
                                }
                            }
                            catch (Exception)
                            {
                                throw new FormatException(
                                    "A numeric value between 1 and 5 must follow the '/' option");
                            }
                        }
                        else if (c == 'L')
                        {
                            lastdayOfWeek = true;
                            i++;
                        }
                        else
                        {
                            throw new FormatException($"Illegal characters for this position: '{sub}'");
                        }
                    }
                }
                else
                {
                    throw new FormatException($"Illegal characters for this position: '{sub}'");
                }
                if (eval != -1)
                {
                    incr = 1;
                }
                AddToSet(sval, eval, incr, type);
                return i + 3;
            }

            if (c == '?')
            {
                i++;
                if (i + 1 < s.Length && s[i] != ' ' && s[i + 1] != '\t')
                {
                    throw new FormatException("Illegal character after '?': "
                                              + s[i]);
                }
                if (type != DayOfWeek && type != DayOfMonth)
                {
                    throw new FormatException(
                        "'?' can only be specified for Day-of-Month or Day-of-Week.");
                }
                if (type == DayOfWeek && !lastdayOfMonth)
                {
                    int val = daysOfMonth.LastOrDefault();
                    if (val == NoSpecInt)
                    {
                        throw new FormatException(
                            "'?' can only be specified for Day-of-Month -OR- Day-of-Week.");
                    }
                }

                AddToSet(NoSpecInt, -1, 0, type);
                return i;
            }

            var startsWithAsterisk = c == '*';
            if (startsWithAsterisk || c == '/')
            {
                if (startsWithAsterisk && i + 1 >= s.Length)
                {
                    AddToSet(AllSpecInt, -1, incr, type);
                    return i + 1;
                }
                if (c == '/' && (i + 1 >= s.Length || s[i + 1] == ' ' || s[i + 1] == '\t'))
                {
                    throw new FormatException("'/' must be followed by an integer.");
                }
                if (startsWithAsterisk)
                {
                    i++;
                }
                c = s[i];
                if (c == '/')
                {
                    // is an increment specified?
                    i++;
                    if (i >= s.Length)
                    {
                        throw new FormatException("Unexpected end of string.");
                    }

                    incr = GetNumericValue(s, i);

                    i++;
                    if (incr > 10)
                    {
                        i++;
                    }
                    CheckIncrementRange(incr, type);
                }
                else
                {
                    if (startsWithAsterisk)
                    {
                        // invalid value s
                        throw new FormatException("Illegal characters after asterisk: " + s);
                    }
                    incr = 1;
                }

                AddToSet(AllSpecInt, -1, incr, type);
                return i;
            }
            if (c == 'L')
            {
                i++;
                if (type == DayOfMonth)
                {
                    lastdayOfMonth = true;
                }
                if (type == DayOfWeek)
                {
                    AddToSet(7, 7, 0, type);
                }
                if (type == DayOfMonth && s.Length > i)
                {
                    c = s[i];
                    if (c == '-')
                    {
                        ValueSet vs = GetValue(0, s, i + 1);
                        lastdayOffset = vs.theValue;
                        if (lastdayOffset > 30)
                        {
                            throw new FormatException("Offset from last day must be <= 30");
                        }
                        i = vs.pos;
                    }
                    if (s.Length > i)
                    {
                        c = s[i];
                        if (c == 'W')
                        {
                            nearestWeekday = true;
                            i++;
                        }
                    }
                }
                return i;
            }
            if (c >= '0' && c <= '9')
            {
                int val = Convert.ToInt32(c.ToString(), CultureInfo.InvariantCulture);
                i++;
                if (i >= s.Length)
                {
                    AddToSet(val, -1, -1, type);
                }
                else
                {
                    c = s[i];
                    if (c >= '0' && c <= '9')
                    {
                        ValueSet vs = GetValue(val, s, i);
                        val = vs.theValue;
                        i = vs.pos;
                    }
                    i = CheckNext(i, s, val, type);
                    return i;
                }
            }
            else
            {
                throw new FormatException($"Unexpected character: {c}");
            }

            return i;
        }

4.Cron表達式詳解

Cron表達式是一個字符串,字符串以5或6個空格隔開,分為6或7個域,每一個域代表一個含義,Cron有如下兩種語法格式:

  (1) Seconds Minutes Hours DayofMonth Month DayofWeek Year

  (2)Seconds Minutes Hours DayofMonth Month DayofWeek

  

  一、結構

  corn從左到右(用空格隔開):秒 分 小時 月份中的日期 月份 星期中的日期 年份

  二、各字段的含義

 
字段 允許值 允許的特殊字符
秒(Seconds) 0~59的整數 , - * /    四個字符
分(Minutes 0~59的整數 , - * /    四個字符
小時(Hours 0~23的整數 , - * /    四個字符
日期(DayofMonth 1~31的整數(但是你需要考慮你月的天數) ,- * ? / L W C     八個字符
月份(Month 1~12的整數或者 JAN-DEC , - * /    四個字符
星期(DayofWeek 1~7的整數或者 SUN-SAT (1=SUN) , - * ? / L C #     八個字符
年(可選,留空)(Year 1970~2099 , - * /    四個字符

 

 

 

 

 

 

 

  

 

 

 

注意事項:

  每一個域都使用數字,但還可以出現如下特殊字符,它們的含義是:

  (1)*:表示匹配該域的任意值。假如在Minutes域使用*, 即表示每分鍾都會觸發事件。

  (2)?:只能用在DayofMonth和DayofWeek兩個域。它也匹配域的任意值,但實際不會。因為DayofMonth和DayofWeek會相互影響。例如想在每月的20日觸發調度,不管20日到底是星期幾,則只能使用如下寫法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期幾都會觸發,實際上並不是這樣。

  (3)-:表示范圍。例如在Minutes域使用5-20,表示從5分到20分鍾每分鍾觸發一次 

  (4)/:表示起始時間開始觸發,然后每隔固定時間觸發一次。例如在Minutes域使用5/20,則意味着5分鍾觸發一次,而25,45等分別觸發一次. 

  (5),:表示列出枚舉值。例如:在Minutes域使用5,20,則意味着在5和20分每分鍾觸發一次。 

  (6)L:表示最后,只能出現在DayofWeek和DayofMonth域。如果在DayofWeek域使用5L,意味着在最后的一個星期四觸發。 

  (7)W:表示有效工作日(周一到周五),只能出現在DayofMonth域,系統將在離指定日期的最近的有效工作日觸發事件。例如:在 DayofMonth使用5W,如果5日是星期六,則將在最近的工作日:星期五,即4日觸發。如果5日是星期天,則在6日(周一)觸發;如果5日在星期一到星期五中的一天,則就在5日觸發。另外一點,W的最近尋找不會跨過月份 。

  (8)LW:這兩個字符可以連用,表示在某個月最后一個工作日,即最后一個星期五。 

  (9)#:用於確定每個月第幾個星期幾,只能出現在DayofMonth域。例如在4#2,表示某月的第二個星期三。

  三、常用表達式例子

  (1)0 0 2 1 * ? *   表示在每月的1日的凌晨2點調整任務

  (2)0 15 10 ? * MON-FRI   表示周一到周五每天上午10:15執行作業

  (3)0 15 10 ? 6L 2002-2006   表示2002-2006年的每個月的最后一個星期五上午10:15執行作

  (4)0 0 10,14,16 * * ?   每天上午10點,下午2點,4點 

  (5)0 0/30 9-17 * * ?   朝九晚五工作時間內每半小時 

  (6)0 0 12 ? * WED    表示每個星期三中午12點 

  (7)0 0 12 * * ?   每天中午12點觸發 

  (8)0 15 10 ? * *    每天上午10:15觸發 

  (9)0 15 10 * * ?     每天上午10:15觸發 

  (10)0 15 10 * * ? *    每天上午10:15觸發 

  (11)0 15 10 * * ? 2005    2005年的每天上午10:15觸發 

  (12)0 * 14 * * ?     在每天下午2點到下午2:59期間的每1分鍾觸發 

  (13)0 0/5 14 * * ?    在每天下午2點到下午2:55期間的每5分鍾觸發 

  (14)0 0/5 14,18 * * ?     在每天下午2點到2:55期間和下午6點到6:55期間的每5分鍾觸發 

  (15)0 0-5 14 * * ?    在每天下午2點到下午2:05期間的每1分鍾觸發 

  (16)0 10,44 14 ? 3 WED    每年三月的星期三的下午2:10和2:44觸發 

  (17)0 15 10 ? * MON-FRI    周一至周五的上午10:15觸發 

  (18)0 15 10 15 * ?    每月15日上午10:15觸發 

  (19)0 15 10 L * ?    每月最后一日的上午10:15觸發 

  (20)0 15 10 ? * 6L    每月的最后一個星期五上午10:15觸發 

  (21)0 15 10 ? * 6L 2002-2005   2002年至2005年的每月的最后一個星期五上午10:15觸發 

  (22)0 15 10 ? * 6#3   每月的第三個星期五上午10:15觸發

 

  

  注:

  (1)有些子表達式能包含一些范圍或列表

  例如:子表達式(天(星期))可以為 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”

“*”字符代表所有可能的值

  因此,“*”在子表達式(月)里表示每個月的含義,“*”在子表達式(天(星期))表示星期的每一天


  “/”字符用來指定數值的增量 
  例如:在子表達式(分鍾)里的“0/15”表示從第0分鍾開始,每15分鍾 
在子表達式(分鍾)里的“3/20”表示從第3分鍾開始,每20分鍾(它和“3,23,43”)的含義一樣


  “?”字符僅被用於天(月)和天(星期)兩個子表達式,表示不指定值 
  當2個子表達式其中之一被指定了值以后,為了避免沖突,需要將另一個子表達式的值設為“?”

  “L” 字符僅被用於天(月)和天(星期)兩個子表達式,它是單詞“last”的縮寫 
  但是它在兩個子表達式里的含義是不同的。 
  在天(月)子表達式中,“L”表示一個月的最后一天 
  在天(星期)自表達式中,“L”表示一個星期的最后一天,也就是SAT

  如果在“L”前有具體的內容,它就具有其他的含義了

  例如:“6L”表示這個月的倒數第6天,“FRIL”表示這個月的最一個星期五 
  注意:在使用“L”參數時,不要指定列表或范圍,因為這會導致問題

 官方網站:https://quartznet.sourceforge.io/apidoc/3.0/html/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM