1. 程式人生 > >Quartz.Net定時任務簡單實用(例項)

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 }
複製程式碼