Quartz目录
1.Nuget下载引用 Quartz
2.调度管理中心
方法策略,时间策略,框架日志,
3.具体执行策略中心
4.额外监控策略
5.方法策略,时间策略改为配置文件和IOC容器结合
6.可视化界面管理
7.程序集成到WindowsService最终应用
2.调度管理中心
方法策略,
时间策略,
框架日志执行输出,
3.具体执行策略中心
using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace DispatcherProject.QuartzNet.CustomJob { [PersistJobDataAfterExecution] //锚点1:设置这个后 可以在执行后可以保留执行结果 [DisallowConcurrentExecution] //设置这个后 让定时执行的任务变成串行,必须等待前面任务执行完成才会继续执行下一个任务,就像设置每5秒执行一次任务,但是前面任务没执行完成,后面任务又开始了就会存在问题 public class SendMessage : IJob { //private static object obj = new object(); //定义一个静态变量也可以实现 执行后可以保留执行结果 public SendMessage() { Console.WriteLine("SendMessage 被构造"); } public async Task Execute(IJobExecutionContext context) //context 很强大 他会包含我们想要的切 { await Task.Run(() => { Thread.Sleep(5000); #region sendJob //发消息:给谁发,需要传递参数; Console.WriteLine(); Console.WriteLine("**********************************************"); JobDataMap jobDetailMap = context.JobDetail.JobDataMap; Console.WriteLine($"{jobDetailMap.Get("字符串1")}{DateTime.Now}"); Console.WriteLine($"{jobDetailMap.Get("字符串2")}{DateTime.Now}"); Console.WriteLine($"{jobDetailMap.Get("字符串3")}{DateTime.Now}"); Console.WriteLine($"{jobDetailMap.Get("字符串4")}{DateTime.Now}"); Console.WriteLine($"{jobDetailMap.Get("Year")}"); jobDetailMap.Put("Year", jobDetailMap.GetInt("Year") + 1); JobDataMap triggerMap = context.Trigger.JobDataMap; Console.WriteLine(); #endregion #region trigger Console.WriteLine($"{triggerMap.Get("时间策略的字符串1")}{DateTime.Now}"); Console.WriteLine($"{triggerMap.Get("时间策略的字符串2")}{DateTime.Now}"); Console.WriteLine($"{triggerMap.Get("时间策略的字符串3")}{DateTime.Now}"); Console.WriteLine($"{triggerMap.Get("时间策略的字符串4")}{DateTime.Now}"); //锚点1 获取执行的结果 var getYear = triggerMap.Get("Year"); Console.WriteLine($"{triggerMap.Get("Year")}"); Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); Console.WriteLine(context.MergedJobDataMap.Get("Year")); Console.WriteLine("**********************************************"); Console.WriteLine(); #endregion }); } } }
4.额外监控策略
1.自定义作业监听器 监控工作任务
using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace DispatcherProject.QuartzNet.CustomListener { ////// 自定义作业监听器,需要实行接口IJobListener /// public class CustomJobListener : IJobListener { public string Name => "CustomJobListener"; public async Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default) { await Task.Run(() => { //便于我们自己添加自己的业务逻辑 Console.WriteLine($"{DateTime.Now} this is JobExecutionVetoed"); }); } public async Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default) { await Task.Run(() => { Console.WriteLine($"{DateTime.Now} this is JobToBeExecuted"); }); } public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default) { await Task.Run(() => { Console.WriteLine($"{DateTime.Now} this is JobWasExecuted"); }); } } }
2.自定义调度程序监听器 监控调度程序
using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace DispatcherProject.QuartzNet.CustomListener { ////// 自定义调度程序侦听器 /// public class CustomSchedulerListener : ISchedulerListener { public async Task JobAdded(IJobDetail jobDetail, CancellationToken cancellationToken = default) { await Task.Run(() => { Console.WriteLine($"{jobDetail.Description} 被加入到Scheduler"); }); } public async Task JobDeleted(JobKey jobKey, CancellationToken cancellationToken = default) { await Task.Run(() => { Console.WriteLine($"{jobKey} 被删除 "); }); } public Task JobInterrupted(JobKey jobKey, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task JobPaused(JobKey jobKey, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task JobResumed(JobKey jobKey, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public async Task JobScheduled(ITrigger trigger, CancellationToken cancellationToken = default) { await Task.Run(() => { Console.WriteLine("this is JobScheduled"); }); } public Task JobsPaused(string jobGroup, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task JobsResumed(string jobGroup, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task JobUnscheduled(TriggerKey triggerKey, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task SchedulerError(string msg, SchedulerException cause, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task SchedulerInStandbyMode(CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task SchedulerShutdown(CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task SchedulerShuttingdown(CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public async Task SchedulerStarted(CancellationToken cancellationToken = default) { await Task.Run(() => { Console.WriteLine($"this is SchedulerStarted"); }); } public async Task SchedulerStarting(CancellationToken cancellationToken = default) { await Task.Run(() => { Console.WriteLine($"this is SchedulerStarting"); }); } public Task SchedulingDataCleared(CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task TriggerFinalized(ITrigger trigger, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task TriggerPaused(TriggerKey triggerKey, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task TriggerResumed(TriggerKey triggerKey, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task TriggersPaused(string triggerGroup, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } public Task TriggersResumed(string triggerGroup, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } } }
3.自定义触发监听器 监控时间策略
using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace DispatcherProject.QuartzNet.CustomListener { ////// 自定义触发监听器 /// public class CustomTriggerListener : ITriggerListener { public string Name => "CustomTriggerListener"; public async Task TriggerComplete(ITrigger trigger, IJobExecutionContext context, SchedulerInstruction triggerInstructionCode, CancellationToken cancellationToken = default) { await Task.Run(() => { Console.WriteLine("this is TriggerComplete"); }); } public async Task TriggerFired(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default) { await Task.Run(() => { Console.WriteLine("this is TriggerFired"); }); } public async Task TriggerMisfired(ITrigger trigger, CancellationToken cancellationToken = default) { await Task.Run(() => { Console.WriteLine("this is TriggerComplete"); }); } public async Task<bool> VetoJobExecution(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default) { await Task.Run(() => { Console.WriteLine("VetoJobExecution"); }); return false; //返回false才能继续执行 } } }
5.IOC容器结合
7.获取框架日志信息