zoukankan      html  css  js  c++  java
  • quartz.net结合Topshelf实现windows service服务托管的作业调度框架

    topshelf可以很简单方便的实现windows service服务,详见我的一篇博客的介绍

    http://www.cnblogs.com/xiaopotian/articles/5428361.html

    Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等。 Quartz.NET允许开发人员根据时间间隔(或天)来调度作业。它实现了作业和触发器的多对多关系,还能把多个作业与不同的触发器关联。整合了 Quartz.NET的应用程序可以重用来自不同事件的作业,还可以为一个事件组合多个作业。Quartz.NET是OpenSymphony的开源项目。Quartz.Net 是Quartz的C#移植版本。

    它一些很好的特性:

    1:支持集群,作业分组,作业远程管理。 

    2:自定义精细的时间触发器,使用简单,作业和触发分离。

    3:数据库支持,可以寄宿Windows服务,WebSite,winform等。

    下面是官网的详细介绍http://www.quartz-scheduler.net/documentation/index.html

    结合topshelf、log4net与官网案例写了一个小的demo

    第一步,实现IJob

     1 /// <summary>
     2     /// A sample job that just prints info on console for demostration purposes.
     3     /// </summary>
     4     public class SampleJob : IJob
     5     {
     6         //private static readonly ILog logger = LogManager.GetLogger(typeof(SampleJob));
     7 
     8         /// <summary>
     9         /// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" />
    10         /// fires that is associated with the <see cref="IJob" />.
    11         /// </summary>
    12         /// <remarks>
    13         /// The implementation may wish to set a  result object on the 
    14         /// JobExecutionContext before this method exits.  The result itself
    15         /// is meaningless to Quartz, but may be informative to 
    16         /// <see cref="IJobListener" />s or 
    17         /// <see cref="ITriggerListener" />s that are watching the job's 
    18         /// execution.
    19         /// </remarks>
    20         /// <param name="context">The execution context.</param>
    21         public void Execute(IJobExecutionContext context)
    22         {
    23             LogHelper.Info("SampleJob running...");
    24             //Thread.Sleep(TimeSpan.FromSeconds(5));
    25             Console.WriteLine("111111111111111111111");
    26             LogHelper.Info("SampleJob run finished.");
    27         }
    28     }
    View Code

    第二部,配置quartz.config、quartz_jobs.xml

     1 # You can configure your scheduler in either quartz configuration section
     2 # or in quartz properties file
     3 # Configuration section has precedence
     4 
     5 quartz.scheduler.instanceName = QuartzDemo
     6 
     7 # configure thread pool info
     8 quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz
     9 quartz.threadPool.threadCount = 10
    10 quartz.threadPool.threadPriority = Normal
    11 
    12 # job initialization plugin handles our xml reading, without it defaults are used
    13 quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz
    14 quartz.plugin.xml.fileNames = ~/quartz_jobs.xml
    15 
    16 # export this server to remoting context
    17 quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz
    18 quartz.scheduler.exporter.port = 555
    19 quartz.scheduler.exporter.bindName = QuartzScheduler
    20 quartz.scheduler.exporter.channelType = tcp
    21 quartz.scheduler.exporter.channelName = httpQuartz
    View Code
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 
     3 <!-- This file contains job definitions in schema version 2.0 format -->
     4 
     5 <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
     6 
     7   <processing-directives>
     8     <overwrite-existing-data>true</overwrite-existing-data>
     9   </processing-directives>
    10 
    11   <schedule>
    12     <!--用来定义每个具体的任务的,多个任务请创建多个job节点即可-->
    13     <job>
    14       <!--name(必填) 任务名称,同一个group中多个job的name不能相同,若未设置group则所有未设置group的job为同一个分组,如:<name>sampleJob</name>-->
    15       <name>SampleJob</name>
    16       <!--group(选填) 任务所属分组,用于标识任务所属分组,如:<group>sampleGroup</group>-->
    17       <group>SampleGroup</group>
    18       <!--description(选填) 任务描述,用于描述任务具体内容,如:<description>Sample job for Quartz Server</description>-->
    19       <description>SampleJob测试</description>
    20       <!--job-type(必填) 任务类型,任务的具体类型及所属程序集,格式:实现了IJob接口的包含完整命名空间的类名,程序集名称,如:<job-type>Quartz.Server.SampleJob, Quartz.Server</job-type>-->
    21       <job-type>QuartzDemo.SampleJob,QuartzDemo</job-type>
    22       <durable>true</durable>
    23       <recover>false</recover>
    24     </job>
    25     
    26     <!--用于定义使用何种方式出发任务(job),同一个job可以定义多个trigger ,多个trigger 各自独立的执行调度,每个trigger 中必须且只能定义一种触发器类型(calendar-interval、simple、cron)-->
    27     <trigger>
    28       <!--cron复杂任务触发器,使用cron表达式定制任务调度(强烈推荐)-->
    29       <cron>
    30         <!--name(必填) 触发器名称,同一个分组中的名称必须不同-->
    31         <name>TestJobTrigger</name>
    32         <!--group(选填) 触发器组d-->
    33         <group>Test</group>
    34         <!--escription(选填) 触发器描述-->
    35         <description>TestJobTriggerDescription</description>
    36         <!--job-name(必填) 要调度的任务名称,该job-name必须和对应job节点中的name完全相同-->
    37         <job-name>SampleJob</job-name>
    38         <!--job-group(选填) 调度任务(job)所属分组,该值必须和job中的group完全相同-->
    39         <job-group>SampleGroup</job-group>
    40         <!--start-time(选填) 任务开始执行时间utc时间,北京时间需要+08:00,如:<start-time>2012-04-01T08:00:00+08:00</start-time>表示北京时间2012年4月1日上午8:00开始执行,注意服务启动或重启时都会检测此属性,若没有设置此属性,服务会根据cron-expression的设置执行任务调度;若start-time设置的时间比当前时间较早,则服务启动后会忽略掉cron-expression设置,立即执行一次调度,之后再根据cron-expression执行任务调度;若设置的时间比当前时间晚,则服务会在到达设置时间相同后才会应用cron-expression,根据规则执行任务调度,一般若无特殊需要请不要设置此属性-->
    41         <start-time>2015-01-22T00:00:00+08:00</start-time>
    42         <!--cron-expression(必填) cron表达式,如:<cron-expression>0/10 * * * * ?</cron-expression>每10秒执行一次-->
    43         <cron-expression>0/3 * * * * ?</cron-expression>
    44       </cron>
    45     </trigger>
    46 
    47   </schedule>
    48 </job-scheduling-data>
    View Code

    log4net的配置文件没有贴出,有想看的朋友可以去源代码里面查找

    第三部,结合topshelf调度任务

     1 public class QuartzServer
     2     {
     3         /// <summary>
     4         /// 作业调度器
     5         /// </summary>
     6         private readonly IScheduler scheduler;
     7 
     8         public QuartzServer()
     9         {
    10             scheduler = StdSchedulerFactory.GetDefaultScheduler();
    11         }
    12 
    13         /// <summary>
    14         /// 服务启动
    15         /// </summary>
    16         public void Start()
    17         {
    18             LogHelper.Info("Start...");
    19             scheduler.Start();
    20         }
    21         /// <summary>
    22         /// 服务停止
    23         /// </summary>
    24         public void Stop()
    25         {
    26             LogHelper.Info("Stop...");
    27             scheduler.Shutdown(false);
    28         }
    29         /// <summary>
    30         /// 继续服务
    31         /// </summary>
    32         public void Continue()
    33         {
    34             LogHelper.Info("Continue...");
    35             scheduler.ResumeAll();
    36         }
    37         /// <summary>
    38         /// 服务暂停
    39         /// </summary>
    40         public void Pause()
    41         {
    42             LogHelper.Info("Pause...");
    43             scheduler.PauseAll();
    44         }
    45     }
    View Code

    第四部,配置windows service服务,采用的通用模式

     1 class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             HostFactory.Run(x =>
     6             {
     7                 x.Service<QuartzServer>(s =>
     8                 {
     9                     //3、配置一个完全定制的服务,对Topshelf没有依赖关系。常用的方式。
    10                     s.ConstructUsing(name => new QuartzServer());
    11                     //4、服务开始的事件
    12                     s.WhenStarted(tc => tc.Start());
    13                     //5、服务停止以后事件
    14                     s.WhenStopped(tc => tc.Stop());
    15                 });
    16 
    17                 x.RunAsLocalSystem();
    18 
    19                 x.SetDescription("QuartzDemoDescription");
    20                 x.SetDisplayName("QuartzDemoDisplayName");
    21                 x.SetServiceName("QuartzDemoServiceName");
    22             });
    23         }
    24     }
    View Code

    第五步,安装windows service服务,具体见topshelf篇

    服务运行如下图

    Quartz配置

    quartz_jobs.xml

    job 任务

    其实就是1.x版本中的<job-detail>,这个节点是用来定义每个具体的任务的,多个任务请创建多个job节点即可

    • name(必填) 任务名称,同一个group中多个job的name不能相同,若未设置group则所有未设置group的job为同一个分组,如:<name>sampleJob</name>
    • group(选填) 任务所属分组,用于标识任务所属分组,如:<group>sampleGroup</group>
    • description(选填) 任务描述,用于描述任务具体内容,如:<description>Sample job for Quartz Server</description>
    • job-type(必填) 任务类型,任务的具体类型及所属程序集,格式:实现了IJob接口的包含完整命名空间的类名,程序集名称,如:<job-type>Quartz.Server.SampleJob, Quartz.Server</job-type>
    • durable(选填) 具体作用不知,官方示例中默认为true,如:<durable>true</durable>
    • recover(选填) 具体作用不知,官方示例中默认为false,如:<recover>false</recover>

    trigger 任务触发器

    用于定义使用何种方式出发任务(job),同一个job可以定义多个trigger ,多个trigger 各自独立的执行调度,每个trigger 中必须且只能定义一种触发器类型(calendar-interval、simple、cron)

    calendar-interval 一种触发器类型,使用较少,此处略过

    simple 简单任务的触发器,可以调度用于重复执行的任务

    • name(必填) 触发器名称,同一个分组中的名称必须不同
    • group(选填) 触发器组
    • description(选填) 触发器描述
    • job-name(必填) 要调度的任务名称,该job-name必须和对应job节点中的name完全相同
    • job-group(选填) 调度任务(job)所属分组,该值必须和job中的group完全相同
    • start-time(选填) 任务开始执行时间utc时间,北京时间需要+08:00,如:<start-time>2012-04-01T08:00:00+08:00</start-time>表示北京时间2012年4月1日上午8:00开始执行,注意服务启动或重启时都会检测此属性,若没有设置此属性或者start-time设置的时间比当前时间较早,则服务启动后会立即执行一次调度,若设置的时间比当前时间晚,服务会等到设置时间相同后才会第一次执行任务,一般若无特殊需要请不要设置此属性
    • repeat-count(必填)  任务执行次数,如:<repeat-count>-1</repeat-count>表示无限次执行,<repeat-count>10</repeat-count>表示执行10次
    • repeat-interval(必填) 任务触发间隔(毫秒),如:<repeat-interval>10000</repeat-interval> 每10秒执行一次

    cron复杂任务触发器--使用cron表达式定制任务调度(强烈推荐)

    • name(必填) 触发器名称,同一个分组中的名称必须不同
    • group(选填) 触发器组d
    • escription(选填) 触发器描述
    • job-name(必填) 要调度的任务名称,该job-name必须和对应job节点中的name完全相同
    • job-group(选填) 调度任务(job)所属分组,该值必须和job中的group完全相同
    • start-time(选填) 任务开始执行时间utc时间,北京时间需要+08:00,如:<start-time>2012-04-01T08:00:00+08:00</start-time>表示北京时间2012年4月1日上午8:00开始执行,注意服务启动或重启时都会检测此属性,若没有设置此属性,服务会根据cron-expression的设置执行任务调度;若start-time设置的时间比当前时间较早,则服务启动后会忽略掉cron-expression设置,立即执行一次调度,之后再根据cron-expression执行任务调度;若设置的时间比当前时间晚,则服务会在到达设置时间相同后才会应用cron-expression,根据规则执行任务调度,一般若无特殊需要请不要设置此属性
    • cron-expression(必填) cron表达式,如:<cron-expression>0/10 * * * * ?</cron-expression>每10秒执行一次

    Quartz的cron表达式

     官方英文介绍地址:http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/crontrigger.html

    cron expressions 整体上还是非常容易理解的,只有一点需要注意:"?"号的用法,看下文可以知道“?”可以用在 day of month 和 day of week中,他主要是为了解决如下场景,如:每月的1号的每小时的31分钟,正确的表达式是:* 31 * 1 * ?,而不能是:* 31 * 1 * *,因为这样代表每周的任意一天。


    由7段构成:秒 分 时 日 月 星期 年(可选)
    "-" :表示范围  MON-WED表示星期一到星期三
    "," :表示列举 MON,WEB表示星期一和星期三
    "*" :表是“每”,每月,每天,每周,每年等
    "/" :表示增量:0/15(处于分钟段里面) 每15分钟,在0分以后开始,3/20 每20分钟,从3分钟以后开始
    "?" :只能出现在日,星期段里面,表示不指定具体的值
    "L" :只能出现在日,星期段里面,是Last的缩写,一个月的最后一天,一个星期的最后一天(星期六)
    "W" :表示工作日,距离给定值最近的工作日
    "#" :表示一个月的第几个星期几,例如:"6#3"表示每个月的第三个星期五(1=SUN...6=FRI,7=SAT)

    官方实例

    ExpressionMeaning
    0 0 12 * * ? 每天中午12点触发
    0 15 10 ? * * 每天上午10:15触发
    0 15 10 * * ? 每天上午10:15触发
    0 15 10 * * ? * 每天上午10:15触发
    0 15 10 * * ? 2005 2005年的每天上午10:15触发
    0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发
    0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
    0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
    0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发
    0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发
    0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
    0 15 10 15 * ? 每月15日上午10:15触发
    0 15 10 L * ? 每月最后一日的上午10:15触发
    0 15 10 L-2 * ? Fire at 10:15am on the 2nd-to-last last day of every month
    0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
    0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
    0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发
    0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发
    0 0 12 1/5 * ? Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.
    0 11 11 11 11 ? Fire every November 11th at 11:11am.

    附上demo地址

    https://github.com/xiaopotian1990/QuartzDemo

  • 相关阅读:
    windows内核函数1
    驱动程序中获取当前进程的进程名的方法
    vs2008+WDK7600驱动开发环境配置
    P2P之UDP穿透NAT的原理与实现之我见
    设计模式-工厂模式
    C++解析ini文件_转载
    利用sort和lambda表达式对vector中的pair进行排序
    C++ 获取目录下具有指定后缀名的所有文件名_windows
    深度学习提取得到的特征值进行特征值相似度比对
    C/C++从路径字符串中获取文件名
  • 原文地址:https://www.cnblogs.com/xiaopotian/p/5437364.html
Copyright © 2011-2022 走看看