详细介绍Quartz.Net调度框架配置的实例

这篇文章主要为大家详细介绍了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  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中是否有 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.config 中plugin配置指向(quartz.plugin.xml.type / quartz.plugin.xml.fileNames)

# You can configure your scheduler in either  configuration section# or in quartz properties file# Configuration section has precedencequartz.scheduler.instanceName = ServerScheduler# configure thread pool infoquartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartzquartz.threadPool.threadCount = 10quartz.threadPool.threadPriority = Normal#--------------------------------*************plugin配置------------------------------------# job initialization plugin handles our xml reading, without it defaults are usedquartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartzquartz.plugin.xml.fileNames = ~/quartz_jobs.xml# export this server to remoting contextquartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartzquartz.scheduler.exporter.port = 555quartz.scheduler.exporter.bindName = QuartzSchedulerquartz.scheduler.exporter.channelType = tcpquartz.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()    .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调度框架配置的实例的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2439091.html

(0)
上一篇 2025年3月3日 11:57:26
下一篇 2025年2月26日 19:23:11

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 讨论在VS2015下使用EF框架的使用方法

    这篇文章主要为大家详细介绍了vs2015下简单使用ef框架的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 VS2015下如何简单使用EF框架的方法,具体内容如下 新建Model1.edmx文件 页面引用数据库Model1 如下创建…

    2025年3月3日
    200
  • 华为公司与湖南高速集团在长沙签署战略合作框架协议

    近日,cnmo 注意到,据湖南高速公路官方消息,湖南省高速公路集团有限公司与华为技术有限公司已于 7 月 24 日在长沙签署战略合作框架协议。湖南高速集团董事长罗卫华,副董事长黄斌与华为公司交通智慧化军团副总裁沈素荣出席并见证签约。 湖南高…

    2025年3月2日
    200
  • 微服务架构中如何处理服务的自动化伸缩和调度?

    随着互联网的不断发展,传统的单一的、封闭的应用架构已经无法满足应用的需求,微服务架构已经成为了越来越多企业的首选架构。微服务架构具有高度的可扩展性、高并发性、可靠性等优点,但是微服务架构中涉及到许多服务的自动化伸缩和调度问题,如何解决这些问…

    编程技术 2025年3月2日
    200
  • Go语言中的资源分配优化和调度

    随着互联网的快速发展,go语言作为一种新的编程语言因其并发性能出色和资源管理优越而备受青睐。本文将重点介绍go语言中的资源分配优化和调度,希望能够为读者提供一些有益的理解和实践经验。 一、Go语言的资源管理机制 在Go语言中,每个gorou…

    编程技术 2025年3月2日
    200
  • Go语言中的Docker技术与框架

    随着云计算的兴起和应用的广泛,容器技术越来越受到人们的关注。docker作为一种流行的容器化技术,已经得到了广泛的应用。而go语言作为一种快速、高效的编程语言,也越来越受到人们的青睐。在这篇文章中,我们将深入探讨go语言中的docker技术…

    编程技术 2025年3月2日
    200
  • Go语言中的框架和库的选择标准

    随着互联网的快速发展和技术的不断进步,各种新型框架和库层出不穷,方便了软件开发人员的工作,提升了开发效率。作为一门多范式编程语言,go语言也拥有着丰富的框架和库,这些框架和库可以帮助开发人员快速构建应用程序,降低开发难度,提高开发效率。然而…

    编程技术 2025年3月2日
    200
  • 如何利用Go框架进行高效的Web开发

    随着互联网的不断普及,web开发领域也以惊人的速度发展着。作为一种简单易用、高效稳定的编程语言,go 在 web 开发领域中也呈现出了强劲的表现。go 拥有良好的并发性、高效的内存管理、易于编写和维护的代码等特点,越来越多的开发者开始选择使…

    编程技术 2025年3月2日
    200
  • 学习Go语言框架,常见问题解析

    随着go语言的流行,越来越多的人开始学习和使用go语言编程。go语言具有简单、高效、性能优异等特点,使得它在后端开发领域得到了广泛的应用。而在go语言开发中,框架是不可或缺的一部分,因为它能够为我们提供一系列的工具和功能,使得开发更为高效且…

    编程技术 2025年3月2日
    200
  • 有哪些适合于Go语言开发的微服务框架?

    随着微服务架构的兴起,越来越多的开发者开始探索如何将应用程序拆分成小而独立的服务,并将它们组合成一个更大的应用。go语言因其高效、简洁和并发性能出色的特点,成为了其中一个热门的用于微服务开发的语言。而本文将介绍一些适合于go语言开发的微服务…

    编程技术 2025年3月2日
    200
  • 使用Go语言框架构建大型项目的最佳实践

    随着互联网的发展,越来越多的企业和组织开始依赖于大型软件系统来管理和实现各种业务。在这个背景下,构建高效可靠的大型项目变得至关重要。而go语言作为一门具备高并发和高效率的语言,近年来在构建大型项目中广受欢迎。然而,go语言并没有官方的框架,…

    编程技术 2025年3月2日
    200

发表回复

登录后才能评论