任務調度架構-Quartz.Net

來源:互聯網
上載者:User

標籤:

使用Quartz.Net依賴於以下3個組件:Common.Logging.dll、Common.Logging.Core.dll、Quartz.dll

簡單封裝

 1 using Quartz; 2 using Quartz.Impl; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8  9 namespace Test10 {11     public class SchedulerHelper12     {13         /// <summary>14         /// 任務調度器執行個體15         /// </summary>16         public static IScheduler scheduler { get; }17         static SchedulerHelper()18         {19             //執行個體化任務調度器對象20             scheduler = StdSchedulerFactory.GetDefaultScheduler();21         }22 23         /// <summary>24         /// 新增工作25         /// </summary>26         /// <typeparam name="T">IJob類型</typeparam>27         /// <param name="jobIdentity">job唯一標識</param>28         /// <param name="triggerIdentity">trigger唯一標識</param>29         /// <param name="cronExpression">cron運算式</param>30         /// <param name="jobGroup">任務所屬分組[可選]</param>31         /// <param name="triggerGroup">觸發器所屬分組[可選]</param>32         public static void AddScheduleJob<T>(string jobIdentity, string triggerIdentity, string cronExpression, string jobGroup = "", string triggerGroup = "")33             where T : IJob34         {35             var jobBuilder = JobBuilder.Create<T>();36             if (string.IsNullOrEmpty(jobGroup))37                 jobBuilder.WithIdentity(jobIdentity);38             else39                 jobBuilder.WithIdentity(jobIdentity, jobGroup);40 41             var currentJob = jobBuilder.Build();42 43             var triggerBuilder = TriggerBuilder.Create();44             if (string.IsNullOrEmpty(triggerGroup))45                 triggerBuilder.WithIdentity(triggerIdentity);46             else47                 triggerBuilder.WithIdentity(triggerIdentity, triggerGroup);48 49             var currentTrigger = triggerBuilder50                                 .StartNow()51                                 .WithCronSchedule(cronExpression)52                                 .Build();53 54             //把作業,觸發器加入調度器。55             scheduler.ScheduleJob(currentJob, currentTrigger);56         }57 58         public static void Start()59         {60             if (scheduler == null)61                 throw new ArgumentNullException("scheduler", "任務調度器執行個體對象不能為null");62             if (!scheduler.IsStarted)63                 scheduler.Start();64         }65 66         public static void ShutDown()67         {68             if (scheduler == null)69                 throw new ArgumentNullException("scheduler", "任務調度器執行個體對象不能為null");70             if (!scheduler.IsShutdown)71                 scheduler.Shutdown();72         }73     }74 }

用戶端

using NLog;using Quartz;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Test{    public class WriteLogJob : IJob    {        ILogger log = LogManager.GetLogger("admin");        public void Execute(IJobExecutionContext context)        {            try            {                log.Error(string.Format("目前時間為:{0}", DateTime.Now));            }            catch (Exception x) { }        }    }}
 1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Linq; 5 using System.Text; 6  7 namespace Test 8 { 9     class Program10     {11         static void Main(string[] args)12         {13             SchedulerHelper.AddScheduleJob<WriteLogJob>("WriteLogJob", "WriteLogTrigger", "0/5 * * * * ?");14 15             SchedulerHelper.Start();16 17             Console.ReadLine();18         }19     }20 }
Quartz的cron運算式

官文文檔:cron運算式介紹

由7段構成:秒 分 時 日 月 星期 年(可選)
"-" :表示範圍  MON-WED表示星期一到星期三
"," :表示列舉 MON,WEB表示星期一和星期三
"*" :表是“每”,每月,每天,每周,每年等
"/" :表示增量:0/15(處於分鐘段裡面) 每15分鐘,在0分以後開始,3/20 每20分鐘,從3分鐘以後開始
"?" :只能出現在日,星期段裡面,表示不指定具體的值
"L" :只能出現在日,星期段裡面,是Last的縮寫,一個月的最後一天,一個星期的最後一天(星期六)
"W" :表示工作日,距離給定值最近的工作日
"#" :表示一個月的第幾個星期幾,例如:"6#3"表示每個月的第三個星期五(1=SUN...6=FRI,7=SAT)

常用cron運算式樣本
// 0/5 * * * * ?            每5秒鐘執行一次// 0 0/5 * * * ?            每5分鐘執行一次// 10 0/5 * * * ?           每5分鐘,在第10秒鐘執行一次// 0 30 10-13 ? * WED,FRI   每周的周三、周五,時針為10-13,分鐘刻度為30的時候執行一次// 0 0/30 8-9 5,20 * ?      每個月的第5、第20天,時針為8到9,分鐘刻度為從0開始,每過30分鐘執行一次
官方執行個體
Expression Meaning
0 0 12 * * ? Fire at 12pm (noon) every day
0 15 10 ? * * Fire at 10:15am every day
0 15 10 * * ? Fire at 10:15am every day
0 15 10 * * ? * Fire at 10:15am every day
0 15 10 * * ? 2005 Fire at 10:15am every day during the year 2005
0 * 14 * * ? Fire every minute starting at 2pm and ending at 2:59pm, every day
0 0/5 14 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
0 0/5 14,18 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
0 0-5 14 * * ? Fire every minute starting at 2pm and ending at 2:05pm, every day
0 10,44 14 ? 3 WED Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
0 15 10 ? * MON-FRI Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 * ? Fire at 10:15am on the 15th day of every month
0 15 10 L * ? Fire at 10:15am on the last day of every month
0 15 10 L-2 * ? Fire at 10:15am on the 2nd-to-last last day of every month
0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L 2002-2005 Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
0 15 10 ? * 6#3 Fire at 10:15am on the third Friday of every month
0 0 12 1/5 * ? Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.
0 11 11 11 11 ? Fire every November 11th at 11:11am.

任務調度架構-Quartz.Net

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.