34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
|
using Microsoft.AspNetCore.Builder;
|
|||
|
using Quartz;
|
|||
|
using Quartz.Impl;
|
|||
|
|
|||
|
namespace Bunny.Service.Job.JobService;
|
|||
|
|
|||
|
public static class CronJobService
|
|||
|
{
|
|||
|
public static void AddCronJobService(this WebApplicationBuilder builder)
|
|||
|
{
|
|||
|
var stdSchedulerFactory = new StdSchedulerFactory();
|
|||
|
var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
|||
|
|
|||
|
var jobDetail = JobBuilder.Create<SimpleJob>()
|
|||
|
.UsingJobData("username", "用户名")
|
|||
|
.UsingJobData("password", "密码")
|
|||
|
.StoreDurably() // 设置作业为持久的
|
|||
|
.WithIdentity("simpleJob", "简单的JOB")
|
|||
|
.Build();
|
|||
|
|
|||
|
scheduler.AddJob(jobDetail, true).GetAwaiter().GetResult();
|
|||
|
|
|||
|
var trigger = TriggerBuilder.Create()
|
|||
|
.ForJob(jobDetail)
|
|||
|
.UsingJobData("trigger", "trigger值")
|
|||
|
.WithIdentity("testTrigger", "测试发出器")
|
|||
|
.StartNow()
|
|||
|
.WithCronSchedule("0/1 * * * * ?")
|
|||
|
.Build();
|
|||
|
|
|||
|
scheduler.ScheduleJob(trigger).GetAwaiter().GetResult();
|
|||
|
scheduler.Start().GetAwaiter().GetResult();
|
|||
|
}
|
|||
|
}
|