一、参考源文链接
1、https://www.likecs.com/show-897836.html
2、https://blog.csdn.net/weixin_43614067/article/details/115373776
二、Quartz基本使用
public class Job1 : IJob { public Task Execute(IJobExecutionContext context) { Console.WriteLine("开始定时任务1"); return Task.CompletedTask; } } public class Job2 : IJob { public Task Execute(IJobExecutionContext context) { Console.WriteLine("开始定时任务2"); return Task.CompletedTask; } }
/// <summary> /// 定时任务执行实例1 /// </summary> public static void Test1() { var _scheduler = new StdSchedulerFactory().GetScheduler().Result; var trigger1 = TriggerBuilder.Create() .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever())//每10秒执行一次 .Build(); var trigger2 = TriggerBuilder.Create() .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())//每10秒执行一次 .Build(); var jobDetail = JobBuilder.Create<Job1>() .WithIdentity("Myjob1", "group")//我们给这个作业取了个“Myjob”的名字,并取了个组名为“group” .Build(); var jobDetai2 = JobBuilder.Create<Job2>() .WithIdentity("Myjob2", "group")//我们给这个作业取了个“Myjob”的名字,并取了个组名为“group” .Build(); _scheduler.ScheduleJob(jobDetail, trigger1).Wait(); _scheduler.ScheduleJob(jobDetai2, trigger2).Wait(); _scheduler.Start().Wait(); }
二、暂停和恢复某个任务
public class StartJob : IJob { public Task Execute(IJobExecutionContext context) { for (int i = 0; i < 3; i++) { try { Thread.Sleep(1000); } catch (Exception e) { Console.WriteLine(e.StackTrace); } Console.WriteLine(("hello~~~~~~~~~~~~~~~~~~" + i + DateTime.Now.ToString())); } return Task.CompletedTask; } }
public class PauseJob : IJob { public Task Execute(IJobExecutionContext context) { ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler sched = null; try { sched = sf.GetScheduler().Result; Console.WriteLine("对象sf:" + sf); Console.WriteLine("对象sched:" + sched); } catch (SchedulerException e) { Console.WriteLine(e.StackTrace); } CommonUtil.PauseJob(sched, "startJob", "group1"); return Task.CompletedTask; } }
public class ResumeJob : IJob { public Task Execute(IJobExecutionContext context) { ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler sched = null; try { sched = sf.GetScheduler().Result; Console.WriteLine("对象sf:" + sf); Console.WriteLine("对象sched:" + sched); } catch (SchedulerException e) { Console.WriteLine(e.StackTrace); } CommonUtil.ResumeJob(sched, "startJob", "group1"); return Task.CompletedTask; } }
public class CommonUtil { /// <summary> /// 暂停一个job,暂停后,任务若未执行完,会执行完毕为止。 /// </summary> /// <param name="scheduler"></param> /// <param name="jobName"></param> /// <param name="jobGroupName"></param> public static void PauseJob(IScheduler scheduler, string jobName, string jobGroupName) { try { scheduler.PauseJob(new JobKey(jobName, jobGroupName)); Console.WriteLine(string.Format("定时任务%s暂停成功!", jobName)); } catch (SchedulerException e) { Console.WriteLine((string.Format("定时任务%s暂停异常!", jobName))); } } /// <summary> /// 恢复一个job 恢复定时器时立刻先执行一次定时器的任务 然后再按计划执行 /// </summary> /// <param name="scheduler"></param> /// <param name="jobName"></param> /// <param name="jobGroupName"></param> public static void ResumeJob(IScheduler scheduler, String jobName, String jobGroupName) { try { scheduler.ResumeJob(new JobKey(jobName, jobGroupName)); Console.WriteLine((string.Format("定时任务%s恢复成功!", jobName))); } catch (SchedulerException e) { Console.WriteLine((string.Format("定时任务%s恢复异常!", jobName))); } } }
public static void Test() { ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler sched = sf.GetScheduler().Result; //创建相关的job信息 IJobDetail startJob = JobBuilder.Create<StartJob>() .WithIdentity("startJob", "group1") .Build(); IJobDetail pauseJob = JobBuilder.Create<PauseJob>() .WithIdentity("pauseJob", "group2") .Build(); IJobDetail resumeJob = JobBuilder.Create<ResumeJob>() .WithIdentity("resumeJob", "group3") .Build(); //创建触发器的名称 //每2s执行startJob任务 ITrigger startTrigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .WithSchedule(CronScheduleBuilder.CronSchedule("0/2 * * * * ?")).Build(); //每10s执行暂停任务,去暂停startJob任务 ITrigger pauseTrigger = TriggerBuilder.Create() .WithIdentity("trigger2", "group2") .WithSchedule(CronScheduleBuilder.CronSchedule("0/10 * * * * ?")).Build(); //每15s执行恢复任务,去恢复startJob任务 ITrigger resumeTrigger = TriggerBuilder.Create() .WithIdentity("trigger3", "group3") .WithSchedule(CronScheduleBuilder.CronSchedule("0/15 * * * * ?")).Build(); sched.ScheduleJob(startJob, startTrigger); sched.ScheduleJob(pauseJob, pauseTrigger); sched.ScheduleJob(resumeJob, resumeTrigger); sched.Start(); }
标签:Quartz,Console,netCore,Create,Task,实例,WriteLine,sched,public From: https://www.cnblogs.com/mojiejushi/p/16985492.html