using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp10 { [DisallowConcurrentExecution()] public abstract class JobBase : IJob { #region IJob 成员 public Task Execute(IJobExecutionContext context) { try { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(DateTime.Now.ToString() + "{0}这个Job开始执行", context.JobDetail.Key.Name); ExcuteJob(context); return Task.CompletedTask; ; } catch (Exception ex) { //LoggerFactory.CreateLog().Logger_Debug(this.GetType().Name + "error:" + ex.Message); throw; } } #endregion /// <summary> /// 执行计划,子类可以重写 /// </summary> public virtual string Cron => "0/1 * * * * ?"; /// <summary> /// Job具体类去实现自己的逻辑 /// </summary> protected abstract void ExcuteJob(IJobExecutionContext context); } }
using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; using Quartz; namespace ConsoleApp10 { class Job : JobBase { protected override void ExcuteJob(IJobExecutionContext context) { Console.WriteLine("执行"+DateTime.Now.ToString()); } } }
using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp10 { public class QuartzManager { public static Action<Type> JoinToQuartz = (type) => { StdSchedulerFactory.GetDefaultScheduler().Result.Start(); var obj = Activator.CreateInstance(type); string cron = type.GetProperty("Cron").GetValue(obj).ToString(); var jobDetail = JobBuilder.Create(type) .WithIdentity(type.Name) .Build(); var jobTrigger = TriggerBuilder.Create() .WithIdentity(type.Name + "Trigger") .StartNow() .WithCronSchedule(cron) .Build(); StdSchedulerFactory.GetDefaultScheduler().Result.ScheduleJob(jobDetail, jobTrigger); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"新添加了一个服务{nameof(type)},通过心跳Job自动被加载!"); }; } }
using System; namespace ConsoleApp10 { class Program { static void Main(string[] args) { QuartzManager.JoinToQuartz(typeof(Job)); Console.ReadLine(); } } }
https://www.cnblogs.com/mushroom/p/4067037.html
http://cron.qqe2.com/