【Quartz】常用方法的使用方式(三)

综合编程 2018-05-16 阅读原文

前言




总结了一下quartz组件的一些常用方法,以备将来不时之需。哈哈,看着挺简单有些好是值得笔记一下的。好记性不如烂笔头吗?

代码部分:

方法类内容:


 public class ExamplesGo
    {
        static ISchedulerFactory schedFact = new StdSchedulerFactory();
        public static IScheduler sched = schedFact.GetScheduler();

    }

View Code

任务触发器分离实现:


    #region 1.运行任务【所有】
        /// 
        /// 运行任务
        /// 
        public void Run()
        {
            if (sched != null)
                sched.Start();
        }
        #endregion

        #region 2.添加job
        /// 
        /// 添加job
        /// 
        public void AddJob()
        {
            IJobDetail job = JobBuilder.Create()
                            .WithIdentity("HelloJob", "group1")
                            .StoreDurably()
                            .Build();
            sched.AddJob(job, true);
        }
        #endregion

        #region 3.添加触发器
        /// 
        /// 添加触发器
        /// 
        /// 需要添加触发器的job
        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
        /// 
        /// 根据key返回Job
        /// 
        /// 需要查找的job名称
        /// 
        public IJobDetail GetJob(string jobName)
        {

            JobKey jobKey = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList().Where(b => b.Name == jobName).FirstOrDefault();
            return sched.GetJobDetail(jobKey);
        }
        #endregion

View Code

基本的暂停,恢复,删除:


   #region 5.暂停任务[全部暂停]
        /// 
        /// 暂停任务
        /// 
        public void PauseAll()
        {
            if (sched != null)
                sched.PauseAll();
        }
        #endregion

        #region 6.暂停任务【单个任务】
        /// 
        /// 暂停任务
        /// 
        /// 需要暂停任务的名称
        public void Shutdown(string jobName)
        {
            if (sched != null)
            {
                JobKey jobKey = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList().Where(b => b.Name == jobName).FirstOrDefault();
                sched.PauseJob(jobKey);
            }

        }
        #endregion

        #region 7.恢复当前任务
        /// 
        /// 恢复当前任务
        /// 
        /// 需要恢复任务的名称
        public void ResumeJob(string jobName)
        {
            if (sched != null)
            {
                JobKey jobKey = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList().Where(b => b.Name == jobName).FirstOrDefault();
                sched.ResumeJob(jobKey);
            }

        }
        #endregion

        #region 8.恢复所有任务
        /// 
        /// 恢复所有任务
        /// 
        public void ResumeJobAll()
        {
            if (sched != null)
            {
                sched.ResumeAll();
            }

        }
        #endregion

        #region 9.删除当前任务
        /// 
        /// 删除当前任务
        /// 
        /// 删除任务名称
        public void DeleteJob(string jobName)
        {
            if (sched != null)
            {
                JobKey jobKey = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList().Where(b => b.Name == jobName).FirstOrDefault();
                sched.DeleteJob(jobKey);
            }
        }
        #endregion

        #region 10.删除所有任务
        /// 
        /// 删除所有任务
        /// 
        public void DeleteJobAll()
        {
            if (sched != null)
            {
                sched.Clear();
            }
        }
        #endregion

View Code

最后一些复杂点的查询:


 #region 11.返回所有任务键
        /// 
        /// 返回所有任务键
        /// 
        /// 
        public List GetJobAll()
        {
            //获取所有job
            List jobKeys = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList();
            return jobKeys;
        }
        #endregion

        #region 12.返回所有的触发器
        /// 
        /// 返回所有的触发器
        /// 
        /// 
        public List GetTrigAll()
        {
            List trigKeys = sched.GetTriggerKeys(GroupMatcher.AnyGroup()).ToList();
            return trigKeys;
        }
        #endregion

        #region 13.返回job对应的trigger
        /// 
        /// 返回job对应的trigger
        /// 
        /// 任务名称
        /// 
        public List GetJobToTrig(string jobName)
        {
            JobKey jobKey = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList().Where(b => b.Name == jobName).FirstOrDefault();
            List triggerList = sched.GetTriggersOfJob(jobKey).ToList();
            return triggerList;
        }
        #endregion

View Code

最后添加的两个一个关闭组件和修改触发器:


 #region 14.关闭所有任务
        /// 
        /// 关闭所有任务
        /// 
        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

View Code
博客园精华区

责编内容by:博客园精华区阅读原文】。感谢您的支持!

您可能感兴趣的

elastic-job详解(二):作业的调度 JobScheduler是elastic-job作业调度的关键类,也是起始类,在包com.dangdang.ddframe.job.lite.api下。调度任务的执行需要包含两大步骤:任务的配置和任务的注册。JobScheduler的...
elastic-job详解(三):Job的手动触发功能... elastic-job的任务都是使用quartz来触发的,quartz表达式一般都是定期执行。但有时候一些周期较长的任务,比如一天一次,几小时一次的任务,我们需要等待很久才能触发一次。如果我们需要测试一些功能,或者说重跑一些任务的话,...
baeldung – Coding and Testing Stuff: Introdu... If you’re working with Spring, check out "REST With Spring":>> ...
Open Quartz While stumbling around, I found Open Quartz , which is to Quake as FreeDooM is to DooM, or for those who don’t know ...
spring quartz 串行配置 参考 http://blog.csdn.net/lkforce/article/details/51841890