【Quartz】常用方法的使用方式(三)
前言
总结了一下quartz组件的一些常用方法,以备将来不时之需。哈哈,看着挺简单有些好是值得笔记一下的。好记性不如烂笔头吗?
代码部分:
方法类内容:
public class ExamplesGo { static ISchedulerFactory schedFact = new StdSchedulerFactory(); public static IScheduler sched = schedFact.GetScheduler(); }
任务触发器分离实现:
#region 1.运行任务【所有】 /// <summary> /// 运行任务 /// </summary> public void Run() { if (sched != null) sched.Start(); } #endregion #region 2.添加job /// <summary> /// 添加job /// </summary> public void AddJob() { IJobDetail job = JobBuilder.Create<HelloJob>() .WithIdentity("HelloJob", "group1") .StoreDurably() .Build(); sched.AddJob(job, true); } #endregion #region 3.添加触发器 /// <summary> /// 添加触发器 /// </summary> /// <param name="job">需要添加触发器的job</param> public void AddTrigger(IJobDetail job) { ITrigger trigger = TriggerBuilder.Create() .WithIdentity("myTrigger", "group1") .WithCronSchedule("0/5 * * * * ?") //5秒执行一次 .ForJob(job) .Build(); sched.ScheduleJob(trigger); } public void AddTrigger1(IJobDetail job) { ITrigger trigger = TriggerBuilder.Create() .WithIdentity("myTrigger2", "group1") .WithCronSchedule("0/1 * * * * ?") //5秒执行一次 .ForJob(job) .Build(); sched.ScheduleJob(trigger); } #endregion #region 4.根据key返回Job /// <summary> /// 根据key返回Job /// </summary> /// <param name="jobName">需要查找的job名称</param> /// <returns></returns> public IJobDetail GetJob(string jobName) { JobKey jobKey = sched.GetJobKeys(GroupMatcher<JobKey>.AnyGroup()).ToList().Where(b => b.Name == jobName).FirstOrDefault(); return sched.GetJobDetail(jobKey); } #endregion
基本的暂停,恢复,删除:
#region 5.暂停任务[全部暂停] /// <summary> /// 暂停任务 /// </summary> public void PauseAll() { if (sched != null) sched.PauseAll(); } #endregion #region 6.暂停任务【单个任务】 /// <summary> /// 暂停任务 /// </summary> /// <param name="jobName">需要暂停任务的名称</param> public void Shutdown(string jobName) { if (sched != null) { JobKey jobKey = sched.GetJobKeys(GroupMatcher<JobKey>.AnyGroup()).ToList().Where(b => b.Name == jobName).FirstOrDefault(); sched.PauseJob(jobKey); } } #endregion #region 7.恢复当前任务 /// <summary> /// 恢复当前任务 /// </summary> /// <param name="jobName">需要恢复任务的名称</param> public void ResumeJob(string jobName) { if (sched != null) { JobKey jobKey = sched.GetJobKeys(GroupMatcher<JobKey>.AnyGroup()).ToList().Where(b => b.Name == jobName).FirstOrDefault(); sched.ResumeJob(jobKey); } } #endregion #region 8.恢复所有任务 /// <summary> /// 恢复所有任务 /// </summary> public void ResumeJobAll() { if (sched != null) { sched.ResumeAll(); } } #endregion #region 9.删除当前任务 /// <summary> /// 删除当前任务 /// </summary> /// <param name="jobName">删除任务名称</param> public void DeleteJob(string jobName) { if (sched != null) { JobKey jobKey = sched.GetJobKeys(GroupMatcher<JobKey>.AnyGroup()).ToList().Where(b => b.Name == jobName).FirstOrDefault(); sched.DeleteJob(jobKey); } } #endregion #region 10.删除所有任务 /// <summary> /// 删除所有任务 /// </summary> public void DeleteJobAll() { if (sched != null) { sched.Clear(); } } #endregion
最后一些复杂点的查询:
#region 11.返回所有任务键 /// <summary> /// 返回所有任务键 /// </summary> /// <returns></returns> public List<JobKey> GetJobAll() { //获取所有job List<JobKey> jobKeys = sched.GetJobKeys(GroupMatcher<JobKey>.AnyGroup()).ToList(); return jobKeys; } #endregion #region 12.返回所有的触发器 /// <summary> /// 返回所有的触发器 /// </summary> /// <returns></returns> public List<TriggerKey> GetTrigAll() { List<TriggerKey> trigKeys = sched.GetTriggerKeys(GroupMatcher<TriggerKey>.AnyGroup()).ToList(); return trigKeys; } #endregion #region 13.返回job对应的trigger /// <summary> /// 返回job对应的trigger /// </summary> /// <param name="jobName">任务名称</param> /// <returns></returns> public List<ITrigger> GetJobToTrig(string jobName) { JobKey jobKey = sched.GetJobKeys(GroupMatcher<JobKey>.AnyGroup()).ToList().Where(b => b.Name == jobName).FirstOrDefault(); List<ITrigger> triggerList = sched.GetTriggersOfJob(jobKey).ToList(); return triggerList; } #endregion
最后添加的两个一个关闭组件和修改触发器:
#region 14.关闭所有任务 /// <summary> /// 关闭所有任务 /// </summary> public void Shutdown() { if (sched != null) sched.Shutdown(); } #endregion #region 15.修改任务触发器 public void ModiyTrig() { ITrigger trigger = TriggerBuilder.Create() .WithIdentity("myTrigger", "group1") // .StartAt(runTime) .WithCronSchedule("0/10 * * * * ?") //10秒执行一次 .Build(); sched.RescheduleJob(trigger.Key, trigger); sched.Start(); } #endregion
作者:YanBigFeg —— 颜秉锋
出处:http://www.cnblogs.com/yanbigfeg
本文版权归作者和博客园共有,欢迎转载,转载请标明出处。如果您觉得本篇博文对您有所收获,觉得小弟还算用心,请点击右下角的 [推荐],谢谢!