C#使用Quartz.NET詳解

 

  Quartz.NET是一個開源的做業調度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#寫成,可用於winform和asp.net應用中。它提供了巨大的靈活性而不犧牲簡單性。你可以用它來爲執行一個做業而建立簡單的或複雜的調度。它有不少特徵,如:數據庫支持,集羣,插件,支持cron-like表達式等等。html

你曾經須要應用執行一個任務嗎?這個任務天天或每週星期二晚上11:30,或許僅僅每月的最後一天執行。一個自動執行而無須干預的任務在執行過程當中若是發生一個嚴重錯誤,應用可以知到其執行失敗並嘗試從新執行嗎?你和你的團隊是用.NET編程嗎?若是這些問題中任何一個你回答是,那麼你應該使用Quartz.Net調度器。 Quartz.NET容許開發人員根據時間間隔(或天)來調度做業。它實現了做業和觸發器的多對多關係,還能把多個做業與不一樣的觸發器關聯。整合了 Quartz.NET的應用程序能夠重用來自不一樣事件的做業,還能夠爲一個事件組合多個做業.mysql

Quartz.NET入門sql

要開始使用 Quartz.NET,須要用 Quartz.NET API 對項目進行配置。步驟以下:數據庫

1. 到http://quartznet.sourceforge.net/download.html下載 Quartz.NET API,最新版本是0.6express

2. 解壓縮Quartz.NET-0.6.zip 到目錄,根據你的項目狀況用Visual Studio 2003或者Visual Studio 2005打開相應工程,編譯。你能夠將它放進本身的應用中。Quartz.NET框架只須要少數的第三方庫,而且這些三方庫是必需的,你極可能已經在使用這些庫了。編程

3. 在Quartz.NET有一個叫作quartz.properties的配置文件,它容許你修改框架運行時環境。缺省是使用Quartz.dll裏面的quartz.properties文件。固然你能夠在應用程序配置文件中作相應的配置,下面是一個配置文件示例:安全

 

[c-sharp]  view plain  copy
 
  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <configuration>   
  3. <configSections>   
  4. <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />   
  5. </configSections>   
  6.   
  7. <quartz>   
  8.   
  9. <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler" />   
  10. <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />   
  11. <add key="quartz.threadPool.threadCount" value="10" />   
  12. <add key="quartz.threadPool.threadPriority" value="2" />   
  13. <add key="quartz.jobStore.misfireThreshold" value="60000" />   
  14. <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />   
  15.   
  16. </quartz>   
  17. </configuration>   

 

 

爲了方便讀者,咱們使用Quartz.NET的例子代碼來解釋,如今來看一下 Quartz API 的主要組件。多線程

調度器和做業架構

Quartz.NET框架的核心是調度器。調度器負責管理Quartz.NET應用運行時環境。調度器不是靠本身作全部的工做,而是依賴框架內一些很是重要的部件。Quartz不只僅是線程和線程管理。爲確保可伸縮性,Quartz.NET採用了基於多線程的架構。 啓動時,框架初始化一套worker線程,這套線程被調度器用來執行預約的做業。這就是Quartz.NET怎樣能併發運行多個做業的原理。Quartz.NET依賴一套鬆耦合的線程池管理部件來管理線程環境。做業是一個執行任務的簡單.NET類。任務能夠是任何C#/VB.NET代碼。只需你實現Quartz.IJob接口而且在出現嚴重錯誤狀況下拋出JobExecutionException異常便可。併發

IJob接口包含惟一的一個方法Execute(),做業從這裏開始執行。一旦實現了IJob接口和Execute ()方法,當Quartz.NET肯定該是做業運行的時候,它將調用你的做業。Execute()方法內就徹底是你要作的事情。

經過實現 Quartz.IJob接口,可使 .NET 類變成可執行的。清單 1 提供了 Quartz.IJob做業的一個示例。這個類用一條很是簡單的輸出語句覆蓋了 Execute(JobExecutionContext context) 方法。這個方法能夠包含咱們想要執行的任何代碼(全部的代碼示例都基於 Quartz.NET 0.6 ,它是編寫這篇文章時的穩定發行版)。

清單 1:做業

 

