/// <summary> /// 启用Job /// </summary> /// <param name="builder"></param> /// <param name="configuration"></param> /// <param name="schedulerFactory"></param> static public async void EnableJob(this IApplicationBuilder builder, IConfiguration configuration, ISchedulerFactory schedulerFactory) { var option = configuration.GetSection("SunnyOptions:JobOption").Get <JobOption[]>(); if (option != null && option.Length > 0) { //1、通过调度工厂获得调度器 var scheduler = await schedulerFactory.GetScheduler(); //2、开启调度器 await scheduler.Start(); var types = DiHelper.GetCustomizeTypes(t => typeof(IJobEntity).IsAssignableFrom(t) && !t.GetTypeInfo().IsAbstract); foreach (var item in option) { var tArr = types.Where(x => x.Name == item.JobClassName); foreach (var t in tArr) { //3、创建一个触发器 var trigger = TriggerBuilder.Create() .WithCronSchedule(item.RunAtCron) .Build(); var wrapperType = typeof(JobWrapper <>).MakeGenericType(t.GetTypeInfo()); //4、创建任务 var jobDetail = JobBuilder.Create(wrapperType) .WithIdentity(item.JobClassName, item.JobGroup) .SetJobData(item.Args == null ? new JobDataMap() : new JobDataMap(item.Args)) .Build(); //5、将触发器和任务器绑定到调度器中 await scheduler.ScheduleJob(jobDetail, trigger); } } } }