using Quartz;public static class SchedulerProgram { public static void Start(IScheduler scheduler = null) { try { Assembly asm = typeof(SchedulerProgr ...
using Quartz;
public static class SchedulerProgram { public static void Start(IScheduler scheduler = null) { try { Assembly asm = typeof(SchedulerProgram).GetTypeInfo().Assembly; Type[] types = asm.GetTypes(); IDictionary<int, Type> typeMap = new Dictionary<int, Type>(); List<Type> typeList = new List<Type>(); foreach (Type t in types) { if (new List<Type>(t.GetInterfaces()).Contains(typeof(IJobScheduler))) { typeList.Add(t); } } foreach (Type t in typeList) { IJobScheduler schedule = ObjectUtils.InstantiateType<IJobScheduler>(t); if (scheduler == null) { schedule.Run().ConfigureAwait(false); } else { schedule.Run(scheduler).ConfigureAwait(false); } } } catch (Exception ex) { throw ex; } } }
public class TestJobScheduler : IJobScheduler { public async Task Run(IScheduler scheduler) { Logger.Writelog("Call job scheduler"); //創建任務 IJobDetail job = JobBuilder.Create<TestJob>(). WithIdentity("EntityRelationshipTestJob", "CRMJobGroup"). UsingJobData("jobschedulename", "EntityRelationshipTestJob"). Build(); //創建觸發器 ITrigger trigger = TriggerBuilder.Create().WithIdentity("ContactTrigger", "TimeGroup").WithSimpleSchedule(t => t.WithIntervalInMinutes(30).WithRepeatCount(0)).Build();// test trigger //添加任務及觸發器至調度器中 await scheduler.ScheduleJob(job, trigger); //啟動 Logger.Writelog("Start job scheduler"); await scheduler.Start(); } public Task Run() { throw new NotImplementedException(); } }
public interface IJobScheduler { /// <summary> /// /// </summary> /// <returns></returns> Task Run(); /// <summary> /// 外部傳入scheduler /// </summary> /// <param name="scheduler"></param> /// <returns></returns> Task Run(IScheduler scheduler); }
public class TestJob : IJob { public Task Execute(IJobExecutionContext context) {//具體的方法 return Task.FromResult(0); } }