十年河东,十年河西,莫欺少年穷
学无止境,精益求精
先来点干货,每五秒执行一次
1、程序入口
class Program { static void Main(string[] args) { Task.Run(() => { JobManager.RunSche().GetAwaiter().GetResult(); }); Console.ReadKey(); } }
2、触发器注册
using Quartz; using Quartz.Impl; using Quartz.Impl.Matchers; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace CabinetElectricExe { public class JobManager { private static IScheduler scheduler; public static async Task RunSche() { try { Console.WriteLine("ServiceStart"); StdSchedulerFactory factory = new StdSchedulerFactory(); scheduler = await factory.GetScheduler(); await JobSchedule<JobCenter>.ScheduleDayJob(scheduler); await scheduler.Start(); } catch (Exception ex) { Console.WriteLine("jobinitException"); // Log.Inst.Info($"jobinitException:{ex.ToString()}"); } } public static async Task<string> Monitor() { var groups = await scheduler.GetJobGroupNames(); var jobstring = new StringBuilder(); foreach (var item in groups) { var jobkeys = await scheduler.GetJobKeys(GroupMatcher<JobKey>.GroupEquals(item)); foreach (var jobkey in jobkeys) { string jobName = jobkey.Name; string jobGroup = jobkey.Group; var job = await scheduler.GetJobDetail(jobkey); var triggers = await scheduler.GetTriggersOfJob(jobkey); foreach (ITrigger trigger in triggers) { dynamic trg = trigger; TriggerKey triggerKey = new TriggerKey(trg.Name, trg.Group); TriggerState x = scheduler.GetTriggerState(triggerKey).Result; var lastTime = trigger.GetPreviousFireTimeUtc().GetValueOrDefault().ToString("yyyy-MM-dd HH:mm:ss"); var nextTime = trigger.GetNextFireTimeUtc().GetValueOrDefault().ToString("yyyy-MM-dd HH:mm:ss"); jobstring.AppendFormat($@"状态:{ x.ToString()},上次执行时间:{lastTime},下次执行时间:{nextTime},job:{jobName},group:{jobGroup};"); } } } return jobstring.ToString(); } } }
3、触发器注册,执行函数
using Quartz; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using static Quartz.MisfireInstruction; namespace CabinetElectricExe { public class JobCenter : IJob { public JobCenter() { } /// <summary> /// 调用入口 /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task Execute(IJobExecutionContext context) { //HttpHelper.Post($"{CommonConfig.PMPath}job/excute", ""); Console.WriteLine("执行了一次"); } } public class JobSchedule<T> where T : IJob { /// <summary> /// 注册一个执行服务【定义他的频率,执行时间等,参考】 /// </summary> /// <param name="scheduler"></param> /// <returns></returns> public static async Task ScheduleDayJob(IScheduler scheduler) { var jobName = "CabinetElectricExe"; IJobDetail job = JobBuilder.Create<T>() .WithIdentity($"{jobName}Job", $"{jobName}Group") .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity($"{jobName}Trigger", $"{jobName}Group") .StartAt(DateTime.Now.AddSeconds(5)) //启动间隔 //.StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(5) //等待间隔 .RepeatForever()) .Build(); var st = await scheduler.ScheduleJob(job, trigger); } } }
最后,官方示例下载链接:https://files.cnblogs.com/files/chenwolong/Quartz.NET-2.3.2.zip 【CSDN花了60积分搞下来的,分享出去了,喜欢就点个赞】
@天才卧龙的伯乐