[c-sharp]  view plain  copy
 
  1. using System;   
  2.   
  3. using System.Collections.Generic;   
  4.   
  5. using System.Text;   
  6.   
  7. using Common.Logging;   
  8.   
  9. using Quartz;   
  10.   
  11. namespace QuartzBeginnerExample   
  12.   
  13. {   
  14.   
  15. public class SimpleQuartzJob : IJob   
  16.   
  17. {   
  18.   
  19. private static ILog _log = LogManager.GetLogger(typeof(SimpleQuartzJob));   
  20.   
  21. /// <summary>   
  22.   
  23. /// Called by the <see cref="IScheduler" /> when a   
  24.   
  25. /// <see cref="Trigger" /> fires that is associated with   
  26.   
  27. /// the <see cref="IJob" />.   
  28.   
  29. /// </summary>   
  30.   
  31. public virtual void Execute(JobExecutionContext context)   
  32.   
  33. {   
  34.   
  35. try   
  36.   
  37. {   
  38.   
  39. // This job simply prints out its job name and the   
  40.   
  41. // date and time that it is running   
  42.   
  43. string jobName = context.JobDetail.FullName;   
  44.   
  45. _log.Info("Executing job: " + jobName + " executing at " + DateTime.Now.ToString("r"));   
  46.   
  47. }   
  48.   
  49. catch (Exception e)   
  50.   
  51. {   
  52.   
  53. _log.Info("--- Error in job!");   
  54.   
  55. JobExecutionException e2 = new JobExecutionException(e);   
  56.   
  57. // this job will refire immediately   
  58.   
  59. e2.RefireImmediately = true;   
  60.   
  61. throw e2;   
  62.   
  63. }   
  64.   
  65. }   
  66.   
  67. }   
  68.   
  69. }   

 

請注意,Execute 方法接受一個 JobExecutionContext 對象做爲參數。這個對象提供了做業實例的運行時上下文。特別地,它提供了對調度器和觸發器的訪問,這二者協做來啓動做業以及做業的 JobDetail 對象的執行。Quartz.NET 經過把做業的狀態放在 JobDetail 對象中並讓 JobDetail 構造函數啓動一個做業的實例,分離了做業的執行和做業周圍的狀態。JobDetail 對象儲存做業的偵聽器、羣組、數據映射、描述以及做業的其餘屬性。

做業和觸發器:

Quartz.NET設計者作了一個設計選擇來從調度分離開做業。Quartz.NET中的觸發器用來告訴調度程序做業何時觸發。框架提供了一把觸發器類型,但兩個最經常使用的是SimpleTrigger和CronTrigger。SimpleTrigger爲須要簡單打火調度而設計。

典型地,若是你須要在給定的時間和重複次數或者兩次打火之間等待的秒數打火一個做業,那麼SimpleTrigger適合你。另外一方面,若是你有許多複雜的做業調度,那麼或許須要CronTrigger。

CronTrigger是基於Calendar-like調度的。當你須要在除星期六和星期天外的天天上午10點半執行做業時,那麼應該使用CronTrigger。正如它的名字所暗示的那樣,CronTrigger是基於Unix克隆表達式的。

Cron表達式被用來配置CronTrigger實例。Cron表達式是一個由7個子表達式組成的字符串。每一個子表達式都描述了一個單獨的日程細節。這些子表達式用空格分隔,分別表示:

1. Seconds 秒

2. Minutes 分鐘

3. Hours 小時

4. Day-of-Month 月中的天

5. Month 月

6. Day-of-Week 週中的天

7. Year (optional field) 年(可選的域)

一個cron表達式的例子字符串爲"0 0 12 ? * WED",這表示「每週三的中午12:00」。

單個子表達式能夠包含範圍或者列表。例如:前面例子中的週中的天這個域(這裏是"WED")能夠被替換爲"MON-FRI", "MON, WED, FRI"或者甚至"MON-WED,SAT"。

通配符('*')能夠被用來表示域中「每一個」可能的值。所以在"Month"域中的*表示每月,而在Day-Of-Week域中的*則表示「週中的每一天」。

全部的域中的值都有特定的合法範圍,這些值的合法範圍至關明顯,例如:秒和分域的合法值爲0到59,小時的合法範圍是0到23,Day-of-Month中值得合法凡範圍是0到31,可是須要注意不一樣的月份中的天數不一樣。月份的合法值是0到11。或者用字符串JAN,FEB MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV 及DEC來表示。Days-of-Week能夠用1到7來表示(1=星期日)或者用字符串SUN, MON, TUE, WED, THU, FRI 和SAT來表示.

