将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

转载于:https://www.cnblogs.com/shanyou/archive/2007/12/09/988502.html

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

  1. Quartz.NET作业调度框架详解

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...

  2. Web项目中使用Spring 3.x + Quartz 2.x实现作业调度详解

    Quartz是一个基于Java的作业调度管理的轻量级框架,目前在很多企业应用中被使用,它的作用类似于java.util中的Timer和TimeTask.数据库中的job等,但Quartz的功能更强大. ...

  3. C# Quartz作业调度配置

    什么是Quartz Quartz是一个开源的作业调度框架,Quartz根据用户设定的时间规则来执行作业,使用场景:在平时的工作中,估计大多数都做过轮询调度的任务,比如定时轮询数据库同步,定时邮件通知. ...

  4. quartz.properties配置文件详解

    我们通常是通过quartz.properties属性配置文件(默认情况下均使用该文件)结合StdSchedulerFactory 来使用Quartz的.StdSchedulerFactory 会加载属 ...

  5. .Net平台开源作业调度框架Quartz.Net

    Quartz.NET介绍: Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中. ...

  6. ABP后台服务之作业调度Quartz.NET

    一.简介 Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活 ...

  7. java quartz 源码_Quartz开源作业调度库 v2.3.2

    Quartz是功能强大的开源作业调度库,几乎可以集成到任何Java应用程序中-从最小的独立应用程序到最大的电子商务系统.Quartz可用于创建简单或复杂的计划,以执行数以万计,数以万计的工作.任务定义 ...

  8. 【配置详解】Quartz配置文件详解

    我们通常是通过quartz.properties属性配置文件(默认情况下均使用该文件)结合StdSchedulerFactory 来使用Quartz的.StdSchedulerFactory 会加载属 ...

  9. C#作业调度Quartz简单使用

    首先,作业调度Quartz的定义是: Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.ne ...

最新文章

  1. java swing图形界面开发与案例详解source code
  2. linux程序实例获取,Linux命令备忘实例(4)——获取内容
  3. sql相同顺序法和一次封锁法_数据库:事务处理
  4. 空军军医大学计算机复试线,空军军医大学2019年考研复试分数线
  5. 深度学习优化算法大全系列6:Adam
  6. Android 高德地图自定义InfoWindow
  7. 大规模MIMO多用户系统中的导频调度和预编码方法
  8. go 1.16版本,go get用法介绍
  9. 鸿蒙方将腐皮雀跃而有,鸿蒙是谁:生于庄子,火于华为
  10. 成都链安xFootprint 2022 Web3 安全研报
  11. win10卸载db2_怎么在windows下正确卸载DB2
  12. mysql 8 全文检索_MySQL 8中使用全文检索示例
  13. Linux CentOS7.0 使用root登录桌面
  14. 计算机windows7更新失败,Win7电脑windows update更新失败如何解决?
  15. 程序员加班看不上球赛崩溃,外卖小哥伸出援手:我帮你改代码
  16. 基于Pytorch的强化学习(DQN)之 Experience Replay
  17. COMSOL中电磁场物理场接口中线圈的仿真
  18. VB.net与VB6 调用Websocket功能的方法--Websocket For VB
  19. 猿如意|chat GPT测评
  20. 2022年医药行业数据库系统V4.0 pharnexcloud(库群介绍)

热门文章

  1. 常发生的异常有哪些, 如何使用异常?
  2. Mysql 5.8 参数调优
  3. Spring源码分析-深入理解生命周期之BeanFactoryProcessor
  4. JavaScript中Map的应用及Map中的bug
  5. 微软官方windows phone开发视频教程第二天视频(附下载地址)
  6. linux-Centos 7下bond与vlan技术的结合
  7. 【ARM】arm异常中断处理知识点
  8. view技术简单了解
  9. 关于WEB三层架构的思考
  10. 手把手教你从零构建属于自己的小linux