作業排程系統quartz.net
任務排程在我們日常開發過程中非常常見,比如:每天晚上0點自動執行某某操作;每週三晚上2點執行某某操作;......當然,我們處理這類問題的方法也有很多,比如:sql的自動任務;windows上建立任務計劃;寫windows服務等等。如果系統比較複雜,相互呼叫比較頻繁,任務非常多,幾百上千條甚至上萬條,那麼本身對任務的管理就是比較昂貴的代價;如何提高任務的高可用?任務的測試是否便捷等等問題就會出現。上述的方案是否還能從容應對?
這時我們就迫切地需要一個作業排程系統來處理這些場景。
Quartz.NET是一個強大、開源、輕量的作業排程框架,是 OpenSymphony 的 Quartz API 的.NET移植,它有很多特徵,如:資料庫支援,叢集,外掛,支援cron-like表示式等等。官方網址:https://www.quartz-scheduler.net;GitHub地址:
https://github.com/quartznet/quartznet,各種用法可以參考示例程式。
但如果想方便的知道某個作業執行情況,需要暫停,啟動等操作行為,這時候就需要個Job管理的介面。CrystalQuartz可以實現遠端管理。
多數系統都會涉及到“後臺服務”的開發,一般是為了排程一些自動執行的任務或從佇列中消費一些訊息,開發 windows service 有一點不爽的是:除錯麻煩,當然你還需要知道 windows service 相關的一些開發知識(也不難),TopShelf框架,可以你讓 console application 封裝為 windows service,這樣你就非常方便的開發和除錯 windows service。TopShelf框架的官網為Url:http://topshelf-project.com
用TopShelf和quartz.net編寫任務,CrystalQuartz管理任務。本文就是搭建一個簡易的任務排程方案,啟動任務排程、新增Job、移除Job、遠端管理等。
1、建立一個名為TaskScheduling的Asp.net MVC專案
2、新建一個類RemoteServerExample,主要程式碼:
1 //ILog log = LogManager.GetLogger(typeof(RemoteServerExample)); 2 3 NameValueCollection properties = new NameValueCollection(); 4 properties["quartz.scheduler.instanceName"] = "RemoteAutoTaskServer"; 5 6 properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"; 7 properties["quartz.threadPool.threadCount"] = "5"; 8 properties["quartz.threadPool.threadPriority"] = "Normal"; 9 10 // 設定伺服器 11 properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz"; 12 properties["quartz.scheduler.exporter.port"] = "555"; 13 properties["quartz.scheduler.exporter.bindName"] = "QuartzScheduler"; 14 properties["quartz.scheduler.exporter.channelType"] = "tcp"; 15 16 #region 2.3.0版本可用 17 18 //properties["quartz.scheduler.exporter.channelName"] = "httpQuartz"; 19 // 拒絕遠端 20 //properties["quartz.scheduler.exporter.rejectRemoteRequests"] = "true"; 21 22 #endregion 2.3.0版本可用 23 24 #region 持久化所用 25 26 //儲存型別 27 //properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz"; 28 ////表明字首 29 //properties["quartz.jobStore.tablePrefix"] = "QRTZ_"; 30 ////驅動型別 31 //properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz"; 32 ////資料來源名稱 33 //properties["quartz.jobStore.dataSource"] = "myDS"; 34 ////連線字串 35 //properties["quartz.dataSource.myDS.connectionString"] = @"Data Source=(local);Initial Catalog=quartz;User ID=sa;Password=Ayy123"; 36 ////sqlserver版本 37 //properties["quartz.dataSource.myDS.provider"] = "SqlServer-20"; 38 39 #endregion 持久化所用 40 41 ISchedulerFactory sf = new StdSchedulerFactory(properties); 42 IScheduler sched = sf.GetScheduler(); 43 sched.Start(); 44 45 SchedulerMetaData metaData = sched.GetMetaData(); 46 47 IJobDetail job = JobBuilder.Create<SimpleJob>() 48 .WithIdentity("remotelyAddedJob", "default") 49 .Build(); 50 51 JobDataMap map = job.JobDataMap; 52 map.Put("msg", "資訊"); 53 54 ITrigger trigger = TriggerBuilder.Create() 55 .WithIdentity("remotelyAddedTrigger", "default") 56 .ForJob(job.Key) 57 .WithCronSchedule("/5 * * ? * *") 58 .Build(); 59 60 string name = sched.SchedulerName; 61 sched.ScheduleJob(job, trigger);View Code
3、新建一個類RemoteClientExample,主要程式碼:
1 ILog log = LogManager.GetLogger(typeof(RemoteClientExample)); 2 3 NameValueCollection properties = new NameValueCollection(); 4 properties["quartz.scheduler.instanceName"] = "RemoteClient"; 5 6 // 設定執行緒池 7 properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"; 8 properties["quartz.threadPool.threadCount"] = "5"; 9 properties["quartz.threadPool.threadPriority"] = "Normal"; 10 11 // 設定遠端連線 12 //properties["quartz.scheduler.proxy"] = "true"; 13 //properties["quartz.scheduler.proxy.address"] = "tcp://127.0.0.1:556/QuartzScheduler"; 14 15 ISchedulerFactory sf = new StdSchedulerFactory(properties); 16 IScheduler scheduler = sf.GetScheduler("RemoteAutoTaskServer"); 17 18 IJobDetail job = JobBuilder.Create<SimpleJob>() 19 .WithIdentity("remotelyAddedJob1", "default") 20 .Build(); 21 22 JobDataMap map = job.JobDataMap; 23 map.Put("msg", "資訊"); 24 25 ITrigger trigger = TriggerBuilder.Create() 26 .WithIdentity("remotelyAddedTrigger1", "default") 27 .ForJob(job.Key) 28 .WithCronSchedule("/5 * * ? * *") 29 .Build(); 30 31 string name = scheduler.SchedulerName; 32 scheduler.ScheduleJob(job, trigger); 33 34 log.Info("向伺服器新增計劃任務");View Code
4、SimpleJob程式碼:
1 [PersistJobDataAfterExecution] 2 [DisallowConcurrentExecution] 3 public class SimpleJob : IJob 4 { 5 public const string Message = "msg"; 6 7 public virtual void Execute(IJobExecutionContext context) 8 { 9 try 10 { 11 JobKey jobKey = context.JobDetail.Key; 12 string message = context.JobDetail.JobDataMap.GetString(Message); 13 14 string strLog = String.Format("{0} 執行時間 {1}", jobKey, DateTime.Now.ToString()); 15 } 16 catch (Exception ex) 17 { 18 } 19 } 20 }View Code
5、程式執行介面:
點選啟動排程器,啟動伺服器監聽且預設有一個名為remotelyAddedJob的job,並且允許遠端管理;點選新增任務,會加入一個名為remotelyAddedJob1的job;刪除任務會刪除所有job。
如果我們已經啟動了排程器,有一個任務,比如,我們用TopShelf和quartz.net寫了一個windows服務,在本地執行,如果要加入到遠端管理中,怎麼辦呢?
可以按照上述RemoteClientExample程式碼,配置properties,呼叫的時候通過反射的方式獲取,然後加入到排程器中,進行遠端管理。
quartz.net還有持久化、叢集等等特性,提高任務的高可用。
分享一個corn表示式生成器:https://files.cnblogs.com/files/net-yuan/CronExpBuilder.zip
大神張善友 的quartz.net系列:https://www.cnblogs.com/shanyou/category/102991.html