'/'字符用來表示值的增量,例如, 若是分鐘域中放入'0/15',它表示「每隔15分鐘,從0開始」,若是在份中域中使用'3/20',則表示「小時中每隔20分鐘,從第3分鐘開始」或者另外相同的形式就是'3,23,43'。

'?'字符能夠用在day-of-month及day-of-week域中,它用來表示「沒有指定值」。這對於須要指定一個或者兩個域的值而不須要對其餘域進行設置來講至關有用。

'L'字符能夠在day-of-month及day-of-week中使用,這個字符是"last"的簡寫,可是在兩個域中的意義不一樣。例如,在day-of-month域中的"L"表示這個月的最後一天,即,一月的31日,非閏年的二月的28日。若是它用在day-of-week中,則表示"7"或者"SAT"。可是若是在day-of-week域中,這個字符跟在別的值後面,則表示"當月的最後的周XXX"。例如:"6L" 或者 "FRIL"都表示本月的最後一個週五。當使用'L'選項時,最重要的是不要指定列表或者值範圍,不然會致使混亂。

'W' 字符用來指定距離給定日最接近的周幾(在day-of-week域中指定)。例如:若是你爲day-of-month域指定爲"15W",則表示「距離月中15號最近的周幾」。

'#'表示表示月中的第幾個周幾。例如:day-of-week域中的"6#3" 或者 "FRI#3"表示「月中第三個週五」。

做爲一個例子,下面的Quartz.NET克隆表達式將在星期一到星期五的天天上午10點15分執行一個做業。

0 15 10 ? * MON-FRI

下面的表達式

0 15 10 ? * 6L 2007-2010

將在2007年到2010年的每月的最後一個星期五上午10點15分執行做業。你不可能用SimpleTrigger來作這些事情。你能夠用二者之中的任何一個,但哪一個跟合適則取決於你的調度須要。

清單 2 中的 SimpleTrigger 展現了觸發器的基礎:

清單2 SimpleTriggerRunner.cs

 

[c-sharp]  view plain  copy
 
  1. using System;   
  2.   
  3. using System.Collections.Generic;   
  4.   
  5. using System.Text;   
  6.   
  7. using Common.Logging;   
  8.   
  9. using Quartz;   
  10.   
  11. using Quartz.Impl;   
  12.   
  13. namespace QuartzBeginnerExample   
  14.   
  15. {   
  16.   
  17. public class SimpleTriggerRunner   
  18.   
  19. {   
  20.   
  21. public virtual void Run()   
  22.   
  23. {   
  24.   
  25. ILog log = LogManager.GetLogger(typeof(SimpleTriggerExample));   
  26.   
  27. log.Info("------- Initializing -------------------");   
  28.   
  29. // First we must get a reference to a scheduler   
  30.   
  31. ISchedulerFactory sf = new StdSchedulerFactory();   
  32.   
  33. IScheduler sched = sf.GetScheduler();   
  34.   
  35. log.Info("------- Initialization Complete --------");   
  36.   
  37. log.Info("------- Scheduling Jobs ----------------");   
  38.   
  39. // jobs can be scheduled before sched.start() has been called   
  40.   
  41. // get a "nice round" time a few seconds in the future...   
  42.   
  43. DateTime ts = TriggerUtils.GetNextGivenSecondDate(null, 15);   
  44.   
  45. // job1 will only fire once at date/time "ts"   
  46.   
  47. JobDetail job = new JobDetail("job1", "group1", typeof(SimpleJob));   
  48.   
  49. SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1");   
  50.   
  51. // set its start up time   
  52.   
  53. trigger.StartTime = ts;   
  54.   
  55. // set the interval, how often the job should run (10 seconds here)   
  56.   
  57. trigger.RepeatInterval = 10000;   
  58.   
  59. // set the number of execution of this job, set to 10 times.   
  60.   
  61. // It will run 10 time and exhaust.   
  62.   
  63. trigger.RepeatCount = 100;   
  64.   
  65. // schedule it to run!   
  66.   
  67. DateTime ft = sched.ScheduleJob(job, trigger);   
  68.   
  69. log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds",   
  70.   
  71. job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval / 1000)));   
  72.   
  73. log.Info("------- Starting Scheduler ----------------");   
  74.   
  75. // All of the jobs have been added to the scheduler, but none of the jobs   
  76.   
  77. // will run until the scheduler has been started   
  78.   
  79. sched.Start();   
  80.   
  81. log.Info("------- Started Scheduler -----------------");   
  82.   
  83. log.Info("------- Waiting 30 seconds... --------------");   
  84.   
  85. try   
  86.   
  87. {   
  88.   
  89. // wait 30 seconds to show jobs   
  90.   
  91. Thread.Sleep(30 * 1000);   
  92.   
  93. // executing...   
  94.   
  95. }   
  96.   
  97. catch (ThreadInterruptedException)   
  98.   
  99. {   
  100.   
  101. }   
  102.   
  103. log.Info("------- Shutting Down ---------------------");   
  104.   
  105. sched.Shutdown(true);   
  106.   
  107. log.Info("------- Shutdown Complete -----------------");   
  108.   
  109. // display some stats about the schedule that just ran   
  110.   
  111. SchedulerMetaData metaData = sched.GetMetaData();   
  112.   
  113. log.Info(string.Format("Executed {0} jobs.", metaData.NumJobsExecuted));   
  114.   
  115. }   
  116.   
  117. }   
  118.   
  119. }   

 

