JXMovies/Core/JXCMS.Core/TimingTask/QuartzTask.cs

40 lines
1.1 KiB
C#
Raw Normal View History

2020-02-10 22:30:27 +08:00
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<bool> 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;
}
}
}