这次由于项目的需求:什么定时发送邮件通知,定时筛选取消客户下单未支付的订单
重新捡起定时器,在网上翻来找去找到----Quartz.Net老字号了并不表示它就真的老了哦
github:https://github.com/quartznet/quartznet
当然介绍的园子里文章很多跟官方文档https://www.quartz-scheduler.net介绍的都挺清楚的!用起来很简便!
so!接下来我简单介绍一下Quartz.Net框架设计 下图借用园友的应该不会介意
园子中搜索发现已经有同学去实践了,这里引用一下各位大大的博文地址(应该不会介意,嘻嘻),大家也可以参考学习:
http://www.cnblogs.com/knowledgesea/p/5145239.html Quartz.net持久化与集群部署开发详解
http://www.cnblogs.com/mushroom/p/4231642.html#3760225 Net作业调度(四)—quartz.net持久化和集群
http://www.cnblogs.com/lanxiaoke/p/6629481.html 任务调度之持久化(基于Quartz.net)
http://www.cnblogs.com/lanxiaoke/p/6637714.html 任务调度之集群(基于Quartz.net)
http://www.cnblogs.com/huangxincheng/p/6916246.html 使用sqlserver搭建高可用双机热备的Quartz集群部署【附源码】
在此博主自己将Quartz.net的所需类目做了一次整合,大伙可以瞅瞅
1 using Quartz; 2 using Quartz.Impl; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 using Wshelper.Caching; 9 using Wshelper.Logging; 10 11 namespace Commons.Helpers 12 { 13 public class QuartzHelper 14 { 15 /// <summary> 16 /// 时间间隔执行任务 17 /// </summary> 18 /// <typeparam name="T">任务类,必须实现IJob接口</typeparam> 19 /// <param name="seconds">时间间隔(单位:毫秒)</param> 20 public static void ExecuteInterval<T>(int seconds) where T : IJob 21 { 22 ISchedulerFactory factory = new StdSchedulerFactory(); 23 IScheduler scheduler = factory.GetScheduler(); 24 25 //IJobDetail job = JobBuilder.Create<T>().WithIdentity("job1", "group1").Build(); 26 IJobDetail job = JobBuilder.Create<T>().Build(); 27 28 ITrigger trigger = TriggerBuilder.Create() 29 .StartNow() 30 .WithSimpleSchedule(x => x.WithIntervalInSeconds(seconds).RepeatForever()) 31 .Build(); 32 33 scheduler.ScheduleJob(job, trigger); 34 35 scheduler.Start(); 36 } 37 /// <summary> 38 /// 指定时间执行任务 39 /// </summary> 40 /// <typeparam name="T">任务类,必须实现IJob接口</typeparam> 41 /// <param name="cronExpression">cron表达式,即指定时间点的表达式</param> 42 public static void ExecuteByCron<T>(string cronExpression) where T : IJob 43 { 44 ISchedulerFactory factory = new StdSchedulerFactory(); 45 IScheduler scheduler = factory.GetScheduler(); 46 47 IJobDetail job = JobBuilder.Create<T>().Build(); 48 49 //DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddSeconds(1), 2); 50 //DateTimeOffset endTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddYears(2), 3); 51 52 ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create() 53 //.StartAt(startTime).EndAt(endTime) 54 .WithCronSchedule(cronExpression) 55 .Build(); 56 57 scheduler.ScheduleJob(job, trigger); 58 59 scheduler.Start(); 60 61 //Thread.Sleep(TimeSpan.FromDays(2)); 62 //scheduler.Shutdown(); 63 } 64 } 65 66 #region 任务执行例 67 public class MyJob : IJob 68 { 69 ILogger logger = LoggerFactory.CreateLog(); 70 public void Execute(IJobExecutionContext context) 71 { 72 //向c:\Quartz.txt写入当前时间并换行 73 //System.IO.File.AppendAllText(@"c:\Quartz.txt", DateTime.Now + Environment.NewLine); 74 ICache cache = new MemoryCacheFactory().Create(); 75 cache.Clear(); 76 logger.Info("时间:" + DateTime.Now.ToString() + "---------系统自动更新缓存!"); 77 } 78 } 79 #endregion 80 }
启动:
1 public static void QuartzHelp() 2 { 3 //#region Quartz调度任务 4 string cronExpression = "1 0 0 * * ? "; //这是指每天的0点执行任务 5 QuartzHelper.ExecuteByCron<MyJob>(cronExpression); //这是调用Cron计划方法 6 //#endregion 7 }
Quartz.Net 作者已经更新了支持.net standrad2.0了,就意味着可以在.net core平台使用了,下载地址:https://www.nuget.org/packages/Quartz/3.0.0-beta1,更新说明地址:https://www.quartz-scheduler.net/2017/10/08/quartznet-3.0-beta1-released.html,相信不久之后就会发布release版本,届时.net core框架开发就不怕没有定时器的支持了,~( ̄▽ ̄~)(~ ̄▽ ̄)~