Quartz.Net調(diào)度框架配置解析
在平時(shí)的工作中,估計(jì)大多數(shù)都做過(guò)輪詢(xún)調(diào)度的任務(wù),比如定時(shí)輪詢(xún)數(shù)據(jù)庫(kù)同步,定時(shí)郵件通知等等。大家通過(guò)windows計(jì)劃任務(wù),windows服務(wù)等都實(shí)現(xiàn)過(guò)此類(lèi)任務(wù),甚至實(shí)現(xiàn)過(guò)自己的配置定制化的框架。那今天就來(lái)介紹個(gè)開(kāi)源的調(diào)度框架Quartz.Net(主要介紹配置的實(shí)現(xiàn),因?yàn)橛信笥褑?wèn)過(guò)此類(lèi)問(wèn)題)。調(diào)度的實(shí)現(xiàn)代碼很簡(jiǎn)單,在源碼中有大量Demo,這里就略過(guò)了。
Quartz.Net當(dāng)前最新版本Quartz.NET 2.0 beta 1 Released
一、基于文件配置
先看一下簡(jiǎn)單的實(shí)現(xiàn)代碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using Quartz; using Quartz.Impl; using Common.Logging; namespace Demo { class Program { static void Main(string[] args) { // First we must get a reference to a scheduler ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler sched = sf.GetScheduler(); sched.Start(); sched.Shutdown(true); } } }
代碼很簡(jiǎn)單,配置文件中的quartz基礎(chǔ)配置,以及job,trigger信息是如何加載的?這個(gè)過(guò)程是發(fā)生 IScheduler sched = sf.GetScheduler();過(guò)程,主要體現(xiàn)在源碼這一段
public void Initialize() { // short-circuit if already initialized if (cfg != null) { return; } if (initException != null) { throw initException; } NameValueCollection props = (NameValueCollection) ConfigurationManager.GetSection("quartz"); string requestedFile = Environment.GetEnvironmentVariable(PropertiesFile); string propFileName = requestedFile != null && requestedFile.Trim().Length > 0 ? requestedFile : "~/quartz.config"; // check for specials propFileName = FileUtil.ResolveFile(propFileName); if (props == null && File.Exists(propFileName)) { // file system try { PropertiesParser pp = PropertiesParser.ReadFromFileResource(propFileName); props = pp.UnderlyingProperties; Log.Info(string.Format("Quartz.NET properties loaded from configuration file '{0}'", propFileName)); } catch (Exception ex) { Log.Error("Could not load properties for Quartz from file {0}: {1}".FormatInvariant(propFileName, ex.Message), ex); } } if (props == null) { // read from assembly try { PropertiesParser pp = PropertiesParser.ReadFromEmbeddedAssemblyResource("Quartz.quartz.config"); props = pp.UnderlyingProperties; Log.Info("Default Quartz.NET properties loaded from embedded resource file"); } catch (Exception ex) { Log.Error("Could not load default properties for Quartz from Quartz assembly: {0}".FormatInvariant(ex.Message), ex); } } if (props == null) { throw new SchedulerConfigException( @"Could not find <quartz> configuration section from your application config or load default configuration from assembly. Please add configuration to your application config file to correctly initialize Quartz."); } Initialize(OverrideWithSysProps(props)); }
通過(guò)上面代碼分析,初始化首先會(huì)檢查系統(tǒng)config中是否有<quartz> configuration section節(jié)點(diǎn) (config指的app.config,web.config),如果系統(tǒng)config有quartz節(jié)點(diǎn),則直接加載此處的配置信息。如果系統(tǒng)config沒(méi)有quartz的基礎(chǔ)配置信息,則會(huì)繼續(xù)查找是否有quartz.config/Quartz.quartz.config 這兩個(gè)配置文件的存在,如果有則加載配置信息,如果沒(méi)有則扔出初始化配置異常。
而jobs.xml(調(diào)度的任務(wù)和觸發(fā)器plugin節(jié)點(diǎn)配置文件)
app.config/web.config 中plugin配置
<quartz> <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/> <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/> <add key="quartz.threadPool.threadCount" value="10"/> <add key="quartz.threadPool.threadPriority" value="2"/> <add key="quartz.jobStore.misfireThreshold" value="60000"/> <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/> <!--******************************Plugin配置********************************************* --> <add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" /> <add key="quartz.plugin.xml.fileNames" value="quartz_jobs.xml"/> </quartz>
quartz.config 中plugin配置指向(quartz.plugin.xml.type / quartz.plugin.xml.fileNames)
# You can configure your scheduler in either <quartz> configuration section # or in quartz properties file # Configuration section has precedence quartz.scheduler.instanceName = ServerScheduler # configure thread pool info quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz quartz.threadPool.threadCount = 10 quartz.threadPool.threadPriority = Normal #--------------------------------*************plugin配置------------------------------------ # job initialization plugin handles our xml reading, without it defaults are used quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz quartz.plugin.xml.fileNames = ~/quartz_jobs.xml # export this server to remoting context quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz quartz.scheduler.exporter.port = 555 quartz.scheduler.exporter.bindName = QuartzScheduler quartz.scheduler.exporter.channelType = tcp quartz.scheduler.exporter.channelName = httpQuartz
二、基于代碼的方式
這種情況直接通過(guò)代碼實(shí)現(xiàn)的,官方DEMO很多都是如此,我們舉個(gè)例子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Quartz; using Quartz.Impl; using System.Threading; using Common.Logging; namespace Demo { class Program { static void Main(string[] args) { ILog log = LogManager.GetLogger(typeof(Demo.HelloJob)); log.Info("------- Initializing ----------------------"); // First we must get a reference to a scheduler ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler sched = sf.GetScheduler(); log.Info("------- Initialization Complete -----------"); //---------------------------------------代碼添加job和trigger // computer a time that is on the next round minute DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow); log.Info("------- Scheduling Job -------------------"); // define the job and tie it to our HelloJob class IJobDetail job = JobBuilder.Create<HelloJob>() .WithIdentity("job1", "group1") .Build(); // Trigger the job to run on the next round minute ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartAt(runTime) .Build(); // Tell quartz to schedule the job using our trigger sched.ScheduleJob(job, trigger); log.Info(string.Format("{0} will run at: {1}", job.Key, runTime.ToString("r"))); // Start up the scheduler (nothing can actually run until the // scheduler has been started) sched.Start(); log.Info("------- Started Scheduler -----------------"); // wait long enough so that the scheduler as an opportunity to // run the job! log.Info("------- Waiting 65 seconds... -------------"); // wait 65 seconds to show jobs Thread.Sleep(TimeSpan.FromSeconds(65)); // shut down the scheduler log.Info("------- Shutting Down ---------------------"); sched.Shutdown(true); log.Info("------- Shutdown Complete -----------------"); } } }
其實(shí)代碼方式已經(jīng)實(shí)現(xiàn)了和配置文件混搭的方式了。但是這種對(duì)方式是通過(guò)配置關(guān)聯(lián)加載job與trigger配置,我們還有第三種方式,自己加載job與trigger配置文件。
三、手動(dòng)加載配置文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Quartz; using Quartz.Xml; using Quartz.Impl; using Quartz.Simpl; using System.Threading; using Common.Logging; using System.IO; namespace Demo { class Program { static void Main(string[] args) { XMLSchedulingDataProcessor processor = new XMLSchedulingDataProcessor(new SimpleTypeLoadHelper()); ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler scheduler = sf.GetScheduler(); Stream s = new StreamReader("~/quartz.xml").BaseStream; processor.ProcessStream(s, null); processor.ScheduleJobs(scheduler); scheduler.Start(); scheduler.Shutdown(); } } }
亦或者這樣
using System.Text; using Quartz; using Quartz.Xml; using Quartz.Impl; using Quartz.Simpl; using System.Threading; using Common.Logging; using System.IO; namespace Demo { class Program { static void Main(string[] args) { XMLSchedulingDataProcessor processor = new XMLSchedulingDataProcessor(new SimpleTypeLoadHelper()); ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler scheduler = sf.GetScheduler(); processor.ProcessFileAndScheduleJobs("~/quartz.xml",scheduler); scheduler.Start(); scheduler.Shutdown(); } } }
目前根據(jù)源碼分析大致就這幾種配置方式,很靈活可以任意組合。 關(guān)于quartz的使用源碼很詳細(xì),并且園子量有大量的學(xué)習(xí)文章。
記住quartz的配置讀取方式首先app.config/web.config ---->quartz.config/Quartz.quartz.config ---->quartz_jobs.xml .
通過(guò)代碼方式我們可以改變quartz_jobs.xml 的指向即自己新命名的xml文件
(默認(rèn)的quartz_jobs.xml是在XMLSchedulingDataProcessor.QuartzXmlFileName = "quartz_jobs.xml"被指定的)
方式一,通過(guò)quartz節(jié)點(diǎn)/quartz.config指向指定的jobs.xml。
方式二,通過(guò)XMLSchedulingDataProcessor 加載指定的jobs.xml
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)翻轉(zhuǎn)字符串的方法
這篇文章主要介紹了C#實(shí)現(xiàn)翻轉(zhuǎn)字符串的方法,涉及C#操作字符串的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04C#?異步多線(xiàn)程入門(mén)到精通之Thread篇
Thread?是?1.0?時(shí)代的產(chǎn)物,當(dāng)時(shí)?C#?就考慮到了多線(xiàn)程,于是就設(shè)計(jì)了?Thread?。其實(shí)現(xiàn)在不推薦使用,除非在維護(hù)老的項(xiàng)目已經(jīng)用了的。Thread?也是比較雞肋的,有很多缺陷,但也并不是一無(wú)是處2021-11-11C#實(shí)現(xiàn)掃描局域網(wǎng)內(nèi)的所有IP和端口
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)掃描局域網(wǎng)內(nèi)的所有IP和端口的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12在Winform和WPF中注冊(cè)全局快捷鍵實(shí)現(xiàn)思路及代碼
如果注冊(cè)快捷鍵,RegisterHotKey中的fsModifiers參數(shù)為0,即None選項(xiàng),一些安全軟件會(huì)警報(bào),可能因?yàn)檫@樣就可以全局監(jiān)聽(tīng)鍵盤(pán)而造成安全問(wèn)題,感興趣的你可以參考下本文2013-02-02C#如何打開(kāi)選擇文件對(duì)話(huà)框和選擇目錄對(duì)話(huà)框
這篇文章主要介紹了C#如何打開(kāi)選擇文件對(duì)話(huà)框和選擇目錄對(duì)話(huà)框問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07C#使用dir命令實(shí)現(xiàn)文件搜索功能示例
這篇文章主要介紹了C#使用dir命令實(shí)現(xiàn)文件搜索功能,結(jié)合具體實(shí)例形式分析了C#調(diào)用與使用cmd命令相關(guān)操作技巧,需要的朋友可以參考下2017-07-07深入解析C#設(shè)計(jì)模式編程中對(duì)建造者模式的運(yùn)用
這篇文章主要介紹了C#設(shè)計(jì)模式編程中對(duì)建造者模式的運(yùn)用,文中還介紹了在.NET框架下建造者模式編寫(xiě)思路的實(shí)現(xiàn),需要的朋友可以參考下2016-02-02