Quartz.Net调度框架配置解析
在平时的工作中,估计大多数都做过轮询调度的任务,比如定时轮询数据库同步,定时邮件通知等等。大家通过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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
您可能感兴趣的文章
- 01-10C#开源的AOP框架--KingAOP基础
- 01-10结合.net框架在C#派生类中触发基类事件及实现接口事件
- 01-10详解C#借助.NET框架中的XmlTextReader类读取XML的方法
- 01-10基于mvc5+ef6+Bootstrap框架实现身份验证和权限管理
- 01-10微信公众平台开发教程(三) 基础框架搭建
- 01-10windows下C#定时管理器框架Task.MainForm详解
- 01-10C# .NET中Socket简单实用框架的使用教程
- 01-10NancyFx框架检测任务管理器详解
- 01-10在Winform框架界面中改变并存储界面皮肤样式的方法
- 01-10C# 通过反射初探ORM框架的实现原理(详解)
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-10delphi制作wav文件的方法
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-11ajax实现页面的局部加载
- 04-02jquery与jsp,用jquery
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10C#中split用法实例总结
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10SublimeText编译C开发环境设置