Fork me on GitHub

Quartz.NET基础知识概述

Quartz.NET是什么

由于我现在使用的Quartz.NET2.2版本,相对2.x变化不大,主要是相对于1.x更新了很多东西,如下基础知识摘录网络。
Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中。它提供了巨大的灵活性而不牺牲简单性。你能够用它来
为执行一个作业而创建简单的或复杂的调度。它有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等等。
Quartz.NET框架的核心是调度器。调度器负责管理Quartz.NET应用运行时环境。Quartz不仅仅是线程和线程管理。为确保可伸缩性,Quartz.NET采用了基于多线程的架构。启动时,框架初始
化一套worker线程,这套线程被调度器用来执行预定的作业。这就是Quartz.NET怎样能并发运行多个作业的原理。Quartz.NET依赖一套松耦合的线程池管理部件来管理线程环境。作业是一个执行任务的简单.NET类。只需实现Quartz.IJob接口,IJob接口包含唯一的一个方法Execute(),作业从这里开始执行。一旦实现了IJob接口和Execute ()方法,当Quartz.NET确定该是作业运行的时候,它将调用作业。Execute()方法内就是要做的事情。

Quartz.NET有如下几个特点

API 操作简单,只要几行简单的代码你就可以在应用程序里面实现自己的作业调度,并实时监视作业执行情况
触发器功能强大,比 Windows 的任务计划提供更细的触发粒度,你可以使用Cron表达式来实现如每周星期一到星期五8:00am,5:00pm(工作时间)执行某一件任务
良好的可扩展性,它基于接口编程,你可以实现自己的 Schedule 调度器,Job 作业,以及 Trigger 触发器等
作业可以保存在 RAM 中,也可以持久化到数据库,支持多种数据库类型:SqlServer、Oracle、MySql等
集群,这是一个高级应用,可以在多台计算机之间创建负载平衡、容错处理

Quartz.NET 2.0与以前版本的改进

2012年4月9日 Quartz.NET 2.0发布了Released版,对应于Java Quartz的2.1版本,下载地址 http://quartznet.sourceforge.net/download.html 。整个版本相对于1.0版本进行了大量的修改,单元测试的代码更友好(重构了更多的接口),API是基于泛型和.NET 3.5 SP1之后的特性,例如DateTimeOffset。这是Quartz.NET 有史以来最大的、最值得兴奋的一个版本。

