定期器–Quartz.Net

此番是因为品种的需求:什么定期发送邮件文告,定期筛选择消客户下单未支付的订单

再一次捡起定期器,在网上翻来找去找到—-Quartz.Net老字号了并不代表它就真正老了啊

github:https://github.com/quartznet/quartznet

自然介绍的田园里小说大多跟官方文书档案https://www.quartz-scheduler.net介绍的都挺清楚的!用起来很便捷!

so!接下去自个儿大致介绍一下Quartz.Net框架设计 
下图借用园友的应有不会介意

亚洲必赢bwin696.com 1

 

园子中找找发现早已有同学去试行了,那里引用一下各位大大的博文地址(应该不会介意,嘻嘻),大家也能够参见学习:
http://www.cnblogs.com/knowledgesea/p/5145239.html Quartz.net持久化与集群布署开垦详解
http://www.cnblogs.com/mushroom/p/4231642.html#3760225 Net作业调度(4)—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的所需类目做了贰遍整合,大伙能够瞅瞅**

亚洲必赢bwin696.com 2亚洲必赢bwin696.com 3

 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 }

View Code

 

启动:

亚洲必赢bwin696.com 4亚洲必赢bwin696.com 5

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         }

View Code

 

 

Quartz.Net 小编曾经更新了帮衬.net standrad二.0了,就代表能够在.net
core平台使用了,下载地址:https://www.nuget.org/packages/Quartz/3.0.0-beta1亚洲必赢bwin696.com,,更新表达地点:https://www.quartz-scheduler.net/2017/10/08/quartznet-3.0-beta1-released.html,相信不久未来就会公布release版本,届时.net
core框架开采就不怕没有按期器的支撑了,~( ̄▽ ̄~)(~ ̄▽ ̄)~

 

相关文章