Quartz.Net 删除Job
来博客园的第一篇文章先写个简单的,希望能帮助到大家。
步入正题:
Quartz.Net有三个重要的概念,分别是 Scheduler 、Job 、Trigger。
Scheduler包含Job和Trigger。
如果要删除一个正在运行的Job,需要在Schedule中将其移除。
调用的是IScheduler 中的DeleteJob 方法
上代码:
public class JobController { private static JobController _jobController = new JobController(); private JobController() { } public static JobController Instance() { return _jobController; } public void Start() { LogProvider.SetCurrentLogProvider(new ConsoleLogProvider()); RunProgramRunExample().GetAwaiter().GetResult(); Console.WriteLine("Press any key to close the application"); Console.ReadKey(); } private static async Task RunProgramRunExample() { try { NameValueCollection props = new NameValueCollection { { "quartz.serializer.type", "binary" } }; StdSchedulerFactory factory = new StdSchedulerFactory(props); IScheduler scheduler = await factory.GetScheduler(); await scheduler.Start(); for (int i = 0; i < 10; i++) { IJobDetail job = JobBuilder.Create<AutoJob>() .WithIdentity("计算作业" + i.ToString(), "组1") .UsingJobData("jobSays", $"Hello {i}!") .UsingJobData("myFloatValue", i) .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity($"trigger{i}", "group1") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(10) .RepeatForever()) .Build(); // Tell quartz to schedule the job using our trigger await scheduler.ScheduleJob(job, trigger); } JobKey jk = new JobKey("计算作业1", "组1"); await scheduler.DeleteJob(jk);//移除一个job await Task.Delay(-1); await scheduler.Shutdown(); } catch (SchedulerException se) { Console.WriteLine(se); } } }
写在最后:
第一次写没什么经验,欢迎大家批评指教!