Quartz.NET学习系列(九)--- XML任务配置

        Quartz.NET除了可以代码定义触发器、任务之外,还可以由XML定义这两个对象。其实Quartz.NET的理想架构是:计划管理容器作为服务器运行在服务机上,而触发器、任务的添加可通过自定义任务管理软件向服务器上添加特定的任务计划。 这时就要求数据的持久化以及服务器的容错性了,后面几篇文章将会介绍持久化以及集群相关内容。

VS的XML只能提示

        第一篇已经将Quartz.NET下载过来了,在路径doc/xml/文件加下可以找到job_scheduling_data_2_0.xsd vs的智能提示配置,将这个文件放在VS安装目录下的Xml/Schemas/文件夹里,重启VS,就可以在XML里有Quartz.NET的只能提示了,截图如下

技术分享


XML任务配置

        下面开始学习下XML任务配置,代码如下:

    public class PlugInExample 
    {
        public static void Run()
        {
            ILog log = LogManager.GetLogger(typeof (PlugInExample));

            
            var properties = new NameValueCollection();
            properties["quartz.plugin.triggHistory.type"] = "Quartz.Plugin.History.LoggingJobHistoryPlugin";

            properties["quartz.plugin.jobInitializer.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin";
            properties["quartz.plugin.jobInitializer.fileNames"] = "quartz_jobs.xml";
            properties["quartz.plugin.jobInitializer.failOnFileNotFound"] = "true";
            properties["quartz.plugin.jobInitializer.scanInterval"] = "1";
            //或者
            //properties["quartz.plugin.xml.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz";
            //properties["quartz.plugin.xml.fileNames"] = "~/quartz_jobs.xml";

            ISchedulerFactory sf = new StdSchedulerFactory(properties);
            IScheduler sched = sf.GetScheduler();

            sched.Start();

            log.Info("-------开始计划 -----------------");
            Thread.Sleep(TimeSpan.FromMinutes(5));
            sched.Shutdown(true);
            log.Info("-------关闭计划 -----------------");

            SchedulerMetaData metaData = sched.GetMetaData();
            log.Info("执行次数 " + metaData.NumberOfJobsExecuted );
        }
    }
quartz_jobs.xml任务定义XML如下:

<?xml version="1.0" encoding="UTF-8"?>

<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 				version="2.0">

  <processing-directives>
    <overwrite-existing-data>true</overwrite-existing-data>
  </processing-directives>

  <schedule>
    
    <job>
      <name>jobName1</name>
      <group>jobGroup1</group>
      <description>jobDesciption1</description>
      <job-type>Quartz.Examples.Example10.SimpleJob, QuartzDemo</job-type>
      <durable>true</durable>
      <recover>false</recover>
      <job-data-map>
        <entry>
          <key>key0</key>
          <value>value0</value>
        </entry>
        <entry>
          <key>key1</key>
          <value>value1</value>
        </entry>
        <entry>
          <key>key2</key>
          <value>value2</value>
        </entry>
      </job-data-map>
    </job>
    
    <trigger>
      <simple>
        <name>simpleName</name>
        <group>simpleGroup</group>
        <description>SimpleTriggerDescription</description>
        <job-name>jobName1</job-name>
        <job-group>jobGroup1</job-group>
        <start-time>1982-06-28T18:15:00.0Z</start-time>
        <end-time>2020-05-04T18:13:51.0Z</end-time>
        <misfire-instruction>SmartPolicy</misfire-instruction>
        <repeat-count>100</repeat-count>
        <repeat-interval>100000</repeat-interval>
      </simple>
    </trigger>

  </schedule>
  
</job-scheduling-data>


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。