CSharp-Single-EFCore/Bunny.Service/IService/Service/JobService.cs

40 lines
1.4 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 Bunny.Service.Job;
using Quartz;
using Quartz.Impl;
namespace Bunny.Service.IService.Service;
public class JobService : IJobService
{
/// <summary>
/// 开启一个简单的工作
/// https://www.youtube.com/watch?v=CuHIScZKup8&list=PLSi1BNmQ61ZohCcl43UdAksg7X3_TSmly&index=6
/// </summary>
public void StartSimpleJob()
{
var schedulerFactory = new StdSchedulerFactory();
// TODO 如果使用异步必须使用await 和 async
// var scheduler = schedulerFactory.GetScheduler();
var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult();
var jobDetail = JobBuilder.Create<SimJob>()
.UsingJobData("username", "用户名")
.UsingJobData("password", "密码")
.WithIdentity("simpleJob", "简单的JOB")
.Build();
var trigger = TriggerBuilder.Create()
.WithIdentity("testTrigger", "测试发出器")
.StartNow()
// 每隔5秒执行一次一共重复10次
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).WithRepeatCount(10))
.Build();
// 使用同步方法
scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult();
scheduler.Start().GetAwaiter().GetResult();
// TODO 使用异步
// await _scheduler.ScheduleJob(jobDetail, trigger);
// await _scheduler.Start();
}
}