任务调度Quartz---拓展使用
新建项目,使用管理NuGet程序程序包添加Quartz,核心代码如下:
#region 测试任务调度Quartz /// <summary> /// 测试任务调度Quartz /// </summary> public static async Task TestQuartz() { Console.WriteLine($"测试任务调度Quartz 开始。。。"); ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); IScheduler scheduler = await schedulerFactory.GetScheduler(); //上面2句代码可以使用下面一句代码 //IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); await scheduler.Start(); var job = JobBuilder.Create<Myjob>() .WithIdentity("11", "group11") .UsingJobData("123", "哈哈哈啊测试数据。。111111。")//UsingJobData可以存储一些信息 .Build(); var trigger = TriggerBuilder.Create() .WithIdentity("11", "group11") .StartNow() .WithSimpleSchedule(ss => ss.WithInterval(TimeSpan.FromSeconds(2)).RepeatForever()) .UsingJobData("123", "哈哈哈啊测试数据。。。")//UsingJobData可以存储一些信息,会覆盖job中UsingJobData相同键值的信息 .Build(); await scheduler.ScheduleJob(job, trigger); var job1 = JobBuilder.Create<Myjob2>() .WithIdentity("22", "group22") .UsingJobData("456", "哈哈哈啊测试数据。。111111。")//UsingJobData可以存储一些信息 .Build(); var trigger1 = TriggerBuilder.Create() .WithIdentity("22", "group22") .StartNow() .WithSimpleSchedule(ss => ss.WithInterval(TimeSpan.FromSeconds(3)).RepeatForever()) .UsingJobData("789", "哈哈哈啊测试数据。。。222")//UsingJobData可以存储一些信息,会覆盖job中UsingJobData相同键值的信息 .Build(); await scheduler.ScheduleJob(job1, trigger1); //拓展 //去掉提示下划线符号 //_= scheduler.GetJobKeys(Quartz.Impl.Matchers.GroupMatcher<JobKey>.GroupContains("group11")); //获取所有的分组,根据分组获取所有JobKey,JobKey是Job和Trigger的管理管理对象,可以根据JobKey获取到Job和Trigger的相关信息 var AllJobKeys = await scheduler.GetJobKeys(Quartz.Impl.Matchers.GroupMatcher<JobKey>.AnyGroup()); //获取指定分组 var JobKeys = await scheduler.GetJobKeys(Quartz.Impl.Matchers.GroupMatcher<JobKey>.GroupContains("group11")); var firstjobkey = JobKeys.FirstOrDefault(); var jobDetail = await scheduler.GetJobDetail(firstjobkey); var jobDetailTriggers = await scheduler.GetTriggersOfJob(firstjobkey); var jobDetailTrigger = jobDetailTriggers.FirstOrDefault(); //Console.WriteLine($"删除触发器{firstjobkey}。。。"); //删除触发器和定时任务 //_ = scheduler.DeleteJob(firstjobkey); //重新发布 // _ = scheduler.RescheduleJob(jobDetailTrigger.Key, jobDetailTrigger); //await scheduler.Start(); } public class Myjob : IJob { public async Task Execute(IJobExecutionContext context) { //根据IJobExecutionContext获取到Job和Trigger的相关信息, //同时IJobExecutionContext的MergedJobDataMap可以获取JobDetail和Trigger中的UsingJobData存储的数据信息, //Trigger中的UsingJobData会覆盖job中UsingJobData相同键值的信息 Console.WriteLine($"data:{context.JobDetail.Key.Name},{context.JobDetail.Key.Group},{context.MergedJobDataMap.GetString("123")}"); Console.WriteLine($"data:{context.Trigger.Key.Name},{context.Trigger.Key.Group},{context.MergedJobDataMap.GetString("123")}"); var a = context.JobDetail.JobDataMap; await Console.Out.WriteLineAsync($"{DateTime.Now}--Myjob 执行了。。。"); } } public class Myjob2 : IJob { public async Task Execute(IJobExecutionContext context) { //根据IJobExecutionContext获取到Job和Trigger的相关信息 Console.WriteLine($"data:{context.JobDetail.Key.Name},{context.JobDetail.Key.Group},{context.MergedJobDataMap.GetString("123")}"); var a = context.JobDetail.JobDataMap; await Console.Out.WriteLineAsync($"{DateTime.Now}--Myjob2 执行了。。。"); } } #endregion