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

30 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Builder;
using Quartz;
using Quartz.Impl;
namespace Bunny.Service.Job.JobService;
public static class SimpleJobService
{
public static void AddCreateSimpleJobService(this WebApplicationBuilder builder)
{
var schedulerFactory = new StdSchedulerFactory();
var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult();
var jobDetail = JobBuilder.Create<SimpleJob>()
.UsingJobData("username", "用户名")
.UsingJobData("password", "密码")
.WithIdentity("simpleJob", "简单的JOB")
.Build();
var trigger = TriggerBuilder.Create()
.WithIdentity("simpleTrigger", "测试发出器")
.StartNow()
// 每隔5秒执行一次一共重复10次
.WithSimpleSchedule(x => x.WithIntervalInSeconds(1).WithRepeatCount(10))
.Build();
// 使用同步方法
scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult();
scheduler.Start().GetAwaiter().GetResult();
}
}