查看: 1949|回复: 0

[ASP.NET教程] Quartz.Net调度框架配置解析

发表于 2018-2-10 08:00:07

在平时的工作中,估计大多数都做过轮询调度的任务,比如定时轮询数据库同步,定时邮件通知等等。大家通过windows计划任务,windows服务等都实现过此类任务,甚至实现过自己的配置定制化的框架。那今天就来介绍个开源的调度框架Quartz.Net(主要介绍配置的实现,因为有朋友问过此类问题)。调度的实现代码很简单,在源码中有大量Demo,这里就略过了。

Quartz.Net当前最新版本Quartz.NET 2.0 beta 1 Released

一、基于文件配置

先看一下简单的实现代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Quartz;
  7. using Quartz.Impl;
  8. using Common.Logging;
  9. namespace Demo
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. // First we must get a reference to a scheduler
  16. ISchedulerFactory sf = new StdSchedulerFactory();
  17. IScheduler sched = sf.GetScheduler();
  18. sched.Start();
  19. sched.Shutdown(true);
  20. }
  21. }
  22. }
复制代码

代码很简单,配置文件中的quartz基础配置,以及job,trigger信息是如何加载的?这个过程是发生 IScheduler sched = sf.GetScheduler();过程,主要体现在源码这一段

  1. public void Initialize()
  2. {
  3. // short-circuit if already initialized
  4. if (cfg != null)
  5. {
  6. return;
  7. }
  8. if (initException != null)
  9. {
  10. throw initException;
  11. }
  12. NameValueCollection props = (NameValueCollection) ConfigurationManager.GetSection("quartz");
  13. string requestedFile = Environment.GetEnvironmentVariable(PropertiesFile);
  14. string propFileName = requestedFile != null && requestedFile.Trim().Length > 0 ? requestedFile : "~/quartz.config";
  15. // check for specials
  16. propFileName = FileUtil.ResolveFile(propFileName);
  17. if (props == null && File.Exists(propFileName))
  18. {
  19. // file system
  20. try
  21. {
  22. PropertiesParser pp = PropertiesParser.ReadFromFileResource(propFileName);
  23. props = pp.UnderlyingProperties;
  24. Log.Info(string.Format("Quartz.NET properties loaded from configuration file '{0}'", propFileName));
  25. }
  26. catch (Exception ex)
  27. {
  28. Log.Error("Could not load properties for Quartz from file {0}: 小贝".FormatInvariant(propFileName, ex.Message), ex);
  29. }
  30. }
  31. if (props == null)
  32. {
  33. // read from assembly
  34. try
  35. {
  36. PropertiesParser pp = PropertiesParser.ReadFromEmbeddedAssemblyResource("Quartz.quartz.config");
  37. props = pp.UnderlyingProperties;
  38. Log.Info("Default Quartz.NET properties loaded from embedded resource file");
  39. }
  40. catch (Exception ex)
  41. {
  42. Log.Error("Could not load default properties for Quartz from Quartz assembly: {0}".FormatInvariant(ex.Message), ex);
  43. }
  44. }
  45. if (props == null)
  46. {
  47. throw new SchedulerConfigException(
  48. @"Could not find <quartz> configuration section from your application config or load default configuration from assembly.
  49. Please add configuration to your application config file to correctly initialize Quartz.");
  50. }
  51. Initialize(OverrideWithSysProps(props));
  52. }
复制代码

通过上面代码分析,初始化首先会检查系统config中是否有 configuration section节点 (config指的app.config,web.config),如果系统config有quartz节点,则直接加载此处的配置信息。如果系统config没有quartz的基础配置信息,则会继续查找是否有quartz.config/Quartz.quartz.config 这两个配置文件的存在,如果有则加载配置信息,如果没有则扔出初始化配置异常。

而jobs.xml(调度的任务和触发器plugin节点配置文件)

app.config/web.config 中plugin配置

  1. <quartz>
  2. <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/>
  3. <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
  4. <add key="quartz.threadPool.threadCount" value="10"/>
  5. <add key="quartz.threadPool.threadPriority" value="2"/>
  6. <add key="quartz.jobStore.misfireThreshold" value="60000"/>
  7. <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
  8. <!--******************************Plugin配置********************************************* -->
  9. <add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" />
  10. <add key="quartz.plugin.xml.fileNames" value="quartz_jobs.xml"/>
  11. </quartz>
复制代码

quartz.config 中plugin配置指向(quartz.plugin.xml.type / quartz.plugin.xml.fileNames)

  1. # You can configure your scheduler in either <quartz> configuration section
  2. # or in quartz properties file
  3. # Configuration section has precedence
  4. quartz.scheduler.instanceName = ServerScheduler
  5. # configure thread pool info
  6. quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz
  7. quartz.threadPool.threadCount = 10
  8. quartz.threadPool.threadPriority = Normal
  9. #--------------------------------*************plugin配置------------------------------------
  10. # job initialization plugin handles our xml reading, without it defaults are used
  11. quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz
  12. quartz.plugin.xml.fileNames = ~/quartz_jobs.xml
  13. # export this server to remoting context
  14. quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz
  15. quartz.scheduler.exporter.port = 555
  16. quartz.scheduler.exporter.bindName = QuartzScheduler
  17. quartz.scheduler.exporter.channelType = tcp
  18. quartz.scheduler.exporter.channelName = httpQuartz
复制代码

二、基于代码的方式

这种情况直接通过代码实现的,官方DEMO很多都是如此,我们举个例子

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text; using Quartz;
  5. using Quartz.Impl;
  6. using System.Threading;
  7. using Common.Logging;
  8. namespace Demo
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. ILog log = LogManager.GetLogger(typeof(Demo.HelloJob));
  15. log.Info("------- Initializing ----------------------");
  16. // First we must get a reference to a scheduler
  17. ISchedulerFactory sf = new StdSchedulerFactory();
  18. IScheduler sched = sf.GetScheduler();
  19. log.Info("------- Initialization Complete -----------");
  20. //---------------------------------------代码添加job和trigger
  21. // computer a time that is on the next round minute
  22. DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
  23. log.Info("------- Scheduling Job -------------------");
  24. // define the job and tie it to our HelloJob class
  25. IJobDetail job = JobBuilder.Create<HelloJob>()
  26. .WithIdentity("job1", "group1")
  27. .Build();
  28. // Trigger the job to run on the next round minute
  29. ITrigger trigger = TriggerBuilder.Create()
  30. .WithIdentity("trigger1", "group1")
  31. .StartAt(runTime)
  32. .Build();
  33. // Tell quartz to schedule the job using our trigger
  34. sched.ScheduleJob(job, trigger);
  35. log.Info(string.Format("{0} will run at: 小贝", job.Key, runTime.ToString("r")));
  36. // Start up the scheduler (nothing can actually run until the
  37. // scheduler has been started)
  38. sched.Start();
  39. log.Info("------- Started Scheduler -----------------");
  40. // wait long enough so that the scheduler as an opportunity to
  41. // run the job!
  42. log.Info("------- Waiting 65 seconds... -------------");
  43. // wait 65 seconds to show jobs
  44. Thread.Sleep(TimeSpan.FromSeconds(65));
  45. // shut down the scheduler
  46. log.Info("------- Shutting Down ---------------------");
  47. sched.Shutdown(true);
  48. log.Info("------- Shutdown Complete -----------------");
  49. }
  50. }
  51. }
