Quartz.NET
概述
Quartz.NET是一個開源的作業(yè)調(diào)度框架,非常適合在平時的工作中,定時輪詢數(shù)據(jù)庫同步,定時郵件通知,定時處理數(shù)據(jù)等。?Quartz.NET允許開發(fā)人員根據(jù)時間間隔(或天)來調(diào)度作業(yè)。它實現(xiàn)了作業(yè)和觸發(fā)器的多對多關(guān)系,還能把多個作業(yè)與不同的觸發(fā)器關(guān)聯(lián)。整合了 Quartz.NET的應(yīng)用程序可以重用來自不同事件的作業(yè),還可以為一個事件組合多個作業(yè)。
參考
官方學習文檔:http://www.quartz-scheduler.net/documentation/index.html
使用實例介紹:http://www.quartz-scheduler.net/documentation/quartz-2.x/quick-start.html
官方的源代碼下載:http://sourceforge.net/projects/quartznet/files/quartznet/? ?或者到我上傳的csdn下載:http://download.csdn.net/detail/jys1216/8878305
下載下來官方的例子,我們來分析一下:
解壓后,看到的文檔
打開后,看到的項目結(jié)構(gòu)如下:
項目可以直接運行:
運行后,我們可以看到,每隔10秒有輸出,那是因為,在配置quart.net的服務(wù)文件里,配置了每10秒執(zhí)行一次
快速搭建一個Quartz
第一步:安裝
新建一個QuartzDemo項目后,安裝下面的程序包
- Install-Package Quartz
- Install-Package Common.Logging.Log4Net1211
- Install-Package?log4net
- Install-Package?Topshelf
- Install-Package Topshelf.Log4Net
?Quartz依賴Common.Logging和Common.Logging.Log4Net1211,又因為Log4Net是比較標準的日志工具,因此我們一般都會安裝log4net,另外定時作業(yè)一般都允許在后臺服務(wù)中,因此我們也安裝了Topshelf。
第二步:實現(xiàn)IJob
TestJob.cs 實現(xiàn)IJob,在Execute方法里編寫要處理的業(yè)務(wù)邏輯,系統(tǒng)就會按照Quartz的配置,定時處理。
using log4net; using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace QuartzDemo.QuartzJobs {public sealed class TestJob : IJob{private readonly ILog _logger = LogManager.GetLogger(typeof(TestJob));public void Execute(IJobExecutionContext context){_logger.InfoFormat("TestJob測試");}} }第三步:使用Topshelf調(diào)度任務(wù)
Topshelf的使用介紹,請看我的另一遍介紹:http://www.cnblogs.com/jys509/p/4614975.html
ServiceRunner.cs
using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Topshelf;namespace QuartzDemo {public sealed class ServiceRunner : ServiceControl, ServiceSuspend{private readonly IScheduler scheduler;public ServiceRunner(){scheduler = StdSchedulerFactory.GetDefaultScheduler();}public bool Start(HostControl hostControl){scheduler.Start();return true;}public bool Stop(HostControl hostControl){scheduler.Shutdown(false);return true;}public bool Continue(HostControl hostControl){scheduler.ResumeAll();return true;}public bool Pause(HostControl hostControl){scheduler.PauseAll();return true;}} }第四步:程序入口
namespace QuartzDemo {class Program{static void Main(string[] args){log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));HostFactory.Run(x =>{x.UseLog4Net();x.Service<ServiceRunner>();x.SetDescription("QuartzDemo服務(wù)描述");x.SetDisplayName("QuartzDemo服務(wù)顯示名稱");x.SetServiceName("QuartzDemo服務(wù)名稱");x.EnablePauseAndContinue();});}} }第五步:配置quartz.config、quartz_jobs.xml、log4net.config
說明:這三個文件,分別選中→右鍵屬性→復(fù)制到輸入目錄設(shè)為:始終復(fù)制
quartz.config
# You can configure your scheduler in either <quartz> configuration section # or in quartz properties file # Configuration section has precedencequartz.scheduler.instanceName = QuartzTest# configure thread pool info quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz quartz.threadPool.threadCount = 10 quartz.threadPool.threadPriority = Normal# job initialization plugin handles our xml reading, without it defaults are used quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz quartz.plugin.xml.fileNames = ~/quartz_jobs.xml# export this server to remoting context #quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz #quartz.scheduler.exporter.port = 555 #quartz.scheduler.exporter.bindName = QuartzScheduler #quartz.scheduler.exporter.channelType = tcp #quartz.scheduler.exporter.channelName = httpQuartzquartz_jobs.xml
<?xml version="1.0" encoding="UTF-8"?><!-- This file contains job definitions in schema version 2.0 format --><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><!--TestJob測試 任務(wù)配置--><job><name>TestJob</name><group>Test</group><description>TestJob測試</description><job-type>QuartzDemo.QuartzJobs.TestJob,QuartzDemo</job-type><durable>true</durable><recover>false</recover></job><trigger><cron><name>TestJobTrigger</name><group>Test</group><job-name>TestJob</job-name><job-group>Test</job-group><start-time>2015-01-22T00:00:00+08:00</start-time><cron-expression>0/3 * * * * ?</cron-expression></cron></trigger></schedule> </job-scheduling-data>log4net.config
<?xml version="1.0" encoding="utf-8" ?> <configuration><configSections><section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/></configSections><log4net><appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"><!--日志路徑--><param name= "File" value= "D:\App_Log\servicelog\"/><!--是否是向文件中追加日志--><param name= "AppendToFile" value= "true"/><!--log保留天數(shù)--><param name= "MaxSizeRollBackups" value= "10"/><!--日志文件名是否是固定不變的--><param name= "StaticLogFileName" value= "false"/><!--日志文件名格式為:2008-08-31.log--><param name= "DatePattern" value= "yyyy-MM-dd".read.log""/><!--日志根據(jù)日期滾動--><param name= "RollingStyle" value= "Date"/><layout type="log4net.Layout.PatternLayout"><param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" /></layout></appender><!-- 控制臺前臺顯示日志 --><appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"><mapping><level value="ERROR" /><foreColor value="Red, HighIntensity" /></mapping><mapping><level value="Info" /><foreColor value="Green" /></mapping><layout type="log4net.Layout.PatternLayout"><conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" /></layout><filter type="log4net.Filter.LevelRangeFilter"><param name="LevelMin" value="Info" /><param name="LevelMax" value="Fatal" /></filter></appender><root><!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) --><level value="all" /><appender-ref ref="ColoredConsoleAppender"/><appender-ref ref="RollingLogFileAppender"/></root></log4net> </configuration>運行后,效果下圖,每隔3秒有輸出
最后,就是安裝成windows服務(wù)了。具體安裝參考:http://www.cnblogs.com/jys509/p/4614975.html
源碼下載:http://download.csdn.net/download/jys1216/8882315
Quartz配置
quartz_jobs.xml
job 任務(wù)
其實就是1.x版本中的<job-detail>,這個節(jié)點是用來定義每個具體的任務(wù)的,多個任務(wù)請創(chuàng)建多個job節(jié)點即可
- name(必填) 任務(wù)名稱,同一個group中多個job的name不能相同,若未設(shè)置group則所有未設(shè)置group的job為同一個分組,如:<name>sampleJob</name>
- group(選填) 任務(wù)所屬分組,用于標識任務(wù)所屬分組,如:<group>sampleGroup</group>
- description(選填) 任務(wù)描述,用于描述任務(wù)具體內(nèi)容,如:<description>Sample job for Quartz Server</description>
- job-type(必填) 任務(wù)類型,任務(wù)的具體類型及所屬程序集,格式:實現(xiàn)了IJob接口的包含完整命名空間的類名,程序集名稱,如:<job-type>Quartz.Server.SampleJob, Quartz.Server</job-type>
- durable(選填) 具體作用不知,官方示例中默認為true,如:<durable>true</durable>
- recover(選填) 具體作用不知,官方示例中默認為false,如:<recover>false</recover>
trigger 任務(wù)觸發(fā)器
用于定義使用何種方式出發(fā)任務(wù)(job),同一個job可以定義多個trigger ,多個trigger 各自獨立的執(zhí)行調(diào)度,每個trigger 中必須且只能定義一種觸發(fā)器類型(calendar-interval、simple、cron)
calendar-interval 一種觸發(fā)器類型,使用較少,此處略過
simple 簡單任務(wù)的觸發(fā)器,可以調(diào)度用于重復(fù)執(zhí)行的任務(wù)
- name(必填) 觸發(fā)器名稱,同一個分組中的名稱必須不同
- group(選填) 觸發(fā)器組
- description(選填) 觸發(fā)器描述
- job-name(必填) 要調(diào)度的任務(wù)名稱,該job-name必須和對應(yīng)job節(jié)點中的name完全相同
- job-group(選填) 調(diào)度任務(wù)(job)所屬分組,該值必須和job中的group完全相同
- start-time(選填) 任務(wù)開始執(zhí)行時間utc時間,北京時間需要+08:00,如:<start-time>2012-04-01T08:00:00+08:00</start-time>表示北京時間2012年4月1日上午8:00開始執(zhí)行,注意服務(wù)啟動或重啟時都會檢測此屬性,若沒有設(shè)置此屬性或者start-time設(shè)置的時間比當前時間較早,則服務(wù)啟動后會立即執(zhí)行一次調(diào)度,若設(shè)置的時間比當前時間晚,服務(wù)會等到設(shè)置時間相同后才會第一次執(zhí)行任務(wù),一般若無特殊需要請不要設(shè)置此屬性
- repeat-count(必填)? 任務(wù)執(zhí)行次數(shù),如:<repeat-count>-1</repeat-count>表示無限次執(zhí)行,<repeat-count>10</repeat-count>表示執(zhí)行10次
- repeat-interval(必填) 任務(wù)觸發(fā)間隔(毫秒),如:<repeat-interval>10000</repeat-interval> 每10秒執(zhí)行一次
cron復(fù)雜任務(wù)觸發(fā)器--使用cron表達式定制任務(wù)調(diào)度(強烈推薦)
- name(必填) 觸發(fā)器名稱,同一個分組中的名稱必須不同
- group(選填) 觸發(fā)器組d
- escription(選填) 觸發(fā)器描述
- job-name(必填) 要調(diào)度的任務(wù)名稱,該job-name必須和對應(yīng)job節(jié)點中的name完全相同
- job-group(選填) 調(diào)度任務(wù)(job)所屬分組,該值必須和job中的group完全相同
- start-time(選填) 任務(wù)開始執(zhí)行時間utc時間,北京時間需要+08:00,如:<start-time>2012-04-01T08:00:00+08:00</start-time>表示北京時間2012年4月1日上午8:00開始執(zhí)行,注意服務(wù)啟動或重啟時都會檢測此屬性,若沒有設(shè)置此屬性,服務(wù)會根據(jù)cron-expression的設(shè)置執(zhí)行任務(wù)調(diào)度;若start-time設(shè)置的時間比當前時間較早,則服務(wù)啟動后會忽略掉cron-expression設(shè)置,立即執(zhí)行一次調(diào)度,之后再根據(jù)cron-expression執(zhí)行任務(wù)調(diào)度;若設(shè)置的時間比當前時間晚,則服務(wù)會在到達設(shè)置時間相同后才會應(yīng)用cron-expression,根據(jù)規(guī)則執(zhí)行任務(wù)調(diào)度,一般若無特殊需要請不要設(shè)置此屬性
- cron-expression(必填) cron表達式,如:<cron-expression>0/10 * * * * ?</cron-expression>每10秒執(zhí)行一次
Quartz的cron表達式
?官方英文介紹地址:http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/crontrigger.html
cron expressions 整體上還是非常容易理解的,只有一點需要注意:"?"號的用法,看下文可以知道“?”可以用在 day of month 和 day of week中,他主要是為了解決如下場景,如:每月的1號的每小時的31分鐘,正確的表達式是:* 31 * 1 * ?,而不能是:* 31 * 1 * *,因為這樣代表每周的任意一天。
由7段構(gòu)成:秒 分 時 日 月 星期 年(可選)
"-" :表示范圍? MON-WED表示星期一到星期三
"," :表示列舉 MON,WEB表示星期一和星期三
"*" :表是“每”,每月,每天,每周,每年等
"/" :表示增量:0/15(處于分鐘段里面) 每15分鐘,在0分以后開始,3/20 每20分鐘,從3分鐘以后開始
"?" :只能出現(xiàn)在日,星期段里面,表示不指定具體的值
"L" :只能出現(xiàn)在日,星期段里面,是Last的縮寫,一個月的最后一天,一個星期的最后一天(星期六)
"W" :表示工作日,距離給定值最近的工作日
"#" :表示一個月的第幾個星期幾,例如:"6#3"表示每個月的第三個星期五(1=SUN...6=FRI,7=SAT)
官方實例
| 0 0 12 * * ? | 每天中午12點觸發(fā) |
| 0 15 10 ? * * | 每天上午10:15觸發(fā) |
| 0 15 10 * * ? | 每天上午10:15觸發(fā) |
| 0 15 10 * * ? * | 每天上午10:15觸發(fā) |
| 0 15 10 * * ? 2005 | 2005年的每天上午10:15觸發(fā) |
| 0 * 14 * * ? | 在每天下午2點到下午2:59期間的每1分鐘觸發(fā) |
| 0 0/5 14 * * ? | 在每天下午2點到下午2:55期間的每5分鐘觸發(fā) |
| 0 0/5 14,18 * * ? | 在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發(fā) |
| 0 0-5 14 * * ? | 在每天下午2點到下午2:05期間的每1分鐘觸發(fā) |
| 0 10,44 14 ? 3 WED | 每年三月的星期三的下午2:10和2:44觸發(fā) |
| 0 15 10 ? * MON-FRI | 周一至周五的上午10:15觸發(fā) |
| 0 15 10 15 * ? | 每月15日上午10:15觸發(fā) |
| 0 15 10 L * ? | 每月最后一日的上午10:15觸發(fā) |
| 0 15 10 L-2 * ? | Fire at 10:15am on the 2nd-to-last last day of every month |
| 0 15 10 ? * 6L | 每月的最后一個星期五上午10:15觸發(fā) |
| 0 15 10 ? * 6L | Fire at 10:15am on the last Friday of every month |
| 0 15 10 ? * 6L 2002-2005 | 2002年至2005年的每月的最后一個星期五上午10:15觸發(fā) |
| 0 15 10 ? * 6#3 | 每月的第三個星期五上午10:15觸發(fā) |
| 0 0 12 1/5 * ? | Fire at 12pm (noon) every 5 days every month, starting on the first day of the month. |
| 0 11 11 11 11 ? | Fire every November 11th at 11:11am. |
源碼下載及可能需要了解的資料
源碼下載:http://download.csdn.net/download/jys1216/8882315
Topself介紹:http://www.cnblogs.com/jys509/p/4614975.html
Log4Net介紹:http://www.cnblogs.com/jys509/p/4569874.html
總結(jié)
以上是生活随笔為你收集整理的Quartz.NET的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于ASP.NET的comet简单实现
- 下一篇: 做餐饮具备哪些条件 有了这几个就足够了