定时器--Quartz.Net

时间:2022-11-18 01:07:15

这次由于项目的需求:什么定时发送邮件通知,定时筛选取消客户下单未支付的订单

重新捡起定时器,在网上翻来找去找到----Quartz.Net老字号了并不表示它就真的老了哦

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

当然介绍的园子里文章很多跟官方文档https://www.quartz-scheduler.net介绍的都挺清楚的!用起来很简便!

so!接下来我简单介绍一下Quartz.Net框架设计  下图借用园友的应该不会介意

定时器--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的所需类目做了一次整合,大伙可以瞅瞅

 using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wshelper.Caching;
using Wshelper.Logging; namespace Commons.Helpers
{
public class QuartzHelper
{
/// <summary>
/// 时间间隔执行任务
/// </summary>
/// <typeparam name="T">任务类,必须实现IJob接口</typeparam>
/// <param name="seconds">时间间隔(单位:毫秒)</param>
public static void ExecuteInterval<T>(int seconds) where T : IJob
{
ISchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = factory.GetScheduler(); //IJobDetail job = JobBuilder.Create<T>().WithIdentity("job1", "group1").Build();
IJobDetail job = JobBuilder.Create<T>().Build(); ITrigger trigger = TriggerBuilder.Create()
.StartNow()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(seconds).RepeatForever())
.Build(); scheduler.ScheduleJob(job, trigger); scheduler.Start();
}
/// <summary>
/// 指定时间执行任务
/// </summary>
/// <typeparam name="T">任务类,必须实现IJob接口</typeparam>
/// <param name="cronExpression">cron表达式,即指定时间点的表达式</param>
public static void ExecuteByCron<T>(string cronExpression) where T : IJob
{
ISchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = factory.GetScheduler(); IJobDetail job = JobBuilder.Create<T>().Build(); //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(cronExpression)
.Build(); scheduler.ScheduleJob(job, trigger); scheduler.Start(); //Thread.Sleep(TimeSpan.FromDays(2));
//scheduler.Shutdown();
}
} #region 任务执行例
public class MyJob : IJob
{
ILogger logger = LoggerFactory.CreateLog();
public void Execute(IJobExecutionContext context)
{
//向c:\Quartz.txt写入当前时间并换行
//System.IO.File.AppendAllText(@"c:\Quartz.txt", DateTime.Now + Environment.NewLine);
ICache cache = new MemoryCacheFactory().Create();
cache.Clear();
logger.Info("时间:" + DateTime.Now.ToString() + "---------系统自动更新缓存!");
}
}
#endregion
}

启动:

 public static void QuartzHelp()
{
//#region Quartz调度任务
string cronExpression = "1 0 0 * * ? ";  //这是指每天的0点执行任务
QuartzHelper.ExecuteByCron<MyJob>(cronExpression);  //这是调用Cron计划方法
//#endregion
}

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框架开发就不怕没有定时器的支持了,~( ̄▽ ̄~)(~ ̄▽ ̄)~