public class TestController : ControllerBase
{
static IScheduler _scheduler = null;
public TestController()
{
ISchedulerFactory factory = new StdSchedulerFactory();
_scheduler = factory.GetScheduler().Result;
}
[HttpGet]
public async Task Quartz_Start()
{
var schedulerFactory = new StdSchedulerFactory();
var scheduler = await schedulerFactory.GetScheduler();
await scheduler.Start();
Console.WriteLine($"任务调度器已启动");
//创建作业和触发器
var jobDetail = JobBuilder.Create<FirstJob>()
.WithIdentity("FirstJob")
.Build();
var trigger = TriggerBuilder.Create()
.WithSimpleSchedule(m =>
{
m.WithIntervalInSeconds(1).RepeatForever();
})
.Build();
//添加调度
await scheduler.ScheduleJob(jobDetail, trigger);
}
/// <summary>
/// 暂停一个job
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task Quartz_Pause()
{
//暂停使用
var jobKey = new JobKey("FirstJob");
if (await _scheduler.CheckExists(jobKey))
{
await _scheduler.PauseJob(jobKey);
}
}
/// <summary>
/// 恢复一个job
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task Quartz_Resume()
{
//恢复使用
var jobKey = new JobKey("FirstJob");
if (await _scheduler.CheckExists(jobKey))
{
await _scheduler.ResumeJob(jobKey);
}
}
}