清單 2 開始時實例化一個 SchedulerFactory,得到此調度器。就像前面討論過的,建立 JobDetail 對象時,它的構造函數要接受一個 Job 做爲參數。顧名思義,SimpleTrigger 實例至關原始。在建立對象以後,設置幾個基本屬性以當即調度任務,而後每 10 秒重複一次,直到做業被執行 100 次。

還有其餘許多方式能夠操縱 SimpleTrigger。除了指定重複次數和重複間隔,還能夠指定做業在特定日曆時間執行,只需給定執行的最長時間或者優先級(稍後討論)。執行的最長時間能夠覆蓋指定的重複次數,從而確保做業的運行不會超過最長時間。

清單 3 顯示了 CronTrigger 的一個示例。請注意 SchedulerFactory、Scheduler 和 JobDetail 的實例化,與 SimpleTrigger 示例中的實例化是相同的。在這個示例中,只是修改了觸發器。這裏指定的 cron 表達式(「0/5 * * * * ?」)安排任務每 5 秒執行一次。

清單3 CronTriggerRunner.cs

 

[c-sharp]  view plain  copy
 
  1. using System;   
  2.   
  3. using System.Collections.Generic;   
  4.   
  5. using System.Text;   
  6.   
  7. using Common.Logging;   
  8.   
  9. using Quartz;   
  10.   
  11. using Quartz.Impl;   
  12.   
  13. using System.Threading;   
  14.   
  15. namespace QuartzBeginnerExample   
  16.   
  17. {   
  18.   
  19. public class CronTriggerRunner   
  20.   
  21. {   
  22.   
  23. public virtual void Run()   
  24.   
  25. {   
  26.   
  27. ILog log = LogManager.GetLogger(typeof(CronTriggerRunner));   
  28.   
  29. log.Info("------- Initializing -------------------");   
  30.   
  31. // First we must get a reference to a scheduler   
  32.   
  33. ISchedulerFactory sf = new StdSchedulerFactory();   
  34.   
  35. IScheduler sched = sf.GetScheduler();   
  36.   
  37. log.Info("------- Initialization Complete --------");   
  38.   
  39. log.Info("------- Scheduling Jobs ----------------");   
  40.   
  41. // jobs can be scheduled before sched.start() has been called   
  42.   
  43. // job 1 will run every 20 seconds   
  44.   
  45. JobDetail job = new JobDetail("job1", "group1", typeof(SimpleQuartzJob));   
  46.   
  47. CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1");   
  48.   
  49. trigger.CronExpressionString = "0/20 * * * * ?";   
  50.   
  51. sched.AddJob(job, true);   
  52.   
  53. DateTime ft = sched.ScheduleJob(trigger);   
  54.   
  55. log.Info(string.Format("{0} has been scheduled to run at: {1} and repeat based on expression: {2}", job.FullName, ft.ToString("r"), trigger.CronExpressionString));   
  56.   
  57. log.Info("------- Starting Scheduler ----------------");   
  58.   
  59. // All of the jobs have been added to the scheduler, but none of the   
  60.   
  61. // jobs   
  62.   
  63. // will run until the scheduler has been started   
  64.   
  65. sched.Start();   
  66.   
  67. log.Info("------- Started Scheduler -----------------");   
  68.   
  69. log.Info("------- Waiting five minutes... ------------");   
  70.   
  71. try   
  72.   
  73. {   
  74.   
  75. // wait five minutes to show jobs   
  76.   
  77. Thread.Sleep(300 * 1000);   
  78.   
  79. // executing...   
  80.   
  81. }   
  82.   
  83. catch (ThreadInterruptedException)   
  84.   
  85. {   
  86.   
  87. }   
  88.   
  89. log.Info("------- Shutting Down ---------------------");   
  90.   
  91. sched.Shutdown(true);   
  92.   
  93. log.Info("------- Shutdown Complete -----------------");   
  94.   
  95. SchedulerMetaData metaData = sched.GetMetaData();   
  96.   
  97. log.Info(string.Format("Executed {0} jobs.", metaData.NumJobsExecuted));   
  98.   
  99. }   
  100.   
  101. }   
  102.   
  103. }   

 

