前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Quartz.net通过配置文件来完成作业调度

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

作者头像
张善友
发布2018-01-22 10:33:10
9540
发布2018-01-22 10:33:10
举报
文章被收录于专栏:张善友的专栏

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

代码语言:js
复制
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,设置为嵌入资源类型。内容如下:

代码语言:js
复制
<?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文件,例如:

代码语言:js
复制
            // 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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2007-12-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档