复制代码

其实代码方式已经实现了和配置文件混搭的方式了。但是这种对方式是通过配置关联加载job与trigger配置,我们还有第三种方式,自己加载job与trigger配置文件。

三、手动加载配置文件

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Quartz;
  6. using Quartz.Xml;
  7. using Quartz.Impl;
  8. using Quartz.Simpl;
  9. using System.Threading;
  10. using Common.Logging;
  11. using System.IO;
  12. namespace Demo
  13. {
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. XMLSchedulingDataProcessor processor = new XMLSchedulingDataProcessor(new SimpleTypeLoadHelper());
  19. ISchedulerFactory sf = new StdSchedulerFactory();
  20. IScheduler scheduler = sf.GetScheduler();
  21. Stream s = new StreamReader("~/quartz.xml").BaseStream;
  22. processor.ProcessStream(s, null);
  23. processor.ScheduleJobs(scheduler);
  24. scheduler.Start();
  25. scheduler.Shutdown();
  26. }
  27. }
  28. }
复制代码

亦或者这样

  1. using System.Text;
  2. using Quartz;
  3. using Quartz.Xml;
  4. using Quartz.Impl;
  5. using Quartz.Simpl;
  6. using System.Threading;
  7. using Common.Logging;
  8. using System.IO;
  9. namespace Demo
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. XMLSchedulingDataProcessor processor = new XMLSchedulingDataProcessor(new SimpleTypeLoadHelper());
  16. ISchedulerFactory sf = new StdSchedulerFactory();
  17. IScheduler scheduler = sf.GetScheduler();
  18. processor.ProcessFileAndScheduleJobs("~/quartz.xml",scheduler);
  19. scheduler.Start();
  20. scheduler.Shutdown();
  21. }
  22. }
  23. }
复制代码

目前根据源码分析大致就这几种配置方式,很灵活可以任意组合。 关于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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

您可能感兴趣的文章:

  • 详解免费开源的DotNet任务调度组件Quartz.NET(.NET组件介绍之五)
  • .net下Quartz.Net的使用方法


回复

使用道具 举报