新建解決方案和工程Quartz.net
使用Power Shell 命令 Install-Package Quartz 導入Quartz.net程序集
新建一個計划TestJob
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Quartz.net { public class TestJob : IJob { public void Execute(IJobExecutionContext context) { try { Console.WriteLine("執行計划..."); } catch (Exception) { throw; } } } }
從程序中控制計划時間的用法
using Quartz.Impl; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Quartz.net { class Program { static void Main(string[] args) { // 如果配置了Log4Net 可取消注釋 //Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter { Level = Common.Logging.LogLevel.Info }; // 從工廠里獲取調度實例 IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); scheduler.Start(); // 創建一個任務 IJobDetail job = JobBuilder.Create<TestJob>() .WithIdentity("MyJob", "group1") .Build(); // 創建一個觸發器 ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(10) // 每10秒執行一次 .RepeatForever()) // 無限次執行 .Build(); // 每天執行的觸發器 ITrigger t = TriggerBuilder.Create() .WithIdentity("myTrigger", "group2") .ForJob(job) .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(9, 30)) // 每天9:30執行一次 //.ModifiedByCalendar("myHolidays") // but not on holidays 設置那一天不知道 .Build(); // 每天執行的將觸發器換成天的就可以了 scheduler.ScheduleJob(job, trigger); } } }
從配置文件設置定時計划的用法,先看App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </configSections> <quartz> <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler" /> <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" /> <add key="quartz.threadPool.threadCount" value="10" /> <add key="quartz.threadPool.threadPriority" value="2" /> <add key="quartz.jobStore.misfireThreshold" value="60000" /> <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" /> <!--******************************Plugin配置********************************************* --> <add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" /> <add key="quartz.plugin.xml.fileNames" value="~/quartz_jobs.xml"/> </quartz> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
再看quartz_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> <!--任務--> <job> <!--任務名稱,同一個group中多個job的name不能相同--> <name>TestJob</name> <!--任務分組--> <group>sampleGroup</group> <!--任務描述--> <description>Sample job for Quartz Server</description> <!--任務類型,任務的具體類型及所屬程序集--> <job-type>Quartz.NETConfig.TestJob, Quartz.NETConfig</job-type> <durable>true</durable> <recover>false</recover> </job> <!--任務觸發器--> <trigger> <!--簡單任務的觸發器,可以調度用於重復執行的任務--> <simple> <!--觸發器名稱,同一個分組中的名稱必須不同--> <name>sampleSimpleTrigger</name> <!--觸發器組--> <group>sampleSimpleGroup</group> <!--描述--> <description>Simple trigger to simply fire sample job</description> <!--要調度的任務名稱,該job-name必須和對應job節點中的name完全相同--> <job-name>TestJob</job-name> <!--調度任務(job)所屬分組,該值必須和job中的group完全相同--> <job-group>sampleGroup</job-group> <!--任務開始時間--> <start-time>2012-04-01T08:00:00+08:00</start-time> <misfire-instruction>SmartPolicy</misfire-instruction> <!--任務執行次數 -1 為無限次執行--> <repeat-count>-1</repeat-count> <!--任務觸發間隔(毫秒)--> <repeat-interval>10000</repeat-interval> <!--每3秒中執行一次--> </simple> </trigger> <job> <!--任務名稱,同一個group中多個job的name不能相同--> <name>StatisticsJob</name> <!--任務分組--> <group>sampleGroup1</group> <!--任務描述--> <description>Sample job for Quartz Server</description> <!--任務類型,任務的具體類型及所屬程序集--> <job-type>Quartz.NETConfig.StatisticsJob, Quartz.NETConfig</job-type> <durable>true</durable> <recover>false</recover> </job> <trigger> <cron> <name>TriggerJob001</name> <group>ProjectJobs</group> <job-name>StatisticsJob</job-name> <job-group>sampleGroup1</job-group> <!--秒 分 時 日(星期) 月 年--> <cron-expression>0/3 * * * * ?</cron-expression> </cron> </trigger> <!--每天0點定時執行 0 0 0/1 * * ? --> <job> <name>DefaultJob</name> <group>sampleGroup2</group> <description>Sample job for Quartz Server</description> <job-type>Quartz.NETConfig.DefaultJob, Quartz.NETConfig</job-type> <durable>true</durable> <recover>false</recover> </job> <trigger> <cron> <name>TriggerJob002</name> <group>ProjectJobs</group> <job-name>DefaultJob</job-name> <job-group>sampleGroup2</job-group> <cron-expression>0 0 0/1 * * ?</cron-expression> </cron> </trigger> </schedule> </job-scheduling-data>
用配置調比較簡單
// 從工廠里獲取調度實例
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
配置的任務也沒什么
public class DefaultJob : IJob
{
public void Execute(IJobExecutionContext context)
{
try
{
// 開始統計數據
Console.WriteLine("開始統計數據...");
}
catch (Exception)
{
throw;
}
}
}
public class StatisticsJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("表達式執行調度任務...");
}
}
public class TestJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("執行調度任務");
}
}
以上就是定時計划的用法,可參考文檔 http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/more-about-triggers.html