using OASystem.API.OAMethodLib.Quartz.Jobs; using Quartz; using Quartz.Spi; namespace QuzrtzJob.Factory { public class QuartzFactory { //1、声明一个调度工厂 private ISchedulerFactory _schedulerFactory; private IScheduler _scheduler = default; private IJobFactory _IOCjobFactory; public QuartzFactory(ISchedulerFactory schedulerFactory, IJobFactory jobFactory) { _schedulerFactory = schedulerFactory; _IOCjobFactory = jobFactory; } public async Task Start() { //2、通过调度工厂获得调度器 _scheduler = await _schedulerFactory.GetScheduler(); //这里是指定容器仓库 _scheduler.JobFactory = _IOCjobFactory; //3、开启调度器 await _scheduler.Start(); //4、创建一个触发器 var trigger = TriggerBuilder.Create() //.WithSimpleSchedule(x => x.WithIntervalInSeconds(2).RepeatForever())//每两秒执行一次 .WithCronSchedule("0 15 9 * * ?") .Build(); var taskTrigger = TriggerBuilder.Create() //.WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever())//每六十秒执行一次 .WithCronSchedule("0 0 9,18 * * ?") //每天早上九点和下午六点触发 .Build(); var taskMsgTrigger = TriggerBuilder.Create() //.WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever())//每六十秒执行一次 .WithCronSchedule("0 0 9,18 * * ?") //每天早上九点和下午六点触发 .Build(); var teamCurrencyTrigger = TriggerBuilder.Create() //.WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever())//每六十秒执行一次 .WithCronSchedule("0 0 0 * * ? ") //每天零点零分点触发 .Build(); //5、创建任务 var jobDetail = JobBuilder.Create().WithIdentity("job1", "group").Build(); var taskJobDetail = JobBuilder.Create().WithIdentity("job2", "group").Build(); var taskMsgJobDetail = JobBuilder.Create().WithIdentity("job3", "group").Build(); var teamCurrencyJobDetail = JobBuilder.Create().WithIdentity("job4", "group").Build(); //6、将触发器和任务器绑定到调度器中 await _scheduler.ScheduleJob(jobDetail, trigger); await _scheduler.ScheduleJob(taskJobDetail, taskTrigger); await _scheduler.ScheduleJob(taskMsgJobDetail, taskMsgTrigger); await _scheduler.ScheduleJob(teamCurrencyJobDetail, teamCurrencyTrigger); return await Task.FromResult("将触发器和任务器绑定到调度器中完成"); } } }