Quartz.NET管理類

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   使用   for   sp   div   

最近做項目設計到Quartz.NET,寫了一個Quartz.NET管理類,在此記錄下。

  1     public class QuartzManager<T> where T : class,IJob  2     {  3         #region 變數  4         private static ISchedulerFactory schedulerFactory = new StdSchedulerFactory();  //scheduler工廠  5         private static String JOB_GROUP_NAME = "JOBGROUP_NAME"; //Job群組名  6         private static String TRIGGER_GROUP_NAME = "TRIGGERGROUP_NAME"; //觸發器群組名  7         #endregion  8   9         #region 添加,刪除,修改Job方法 10         /// <summary> 11         /// 添加一個定時任務,使用預設的工作群組名,觸發器名,觸發器組名  12         /// </summary> 13         /// <param name="pStrJobName">任務名</param> 14         /// <param name="pStrCronExpress">觸發器運算式</param> 15         public static void addJob(string pStrJobName, string pStrCronExpress,IDictionary<string,object> pDictionary) 16         { 17             try 18             { 19                 IScheduler sched = schedulerFactory.GetScheduler(); 20                 // 建立任務 21                 IJobDetail job = JobBuilder.Create<T>() 22                     .WithIdentity(pStrJobName, JOB_GROUP_NAME) 23                     .Build(); 24  25                 // 建立觸發器 26                 ITrigger trigger = TriggerBuilder.Create() 27                     .WithIdentity(pStrJobName, TRIGGER_GROUP_NAME) 28                     .WithCronSchedule(pStrCronExpress) 29                     .Build(); 30  31                 //給任務傳參數 32                 foreach (KeyValuePair<string, object> kvp in pDictionary) 33                 { 34                     job.JobDataMap.Put(kvp.Key, kvp.Value); 35                 } 36  37                 sched.ScheduleJob(job, trigger); 38             } 39             catch (Exception e) 40             { 41                 throw new Exception(e.Message); 42             } 43         } 44  45         /// <summary> 46         /// 移除一個任務(使用預設的工作群組名,觸發器名,觸發器組名)  47         /// </summary> 48         /// <param name="pStrJobName">任務名稱</param> 49         public static void RemoveJob(string pStrJobName) 50         { 51             try 52             { 53                 IScheduler sched = schedulerFactory.GetScheduler(); 54                 JobKey jobKey = new JobKey(pStrJobName); 55                 TriggerKey triggerKey = new TriggerKey(pStrJobName, TRIGGER_GROUP_NAME); 56                 sched.PauseTrigger(triggerKey);// 停止觸發器   57                 sched.UnscheduleJob(triggerKey);// 移除觸發器   58                 sched.DeleteJob(jobKey);// 刪除任務   59             } 60             catch (Exception e) 61             { 62                 throw new Exception(e.Message); 63             } 64         } 65  66         /// <summary> 67         /// 修改一個任務的觸發時間(使用預設的工作群組名,觸發器名,觸發器組名)  68         /// </summary> 69         /// <param name="pStrJobName">任務名</param> 70         /// <param name="pStrCronExpress">觸發器運算式</param> 71         public static void ModifyJobTime(string pStrJobName, string pStrCronExpress,IDictionary<string,object> pDictionary) 72         { 73             try 74             { 75                 IScheduler sched = schedulerFactory.GetScheduler(); 76                 TriggerKey triggerKey = new TriggerKey(pStrJobName, TRIGGER_GROUP_NAME); 77                 ICronTrigger trigger = (ICronTrigger)sched.GetTrigger(triggerKey); 78                 if (trigger == null) 79                 { 80                     return; 81                 } 82                 RemoveJob(pStrJobName); 83                 addJob(pStrJobName, pStrCronExpress,pDictionary); 84             } 85             catch (Exception e) 86             { 87                 throw new Exception(e.Message); 88             } 89         } 90         #endregion 91  92         #region 啟動,關閉Job 93         /// <summary> 94         /// 啟動所有定時任務  95         /// </summary> 96         public static void startJobs() 97         { 98             try 99             {100                 IScheduler sched = schedulerFactory.GetScheduler();101                 sched.Start();102             }103             catch (Exception e)104             {105                 throw new Exception(e.Message);106             }107         } 108 109         /// <summary>110         /// 關閉所有定時任務111         /// </summary>112         public static void ShutdownJobs()113         {114             try115             {116                 IScheduler sched = schedulerFactory.GetScheduler();117                 if (!sched.IsShutdown)118                 {119                     sched.Shutdown();120                 }121             }122             catch (Exception e)123             {124                 throw new Exception(e.Message);125             }126         }  127         #endregion128 129     }

 

調用方法也很簡單

1 QuartzManager<MyJob>.addJob(jobnName, cronExpress, dictionary);2 3 QuartzManager<MyJob>.startJobs();

 

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.