一、Quartz.Net是什么?
Quartz.Net是一個定時任務框架
二、有Timer了,為什么需要用Quartz.Net?
Quartz.Net比Timer使用起來更靈活。例如:每個月最后一個星期天23:59分做什么事情,而Timer實現起來就會麻煩很多。
三、Quartz.Net怎么用?
《一、無配置實現》
1. 下載引用3個DLL:Quartz.dll、Common.Logging.dll、Common.Logging.Core.dll
下載地址:http://pan.baidu.com/s/1eRC5Iwi
2.創建任務操作類:
1 public class TimingJob : IJob 2 { 3 public void Execute(IJobExecutionContext context) 4 { 5 //將要定時執行的邏輯代碼寫於此處 6 Console.WriteLine(DateTime.Now.ToString()+"\t工作太久了,休息10分鍾吧。"); 7 } 8 }
3.創建處理類、觸發器類
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 ISchedulerFactory sf = new StdSchedulerFactory(); 6 IScheduler scheduler = sf.GetScheduler(); //創建調度實例 7 //創建任務實例 8 IJobDetail job = JobBuilder.Create<TimingJob>().WithIdentity(new JobKey("job1")).Build(); 9 //創建觸發器實例 10 ITrigger trigger = TriggerBuilder.Create().StartAt(DateTime.Now.AddSeconds(0)).WithCronSchedule("59 59 23 ? * 1L ").Build(); 11 scheduler.ScheduleJob(job, trigger); //綁定觸發器和任務 12 scheduler.Start(); //啟動監控 13 } 14 }
《二、通過配置文件實現》
1. 下載引用3個DLL:Quartz.dll、Common.Logging.dll、Common.Logging.Core.dll
下載地址:http://pan.baidu.com/s/1eRC5Iwi
2. 創建工作類:
1 public class PrintTimeJob : IJob 2 { 3 public void Execute(IJobExecutionContext context) 4 { 5 Console.WriteLine(DateTime.Now.ToString()+"工作了很久,休息一會吧!"); 6 } 7 }
3. 工作類、觸發器類的配置文件(quartz_jobs.xml)
1 <?xml version="1.0" encoding="utf-8" ?> 2 <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> 3 <processing-directives> 4 <overwrite-existing-data>true</overwrite-existing-data> 5 </processing-directives> 6 <schedule> <!--工作類配置--> 7 <job> 8 <name>PrintTimeJob</name> 9 <group>PrintGroup</group> 10 <description>RecyclingResourcesJob</description> 11 <job-type>SettingMothod.PrintTimeJob, SettingMothod</job-type> 12 <durable>true</durable> 13 <recover>false</recover> 14 </job> 15 <trigger> <!--觸發器類配置--> 16 <cron> 17 <name>PrintDateTimeTrigger</name> 18 <group>PrintDateTimeTriggerGroup</group> 19 <job-name>PrintTimeJob</job-name> 20 <job-group>PrintGroup</job-group> 21 <cron-expression>59 59 23 ? * 1L </cron-expression> // 22 </cron> 23 </trigger> 24 </schedule> 25 </job-scheduling-data>
4. 設置配置文件(App.config)
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <section name="quartz" type="System.Configuration.NameValueSectionHandler"/> 5 </configSections> 6 <quartz> 7 <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/> 8 <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/> 9 <add key="quartz.threadPool.threadCount" value="10"/> 10 <add key="quartz.threadPool.threadPriority" value="2"/> 11 <add key="quartz.jobStore.misfireThreshold" value="60000"/> 12 <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/> 13 <!--******************************Plugin配置*********************************************--> 14 <add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" /> 15 <add key="quartz.plugin.xml.fileNames" value="~/quartz_jobs.xml"/> 16 </quartz> 17 </configuration>
5. 實例化、啟動任務調度。
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 ISchedulerFactory sf = new StdSchedulerFactory(); 6 IScheduler sched = sf.GetScheduler(); //創建調度實例 7 sched.Start(); //開啟調度 8 Console.WriteLine("啟動成功 "); 9 Console.ReadKey(); 10 } 11 }
初次寫博客,不足之處請多多指教。
本文源碼:http://pan.baidu.com/s/1qXS0vUG