该版本除了在性能上有所提升外,增加了如下新特性

  • Scheduler.Clear() 提供方便用于清除所有任务、触发器和日程的方法
  • Scheduler.ScheduleJobs((IDictionary> triggersAndJobs, boolean replace) 方法可批量增加任务和触发器
  • Scheduler.UnscheduleJobs(IList triggerKeys) 方法提供批量取消任务
  • Scheduler.DeleteJobs(IList jobKeys) 批量删除任务
  • Scheduler.CheckExists(JobKey jobKey) 和 Scheduler.CheckExists(TriggerKey triggerKey)提供用于检测任务关键字的唯一性
  • AdoJobStore allows one set of tables to be used by multiple distinct scheduler instances
  • AdoJobStore is now capable of storing non-core Trigger implementations without using BLOB columns, through the use of the new TriggerPersistenceDelegate interface, which can (optionally) be implemented by implementers of custom Trigger types.
  • Cron 表达式支持指定每月的最后一天和最后一周,例如 L-3 为每月的最后三天
  • 包含调度信息的 XML 文件增加了用来指定启动时间和间隔时间的方法
  • XML 文件支持为触发器指定 priority 属性
  • 增加核心任务 DirectoryScanJob ,之前的FileScanJob 添加了一个迷你mum age参数

2.0在API上也做了重大的修改,API返回值的集合和泛型的使用,消除歧义和冗余代码,掩藏/删除不应该公开给客户端的方法,提高关注点分离,并引入 与领域特定语言DSL的核心实体(jobs and triggers),自然就有了兼容性等问题,我们现在来看下都有哪些重大的修改:

  • 不在支持.NET 1.1和2.0,需要Quartz.net 2.0至少需要.NET 3.5 SP1,是由于采用了新的语言特性和类接口
  • 许多公共接口的返回值Array更改为泛型的IList和ISet,例如GetJobGroupNames(): string[] 现在更改为 GetJobGroupNames(): IList ,现在用来标识Jobs和Triggers 现在是基于JobsKey和TriggersKey,Keys 包含一个Name和一个Group。操作特定jobs/triggers 的方法使用Keys作为参数。例如GetTrigger(TriggerKey key): ITrigger 替换了GetTrigger(string name, string group): Trigger,ITrigger现在是一个接口而不是类。ISimpleTrigger, ICronTrigger也一样。新的 DSL/builder-based API 组织Jobs和Triggers。
     IJobDetail job = JobBuilder.Create<SimpleJob>() .WithIdentity("job1", "group1") .Build();
     ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartAt(DateBuilder.FutureDate(2, IntervalUnit.HOURS)) .WithSimpleSchedule(x => x.RepeatHourlyForever()) .ModifiedByCalendar("holidays") .Build();
  • JobInitializationPlugin 已经移除,用XMLSchedulingDataProcessorPlugin替代
  • Microsoft's Oracle drivers已经不再支持,使用10g 或者 11g ODP.NET drivers替代
  • 数据库的Sechema已经修改,需要使用database目录下的脚本吧1.x的Schema升级到2.0
变化一 比1.0多引用了C5.dll
C5.dll 一个C#和其他CLI语言的泛型集合类。.Net2.0及以上才可以使用。简介地址:http://www.itu.dk/research/c5
变化二 quartz.config有细微变化
  • quartz.plugin.xml.type 由1.x的Quartz.Plugin.Xml.JobInitializationPlugin, Quartz变为了2.0中的Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz
  • 2.0版本新增了一行配置quartz.scheduler.exporter.channelName = httpQuart
变化三 实现IJob接口 JobExecutionContext对象变成了IJobExecutionContext
变化四 quartz_jobs.xml配置节发生了变化
变化五 支持.Net版本不同
    • Quartz 1.0可以支持.Net 1.1 和 .Net 2.0及以上版本 
    • Quartz 2.0仅支持.Net 3.5及以上版本,放弃了对.Net 1.1和.Net 2.0的支持

Quartz.NET 2.2 ChangeLog

This release contains important bug fixes, new functionality and minor breaking changes.
UPGRADING
Please examine and run the database\schema_20_to_22_upgrade.sql script if you are using AdoJobStore
    this script adds a new column SCHED_TIME to table QRTZ_FIRED_TRIGGERS
    file contains the alter command for SQL Server and other database samples in comments
BREAKING CHANGES
    database schema needs upgrade
    add SchedulerStarting() method to ISchedulerListener interface
    make the scheduler's TypeLoadHelper available to plugins when they are initialized
    dbFailureRetryInterval parameter was removed from DirectSchedulerFactory APIs
NEW FEATURES
    ability to override worker thread names (when using SimpleThreadPool)
    add new IScheduler method: ScheduleJob(IJobDetail job, ISet trigger) to schedule multiple triggers for a job all at once
    allow 'triggerless' initial storing of non-durable jobs.
    improvements for job recovery information
    package job_scheduling_data_2_0.xsd to nuget package's content folder
    allow scheduler exported with remoting to be used from local machine only
    support for Oracle managed ODP driver
FIXES
    job ending with exception and trigger not going to fire again, trigger is incorrectly not removed from job store
    XML schema supports multiple schedule elements but processor does not
    DailyTimeIntervalTriggerPersistenceDelegate does not handle empty time interval properly
    DailyTimeIntervalScheduleBuilder.EndingDailyAfterCount(...) doesn't pass validation
    trace throwing exception
    bug in QuartzSchedulerThread.GetRandomizedIdleWaitTime()
    can't delete or replace job without the referenced class
MISC
    Performance improvements, including improvements to some select statements in AdoJobStore

posted @ 2013-11-17 01:09  花儿笑弯了腰  阅读(1082)  评论(0编辑  收藏  举报