转载

Quartz Scheduler

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);
        }

    }
原文  https://www.maiyewang.com/archives/93843
正文到此结束
Loading...