作業調度系統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

GitHub:https://github.com/net-yuan/TaskScheduling

 


免責聲明!

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



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