在日常平凡的工作中,预计大多数都做过轮询调理的使命,比方定时轮询数据库同步,定时邮件关照等等。人人经由历程windows计划使命,windows效劳等都完成过此类使命,以至完成过本身的设置定制化的框架。那本日就来引见个开源的调理框架Quartz.Net(重要引见设置的完成,由于有朋侪问过此类题目)。调理的完成代码很简朴,在源码中有大批Demo,这里就略过了。
Quartz.Net当前最新版本Quartz.NET 2.0 beta 1 Released
一、基于文件设置
先看一下简朴的完成代码
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); } } }
代码很简朴,设置文件中的quartz基本设置,以及job,trigger信息是怎样加载的?这个历程是发作 IScheduler sched = sf.GetScheduler();历程,重要体现在源码这一段
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)); }
经由历程上面代码剖析,初始化起首会搜检体系config中是不是有<quartz> configuration section节点 (config指的app.config,web.config),假如体系config有quartz节点,则直接加载此处的设置信息。假如体系config没有quartz的基本设置信息,则会继承查找是不是有quartz.config/Quartz.quartz.config 这两个设置文件的存在,假如有则加载设置信息,假如没有则扔出初始化设置非常。
而jobs.xml(调理的使命和触发器plugin节点设置文件)
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
二、基于代码的体式格局
这类状况直接经由历程代码完成的,官方DEMO许多都是云云,我们举个例子
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 -----------------"); } } }
实在代码体式格局已完成了和设置文件混搭的体式格局了。然则这类对体式格局是经由历程设置关联加载job与trigger设置,我们另有第三种体式格局,本身加载job与trigger设置文件。
三、手动加载设置文件
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(); } } }
现在依据源码剖析大抵就这几种设置体式格局,很天真能够恣意组合。 关于quartz的运用源码很细致,而且园子量有大批的进修文章。
记着quartz的设置读取体式格局起首app.config/web.config ---->quartz.config/Quartz.quartz.config ---->quartz_jobs.xml .
经由历程代码体式格局我们能够转变quartz_jobs.xml 的指向即本身新定名的xml文件
(默许的quartz_jobs.xml是在XMLSchedulingDataProcessor.QuartzXmlFileName = "quartz_jobs.xml"被指定的)
体式格局一,经由历程quartz节点/quartz.config指向指定的jobs.xml。
体式格局二,经由历程XMLSchedulingDataProcessor 加载指定的jobs.xml
以上就是细致引见Quartz.Net调理框架设置的实例的细致内容,更多请关注ki4网别的相干文章!