如上所示,只用做業和觸發器,就能訪問大量的功能。可是,Quartz 是個豐富而靈活的調度包,對於願意研究它的人來講,它還提供了更多功能。下一節討論 Quartz 的一些高級特性。

做業管理和存儲

做業一旦被調度,調度器須要記住而且跟蹤做業和它們的執行次數。若是你的做業是30分鐘後或每30秒調用,這不是頗有用。事實上,做業執行須要很是準確和即時調用在被調度做業上的Execute()方法。Quartz經過一個稱之爲做業存儲(JobStore)的概念來作做業存儲和管理。

有效做業存儲

Quartz提供兩種基本做業存儲類型。第一種類型叫作RAMJobStore,它利用一般的內存來持久化調度程序信息。這種做業存儲類型最容易配置、構造和運行。Quartz.net缺省使用的就是RAMJobStore。對許多應用來講,這種做業存儲已經足夠了。

然而,由於調度程序信息是存儲在被分配在內存裏面,因此,當應用程序中止運行時,全部調度信息將被丟失。若是你須要在從新啓動之間持久化調度信息,則將須要第二種類型的做業存儲。爲了修正這個問題,Quartz.NET 提供了 AdoJobStore。顧名思義,做業倉庫經過 ADO.NET把全部數據放在數據庫中。數據持久性的代價就是性能下降和複雜性的提升。它將全部的數據經過ADO.NET保存到數據庫可中。它的配置要比RAMJobStore稍微複雜,同時速度也沒有那麼快。可是性能的缺陷不是很是差,尤爲是若是你在數據庫表的主鍵上創建索引。

設置AdoJobStore

AdoJobStore幾乎能夠在任何數據庫上工做,它普遍地使用OracleMySQL, MS SQLServer2000, HSQLDB, PostreSQL 以及 DB2。要使用AdoJobStore,首先必須建立一套Quartz使用的數據庫表,能夠在Quartz 的database/tables找到建立庫表的SQL腳本。若是沒有找到你的數據庫類型的腳本,那麼找到一個已有的,修改爲爲你數據庫所須要的。須要注意的一件事情就是全部Quartz庫表名都以QRTZ_做爲前綴(例如:表"QRTZ_TRIGGERS",及"QRTZ_JOB_DETAIL")。實際上,能夠你能夠將前綴設置爲任何你想要的前綴,只要你告訴AdoJobStore那個前綴是什麼便可(在你的Quartz屬性文件中配置)。對於一個數據庫中使用多個scheduler實例,那麼配置不一樣的前綴能夠建立多套庫表,十分有用。

一旦數據庫表已經建立,在配置和啓動AdoJobStore以前,就須要做出一個更加劇要的決策。你要決定在你的應用中須要什麼類型的事務。若是不想將scheduling命令綁到其餘的事務上,那麼你能夠經過對JobStore使用JobStoreTX來讓Quartz幫你管理事務(這是最廣泛的選擇)。

最後的疑問就是如何創建得到數據庫聯接的數據源(DataSource)。Quartz屬性中定義數據源是經過提供全部聯接數據庫的信息,讓Quartz本身建立和管理數據源。

要使用AdoJobStore(假定使用StdSchedulerFactory),首先須要設置Quartz配置中的quartz.jobStore.type屬性爲Quartz.Impl.AdoJobStore.JobStoreTX, Quartz。

配置 Quartz使用 JobStoreTx

quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz

