专栏首页张善友的专栏Quartz.net通过配置文件来完成作业调度

Quartz.net通过配置文件来完成作业调度

将Quartz.NET集成到 Castle中 例子代码使用的Quartz.net版本是0.6,Quartz.NET 0.9 发布了 ,最新版本支持通过配置文件来完成后台的作业调度,不必手工创建Trigger和Scheduler。将QuartzStartable 改造如下:

using System;
using System.Collections.Generic;
using System.Text;
using Castle.Core;
using Quartz.Impl;
using Quartz;
using Common.Logging;
using System.Threading;
using System.IO;
using Quartz.Xml;
using System.Collections;
namespace QuartzComponent
{
    [Transient]
    public class QuartzStartable : IStartable
    {
        private ISchedulerFactory _schedFactory;
        private JobSchedulingDataProcessor processor;
        private static ILog log = LogManager.GetLogger(typeof(QuartzStartable));
        public QuartzStartable(ISchedulerFactory schedFactory)
        {
            _schedFactory = schedFactory;
            processor = new JobSchedulingDataProcessor(true, true);
        }
        public void Start()
        {
            log.Info("Starting service");
            IScheduler sched = _schedFactory.GetScheduler();
            //log.Info("------- Scheduling Jobs ----------------");
            //// jobs can be scheduled before sched.start() has been called
            //// get a "nice round" time a few seconds in the future...
            //DateTime ts = TriggerUtils.GetNextGivenSecondDate(null, 15);
            //// job1 will only fire once at date/time "ts"
            //JobDetail job = new JobDetail("job1", "group1", typeof(SimpleQuartzJob));
            //SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1");
            //// set its start up time
            //trigger.StartTimeUtc = ts;
            //// set the interval, how often the job should run (10 seconds here) 
            //trigger.RepeatInterval = 10000;
            //// set the number of execution of this job, set to 10 times. 
            //// It will run 10 time and exhaust.
            //trigger.RepeatCount = 100;

            //// schedule it to run!
            //DateTime ft = sched.ScheduleJob(job, trigger);
            //log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds",
            //    job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval / 1000)));
            //log.Info("------- Waiting five minutes... ------------");
            //sched.Start();
            Stream s = ReadJobXmlFromEmbeddedResource("MinimalConfiguration.xml");
            processor.ProcessStream(s, null);
            processor.ScheduleJobs(new Hashtable(), sched, false);
            sched.Start();
            try
            {
                // wait five minutes to show jobs
                Thread.Sleep(300 * 1000);
                // executing...
            }
            catch (ThreadInterruptedException)
            {
            }

        }
        private static Stream ReadJobXmlFromEmbeddedResource(string resourceName)
        {
            string fullName = "QuartzComponent." + resourceName;
            return new StreamReader(typeof(QuartzStartable).Assembly.GetManifestResourceStream(fullName)).BaseStream;
        }
        public void Stop()
        {
            log.Info("Stopping service");
            try
            {
                IScheduler scheduler = _schedFactory.GetScheduler();
                scheduler.Shutdown(true);
            }
            catch (SchedulerException se)
            {
                log.Error("Cannot shutdown scheduler.", se);
            }
        }
    }
}

增加一个配置文件MinimalConfiguration.xml,设置为嵌入资源类型。内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<quartz xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="1.0"
    overwrite-existing-jobs="true">
 
  <job>
  <job-detail>
   <name>jobName1</name>
   <group>jobGroup1</group>
   <job-type>QuartzComponent.SimpleQuartzJob, QuartzComponent</job-type>
  </job-detail>
 
  <trigger>
   <simple>
    <name>simpleName</name>
    <group>simpleGroup</group>
    <job-name>jobName1</job-name>
    <job-group>jobGroup1</job-group>
    <start-time>2007-12-09T18:08:50</start-time>
    <repeat-count>100</repeat-count>
    <repeat-interval>3000</repeat-interval>
   </simple>
      </trigger>
   <trigger>
    <cron>
     <name>cronName</name>
     <group>cronGroup</group>
     <job-name>jobName1</job-name>
     <job-group>jobGroup1</job-group>
     <start-time>1982-06-28T18:15:00+02:00</start-time>
     <cron-expression>0/10 * * * * ?</cron-expression>
    </cron>
   </trigger>
 </job>
