Quartz.Net使用
標籤:Quartz.Nethtml
在最近工做中,須要在不一樣時間及不一樣條件下定時發送通知及消息,最初使用 System.Timers.Timer
實現。雖然使用簡單,隨着須要定時處理的任務增多,考慮到 System.Timers.Timer
以下缺點:數據庫
- Timer沒有持久化機制;
- Timer的調度沒有彈性,僅能定時觸發,不可自由配置觸發時間點;
- Timer不能利用線程池,每個Timer須要開啓一個線程;
- 每一個Timer中的任務爲串行執行,同一時間只能有一個任務在執行。前一個任務執行失敗影響後面任務的執行;
而Quartz.Net
的如下優勢可解決以上問題app
- 使用靈活,有多種使用方式(
eg
XML
配置任務,及代碼實現),且能夠混合使用;- 支持集羣,做業分組,及做業遠程管理;
- 可自定義精細的時間管理;
- 支持數據庫,可持久化。
安裝
- 經過
NuGet
安裝Quartz.Net
說明
- 每個任務須要繼承接口
IJob
,並實現Execute(IJobExecutionContext context)
,該方法爲每一個Job
須要作的具體處理;
代碼示例
- 代碼建立job示例
private static void Main(string[] args) { try { WatchFile(); var fileName = Path.Combine(Environment.CurrentDirectory, "log4config.config"); XmlConfigurator.ConfigureAndWatch(new FileInfo(fileName)); StdSchedulerFactory factory = new StdSchedulerFactory(); IScheduler scheduler = factory.GetScheduler(); // 啓動任務 scheduler.Start(); // 建立Job,可經過JobDataMap傳遞數據 IJobDetail job = JobBuilder.Create<HelloJob>() .WithIdentity("job1", "group1") .UsingJobData("jobSays", "Hello World!") .UsingJobData("myFloatValue", 3.141f) .Build(); //建立trigger ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartNow() .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()) .Build(); //把job,trigger加入調度器 scheduler.ScheduleJob(job, trigger); //關閉調度器 scheduler.Shutdown(); } catch (Exception ex) { LogHelper.Error("Main" + ex); } Console.WriteLine("Press any key to close the application"); Console.ReadKey(); } public class HelloJob : IJob { public void Execute(IJobExecutionContext context) { JobKey key = context.JobDetail.Key; //獲取執行的數據 JobDataMap dataMap = context.JobDetail.JobDataMap; string jobSays = dataMap.GetString("jobSays"); float myFloatValue = dataMap.GetFloat("myFloatValue"); Console.Error.WriteLineAsync("Instance " + key + " of DumbJob says: " + jobSays + ", and val is: " + myFloatValue); } } pub
XML
方式配置Job、Trigger
代碼示例 1) 在quartz_jobs.xml
爲配置Job
及Trigger
的文件 配置文件示例:
<?xml version="1.0" encoding="utf-8" ?> <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> <processing-directives> <overwrite-existing-data>true</overwrite-existing-data> </processing-directives> <schedule> <job> <name>CommonIMRemindJob</name> <group>IMRemindJob</group> <description>IMRemindJob</description> <job-type>NXIN.Qlw.JobServices.CommonIMRemindJobService,NXIN.Qlw.JobServices</job-type> <durable>true</durable> <recover>true</recover> </job> <trigger> <simple> <name>CommonIMRemindTrigger</name> <group>IMRemindJob</group> <description>CommonIMRemindTrigger</description> <job-name>CommonIMRemindJob</job-name> <job-group>IMRemindJob</job-group> <misfire-instruction>SmartPolicy</misfire-instruction> <repeat-count>-1</repeat-count> <repeat-interval>300000</repeat-interval> </simple> </trigger> </schedule> </job-scheduling-data>
2)實現示例
private static void InitScheduler() { try { NameValueCollection props = new NameValueCollection { { "quartz.serializer", "binary" } }; XMLSchedulingDataProcessor processor = new XMLSchedulingDataProcessor(new SimpleTypeLoadHelper()); StdSchedulerFactory factory = new StdSchedulerFactory(props); IScheduler scheduler = factory.GetScheduler(); processor.ProcessFileAndScheduleJobs("~/quartz_jobs.xml", scheduler); scheduler.Start(); } catch (SchedulerException se) { LogHelper.Error("/Main/RunProgramRunExample" + se); } }
參考博客:[蘑菇先生Net做業調度系列][1] [1]: http://www.cnblogs.com/mushroom/p/4850115.htmlui