下一步,須要爲JobStore 選擇一個DriverDelegate , DriverDelegate負責作指定數據庫的全部ADO.NET工做。StdADO.NETDelegate是一個使用vanilla" ADO.NET代碼(以及SQL語句)來完成工做的代理。若是數據庫沒有其餘指定的代理,那麼就試用這個代理。只有當使用StdADO.NETDelegate發生問題時,咱們纔會使用數據庫特定的代理(這看起來很是樂觀。其餘的代理能夠在Quartz.Impl.AdoJobStor命名空間找到。)。其餘的代理包括PostgreSQLDelegate ( 專爲PostgreSQL 7.x)。

一旦選擇好了代理,就將它的名字設置給AdoJobStore。

配置AdoJobStore 使用DriverDelegate

quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz

接下來,須要爲JobStore指定所使用的數據庫表前綴(前面討論過)。

配置AdoJobStore的數據庫表前綴

quartz.jobStore.tablePrefix = QRTZ

而後須要設置JobStore所使用的數據源。必須在Quartz屬性中定義已命名的數據源,好比,咱們指定Quartz使用名爲"default"的數據源(在配置文件的其餘地方定義)。

配置 AdoJobStore使用數據源源的名字

properties["quartz.jobStore.dataSource"] = "default"

最後,須要配置數據源的使用的Ado.net數據提供者和數據庫鏈接串,數據庫鏈接串是標準的Ado.net 數據庫鏈接的鏈接串。數據庫提供者是關係數據庫同Quartz.net之間保持低耦合的數據庫的鏈接提供者.

配置AdoJobStore使用數據源源的數據庫鏈接串和數據庫提供者

quartz.dataSource.default.connectionString = Server=(local);Database=quartz;Trusted_Connection=True;

quartz.dataSource.default.provider= SqlServer-11

目前Quartz.net支持的如下數據庫的數據提供者:

l SqlServer-11 - SQL Server driver for .NET Framework 1.1

l SqlServer-20 - SQL Server driver for .NET Framework 2.0

l OracleClient-20 - Microsoft's oracle Driver (comes bundled with .NET Framework)

l OracleODP-20 - Oracle's Oracle Driver

mysql-10 - MySQL Connector/.NET v. 1.0.7

l MySql-109 - MySQL Connector/.NET v. 1.0.9

l MySql-50 - MySQL Connector/.NET v. 5.0 (.NET 2.0)

l MySql-51 - MySQL Connector/:NET v. 5.1 (.NET 2.0)

l SQLite1044 - SQLite ADO.NET 2.0 Provider v. 1.0.44 (.NET 2.0)

