using System; using System.Threading.Tasks; using Quartz; using Quartz.Impl; namespace JXCMS.Core.TimingTask { public class QuartzTask { private ISchedulerFactory _schedulerFactory; private IScheduler _scheduler; private QuartzTask() { _schedulerFactory = new StdSchedulerFactory(); _scheduler = _schedulerFactory.GetScheduler().Result; _scheduler.Start(); } private static QuartzTask _instance; public static QuartzTask Instance() { if (_instance == null) { _instance = new QuartzTask(); } return _instance; } public async Task AddTask(Type jobType, string cron, string jobName, string jobGroup = "default") { var job = JobBuilder.Create(jobType).WithIdentity("job", "job").Build(); var trigger = TriggerBuilder.Create().WithIdentity(jobName + "tigger").StartNow().WithCronSchedule(cron).Build(); await _scheduler.ScheduleJob(job, trigger); return true; } } }