CSharp-Single-EFCore/Bunny.Service/Job/JobService/CronJobService.cs

36 lines
1.2 KiB
C#
Raw Normal View History

2024-08-15 22:55:32 +08:00
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<CronJob>()
2024-08-15 22:55:32 +08:00
.UsingJobData("username", "用户名")
.UsingJobData("password", "密码")
.UsingJobData("count", 1)
2024-08-15 22:55:32 +08:00
.StoreDurably() // 设置作业为持久的
.WithIdentity("simpleJob", "简单的JOB")
.Build();
scheduler.AddJob(jobDetail, true).GetAwaiter().GetResult();
var trigger = TriggerBuilder.Create()
.ForJob(jobDetail)
.UsingJobData("trigger", "trigger值")
.UsingJobData("triggerCount", 1)
.WithIdentity("cronTrigger", "测试发出器")
2024-08-15 22:55:32 +08:00
.StartNow()
.WithCronSchedule("0/1 * * * * ?")
2024-08-15 22:55:32 +08:00
.Build();
scheduler.ScheduleJob(trigger).GetAwaiter().GetResult();
scheduler.Start().GetAwaiter().GetResult();
}
}