最近做項目設計到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 try 115 { 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 #endregion 128 129 }
調用方法也很簡單
1 QuartzManager<MyJob>.addJob(jobnName, cronExpress, dictionary); 2 3 QuartzManager<MyJob>.startJobs();
有兩個DLL引用到項目中,Quartz.dll ,Common.Logging.dll
在項目中就可以使用 任務調度框架了。
並非沒有優秀的類庫,因為JAVA比.NET的歷史長得多,一些好的,穩定的直接移植非常好,何必重復造輪子?