若是Scheduler很是忙(好比,執行的任務數量差很少和線程池的數量相同,那麼你須要正確地配置DataSource的鏈接數量爲線程池數量。爲了指示AdoJobStore全部的JobDataMaps中的值都是字符串,而且能以「名字-值」對的方式存儲而不是以複雜對象的序列化形式存儲在BLOB字段中,應設置 quartz.jobStore.usePropertiess配置參數的值爲"true"(這是缺省的方式)。這樣作,從長遠來看很是安全,這樣避免了對存儲在BLOB中的非字符串的序列化對象的類型轉換問題。

清單 4 展現了 AdoJobStore提供的數據持久性。就像在前面的示例中同樣,先從初始化 SchedulerFactory 和 Scheduler 開始。而後,再也不須要初始化做業和觸發器,而是要獲取觸發器羣組名稱列表,以後對於每一個羣組名稱,獲取觸發器名稱列表。請注意,每一個現有的做業都應當用 Scheduler. RescheduleJob () 方法從新調度。僅僅從新初始化在先前的應用程序運行時終止的做業,不會正確地裝載觸發器的屬性。

清單4 AdoJobStoreRunner.cs

 

[c-sharp]  view plain  copy
 
  1. public class AdoJobStoreRunner : IExample   
  2.   
  3. {   
  4.   
  5. public string Name   
  6.   
  7. {   
  8.   
  9. get { return GetType().Name; }   
  10.   
  11. }   
  12.   
  13. private static ILog _log = LogManager.GetLogger(typeof(AdoJobStoreRunner));   
  14.   
  15. public virtual void CleanUp(IScheduler inScheduler)   
  16.   
  17. {   
  18.   
  19. _log.Warn("***** Deleting existing jobs/triggers *****");   
  20.   
  21. // unschedule jobs   
  22.   
  23. string[] groups = inScheduler.TriggerGroupNames;   
  24.   
  25. for (int i = 0; i < groups.Length; i++)   
  26.   
  27. {   
  28.   
  29. String[] names = inScheduler.GetTriggerNames(groups[i]);   
  30.   
  31. for (int j = 0; j < names.Length; j++)   
  32.   
  33. inScheduler.UnscheduleJob(names[j], groups[i]);   
  34.   
  35. }   
  36.   
  37. // delete jobs   
  38.   
  39. groups = inScheduler.JobGroupNames;   
  40.   
  41. for (int i = 0; i < groups.Length; i++)   
  42.   
  43. {   
  44.   
  45. String[] names = inScheduler.GetJobNames(groups[i]);   
  46.   
  47. for (int j = 0; j < names.Length; j++)   
  48.   
  49. inScheduler.DeleteJob(names[j], groups[i]);   
  50.   
  51. }   
  52.   
  53. }   
  54.   
  55. public virtual void Run(bool inClearJobs, bool inScheduleJobs)   
  56.   
  57. {   
  58.   
  59. NameValueCollection properties = new NameValueCollection();   
  60.   
  61. properties["quartz.scheduler.instanceName"] = "TestScheduler";   
  62.   
  63. properties["quartz.scheduler.instanceId"] = "instance_one";   
  64.   
  65. properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";   
  66.   
  67. properties["quartz.threadPool.threadCount"] = "5";   
  68.   
  69. properties["quartz.threadPool.threadPriority"] = "Normal";   
  70.   
  71. properties["quartz.jobStore.misfireThreshold"] = "60000";   
  72.   
  73. properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";   
  74.   
  75. properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz";   
  76.   
  77. properties["quartz.jobStore.useProperties"] = "false";   
  78.   
  79. properties["quartz.jobStore.dataSource"] = "default";   
  80.   
  81. properties["quartz.jobStore.tablePrefix"] = "QRTZ_";   
  82.   
  83. properties["quartz.jobStore.clustered"] = "true";   
  84.   
  85. // if running MS SQL Server we need this   
  86.   
  87. properties["quartz.jobStore.selectWithLockSQL"] = "SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME = @lockName";   
  88.   
  89. properties["quartz.dataSource.default.connectionString"] = @"Server=LIJUNNIN-PCSQLEXPRESS;Database=quartz;Trusted_Connection=True;";   
  90.   
  91. properties["quartz.dataSource.default.provider"] = "SqlServer-20";   
  92.   
  93. // First we must get a reference to a scheduler   
  94.   
  95. ISchedulerFactory sf = new StdSchedulerFactory(properties);   
  96.   
  97. IScheduler sched = sf.GetScheduler();   
  98.   
  99. if (inClearJobs)   
  100.   
  101. {   
  102.   
  103. CleanUp(sched);   
  104.   
  105. }   
  106.   
  107. _log.Info("------- Initialization Complete -----------");   
  108.   
  109. if (inScheduleJobs)   
  110.   
  111. {   
  112.   
  113. _log.Info("------- Scheduling Jobs ------------------");   
  114.   
  115. string schedId = sched.SchedulerInstanceId;   
  116.   
  117. int count = 1;   
  118.   
  119. JobDetail job = new JobDetail("job_" + count, schedId, typeof(SimpleQuartzJob));   
  120.   
  121. // ask scheduler to re-Execute this job if it was in progress when   
  122.   
  123. // the scheduler went down...   
  124.   
  125. job.RequestsRecovery = true;   
  126.   
  127. SimpleTrigger trigger = new SimpleTrigger("trig_" + count, schedId, 20, 5000L);   
  128.   
  129. trigger.StartTime = DateTime.Now.AddMilliseconds(1000L);   
  130.   
  131. sched.ScheduleJob(job, trigger);   
  132.   
  133. _log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds", job.FullName, trigger.GetNextFireTime(), trigger.RepeatCount, (trigger.RepeatInterval / 1000)));   
  134.   
  135. count++;   
  136.   
  137. job = new JobDetail("job_" + count, schedId, typeof(SimpleQuartzJob));   
  138.   
  139. // ask scheduler to re-Execute this job if it was in progress when   
  140.   
  141. // the scheduler went down...   
  142.   
  143. job.RequestsRecovery = (true);   
  144.   
  145. trigger = new SimpleTrigger("trig_" + count, schedId, 20, 5000L);   
  146.   
  147. trigger.StartTime = (DateTime.Now.AddMilliseconds(2000L));   
  148.   
  149. sched.ScheduleJob(job, trigger);   
  150.   
  151. _log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds", job.FullName, trigger.GetNextFireTime(), trigger.RepeatCount, (trigger.RepeatInterval / 1000)));   
  152.   
  153. count++;   
  154.   
  155. job = new JobDetail("job_" + count, schedId, typeof(SimpleQuartzJob));   
  156.   
  157. // ask scheduler to re-Execute this job if it was in progress when   
  158.   
  159. // the scheduler went down...   
  160.   
  161. job.RequestsRecovery = (true);   
  162.   
  163. trigger = new SimpleTrigger("trig_" + count, schedId, 20, 3000L);   
  164.   
  165. trigger.StartTime = (DateTime.Now.AddMilliseconds(1000L));   
  166.   
  167. sched.ScheduleJob(job, trigger);   
  168.   
  169. _log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds", job.FullName, trigger.GetNextFireTime(), trigger.RepeatCount, (trigger.RepeatInterval / 1000)));   
  170.   
  171. count++;   
  172.   
  173. job = new JobDetail("job_" + count, schedId, typeof(SimpleQuartzJob));   
  174.   
  175. // ask scheduler to re-Execute this job if it was in progress when   
  176.   
  177. // the scheduler went down...   
  178.   
  179. job.RequestsRecovery = (true);   
  180.   
  181. trigger = new SimpleTrigger("trig_" + count, schedId, 20, 4000L);   
  182.   
  183. trigger.StartTime = (DateTime.Now.AddMilliseconds(1000L));   
  184.   
  185. sched.ScheduleJob(job, trigger);   
  186.   
  187. _log.Info(string.Format("{0} will run at: {1} & repeat: {2}/{3}", job.FullName, trigger.GetNextFireTime(), trigger.RepeatCount, trigger.RepeatInterval));   
  188.   
  189. count++;   
  190.   
  191. job = new JobDetail("job_" + count, schedId, typeof(SimpleQuartzJob));   
  192.   
  193. // ask scheduler to re-Execute this job if it was in progress when   
  194.   
  195. // the scheduler went down...   
  196.   
  197. job.RequestsRecovery = (true);   
  198.   
  199. trigger = new SimpleTrigger("trig_" + count, schedId, 20, 4500L);   
  200.   
  201. trigger.StartTime = (DateTime.Now.AddMilliseconds(1000L));   
  202.   
  203. sched.ScheduleJob(job, trigger);   
  204.   
  205. _log.Info(string.Format("{0} will run at: {1} & repeat: {2}/{3}", job.FullName, trigger.GetNextFireTime(), trigger.RepeatCount, trigger.RepeatInterval));   
  206.   
  207. }   
  208.   
  209. // jobs don't start firing until start() has been called...   
  210.   
  211. _log.Info("------- Starting Scheduler ---------------");   
  212.   
  213. sched.Start();   
  214.   
  215. _log.Info("------- Started Scheduler ----------------");   
  216.   
  217. _log.Info("------- Waiting for one hour... ----------");   
  218.   
  219. Thread.Sleep(TimeSpan.FromHours(1));   
  220.   
  221. _log.Info("------- Shutting Down --------------------");   
  222.   
  223. sched.Shutdown();   
  224.   
  225. _log.Info("------- Shutdown Complete ----------------");   
  226.   
  227. }   
  228.   
  229. public void Run()   
  230.   
  231. {   
  232.   
  233. bool clearJobs = true;   
  234.   
  235. bool scheduleJobs = true;   
  236.   
  237. AdoJobStoreRunner example = new AdoJobStoreRunner();   
  238.   
  239. example.Run(clearJobs, scheduleJobs);   
  240.   
  241. }   
  242.   
  243. }  

 

結束語

Quartz.net 做業調度框架所提供的 API 在兩方面都表現極佳:既全面強大,又易於使用。Quartz 能夠用於簡單的做業觸發,也能夠用於複雜的 Ado.net持久的做業存儲和執行。

示例下載 :www.cnblogs.com/Files/shanyou/QuartzBeginnerExample.zip  基於 Quartz.net 的示例 (C#代碼 )   QuartzBeginnerExample.zip    324KB 

獲取Quartz.net :quartznet.sourceforge.net/download.html:.NET應用程序的開放源碼做業調度解決方案


本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/dz45693/archive/2011/01/18/6151189.aspx

相關文章
相關標籤/搜索