</quartz>

可以看到,在配置文件中把jobdetail和trigger都作了完整的定义,并组合成一个job。

当然也可以在quartz.properties文件中设置一个quertz_job.xml文件,例如:

            // First we must get a reference to a scheduler
            NameValueCollection properties = new NameValueCollection();
            properties["quartz.scheduler.instanceName"] = "XmlConfiguredInstance";
 
            // set thread pool info
            properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
            properties["quartz.threadPool.threadCount"] = "5";
            properties["quartz.threadPool.threadPriority"] = "Normal";
            // job initialization plugin handles our xml reading, without it defaults are used
            properties["quartz.plugin.xml.type"] = "Quartz.Plugin.Xml.JobInitializationPlugin, Quartz";
            properties["quartz.plugin.xml.fileNames"] = "~/quartz_jobs.xml";
            ISchedulerFactory sf = new StdSchedulerFactory(properties);

这样,在启动Castle的时候,Quartz.Plugin.Xml.JobInitializationPlugin就会自动读取quartz.properties这个配置文件,并初始化调度信息,启动Scheduler。

一个Job类,一个quartz.properties文件,一个quertz_job.xml文件,非常简单灵活。

下载例子代码 :QuartzComponentWithXml.zip

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Quartz.net官方开发指南 第二课:Jobs And Triggers

    正如前面所提到的那样,通过实现IJob接口来使你的.NET组件可以很简单地被scheduler执行。下面是IJob接口: namespace Quartz ...

    张善友
  • SharePoint Foundation 2010

    下图是一张SharePoint 2010基本架构图,它简要的描述出了SharePoint 2010的基本结构。SharePoint Foundation在之前的...

    张善友
  • Quartz.net官方开发指南 第十一课: 高级(企业级)属性

    Clustering 集群 ( Clustering从0.6版本开始可用了) 目前,集群只能用在使用ADO.NET-Jobstore的情况。特新包括负载均衡和容...

    张善友
  • 使用 Oracle Datapump API 实现数据导出

      Oracle Datapump API 是基于PL/SQL实现的,是命令行方式下的补充。使用Datapump API可以将其逻辑备份特性将其集成到应用程序当...

    Leshami
  • hadoop2-MapReduce详解

    本文是对Hadoop2.2.0版本的MapReduce进行详细讲解。请大家要注意版本,因为Hadoop的不同版本,源码可能是不同的。

    Hongten
  • Laravle Job 案例

    make:job 创建一个新JOb类 protected $msg; /** * Create a new job instance. * * @ret...

    苦咖啡
  • 芯片巨头赛灵思刚刚宣布,收购中国初创公司深鉴科技

    今晨,国际芯片巨头赛灵思公司(Xilinx, Inc.,(NASDAQ:XLNX))宣布已经完成了对中国芯片初创公司深鉴科技的收购。之后,深鉴科技官方也宣布了被...

    大数据文摘
  • ASP.NET MVC5+EF6+EasyUI 后台管理系统(85)-Quartz 作业调度用法详解二

     我们在安装了Quartz后,会在目录下生成job_scheduling_data_2_0.xsd文件,此文件是XML配置智能提示用,如图所示:有利于我们编写X...

    用户1149182
  • ASP.NET MVC5+EF6+EasyUI 后台管理系统(85)-Quartz 作业调度用法详解二

    前言 上一节我们学习了Quartz的基本用法 这一节学习通过XML配置的形式来执行任务 这一节主要认识一些属性,为下一步打基础 代码下载:链接:htt...

    用户1149182
  • 16位汇编第三讲 分段存储管理思想

          内存分段 一丶分段(汇编指令分段) 1.为什么分段?   因为分段是为了更好的管理数据和代码,就好比C语言为什么会有内存4区一样,否则汇编代码都写...

    IBinary

扫码关注云+社区

领取腾讯云代金券