标签:2.0 for class offset handle 北京时间 strong eric values
经常出现场景:定时轮询数据库同步,定时邮件通知,定时处理数据等
其中常用的触发器有:
一、实现IJob,在Execute方法里编写要处理的业务逻辑,系统就会按照Quartz的配置,定时处理。
using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace QuarztDemo1 { public class TestJob:IJob { public void Execute(IJobExecutionContext context) { Console.WriteLine(DateTime.Now.ToString()); } } }
二、调度任务
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Quartz; using Quartz.Impl; namespace QuarztDemo1 { class Program { static void Main(string[] args) { //1.首先创建一个作业调度池 ISchedulerFactory schedf = new StdSchedulerFactory(); IScheduler sched = schedf.GetScheduler(); //2.创建出来一个具体的作业 IJobDetail job = JobBuilder.Create<TestJob>().Build(); //3.创建并配置一个触发器 #region(使用ISimpleTrigger触发器,每次3秒执行一次,无上限) //ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInSeconds(3).WithRepeatCount(int.MaxValue)).Build(); #endregion #region 每3秒执行一次 总共5次 ,开始执行时间设定在当前时间,结束时间我设定在2小时后,不过5次执行完没2小时候都不再执行。 //-------NextGivenSecondDate:如果第一个参数为null则表名当前时间往后推迟2秒的时间点。 //DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddSeconds(5), 2); //DateTimeOffset endTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddHours(2), 3); //ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create().StartAt(startTime).EndAt(endTime) // .WithSimpleSchedule(x => x.WithIntervalInSeconds(3).WithRepeatCount(5)) // .Build(); #endregion #region (使用CronTrigger 触发器)在每小时的第10,20,25,26,33,54分钟,每分钟的第1,10,14秒执行一次 DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddSeconds(1), 2); DateTimeOffset endTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddYears(2), 3); ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create().StartAt(startTime).EndAt(endTime) .WithCronSchedule("1,10,59 10,20,21,26,33,54 * * * ? ") .Build(); #endregion //4.加入作业调度池中 sched.ScheduleJob(job, trigger); //5.开始运行 sched.Start(); Console.ReadKey(); } } }
配置app.config或web.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"/> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/> </sectionGroup> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <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"/> <!--指向指定的jobs.xml -->
</quartz>
</configuration>
quartz_jobs.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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> <name>jobName1</name> <group>jobGroup1</group> <description>jobDesciption1</description> <job-type>QuarztDemo1.TestJob, QuarztDemo1</job-type> <durable>true</durable> <recover>false</recover> </job>
<trigger> <simple> <!--SimpleTrigger触发器--> <name>simpleName</name> <group>simpleGroup</group> <description>SimpleTriggerDescription</description> <job-name>jobName1</job-name> <job-group>jobGroup1</job-group> <start-time>1982-06-28T18:15:00.0Z</start-time> <end-time>2020-05-04T18:13:51.0Z</end-time> <repeat-count>10</repeat-count> <repeat-interval>3000</repeat-interval> </simple> </trigger> </schedule> </job-scheduling-data>
其中触发器也可以是Cron复杂任务触发器,代码如下
<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>
节点释义:
用于定义使用何种方式出发任务(job),同一个job可以定义多个trigger ,多个trigger 各自独立的执行调度,每个trigger 中必须且只能定义一种触发器类型(calendar-interval、simple、cron)
标签:2.0 for class offset handle 北京时间 strong eric values
原文地址:http://www.cnblogs.com/qiuguochao/p/7129044.html