Quartz.NET的简单任务管理类

昨天使用Quartz.NET做了个定时任务的功能,并实现了多个定时任务的功能

下面这个类实现了如下功能:

1、对定时任务进行管理

2、创建定时任务,需要给定时任务一个job的名称

3、判断给定的job的任务是否已存在

4、停止定时任务的功能 

namespace MyUKD.Quartz
{
    /// <summary>
    /// QuartzSchedule管理
    /// </summary>
    public class QuartzSchedulerMgr
    {
        private static ISchedulerFactory schedulerFactory;
        private static IScheduler scheduler;
 
        static QuartzSchedulerMgr()
        {
            schedulerFactory = new StdSchedulerFactory();
            scheduler = schedulerFactory.GetScheduler();
            scheduler.Start();
        }
 
        /// <summary>
        /// 获取Scheduler
        /// </summary>
        /// <returns></returns>
        public static IScheduler GetScheduler()
        {
            return scheduler;
        }
 
        /// <summary>
        /// 创建一个任务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jobName"></param>
        /// <param name="intervalInSeconds"></param>
        /// <param name="repeatCount"></param>
        /// <returns></returns>
        public static DateTimeOffset CreateJob<T>(string jobName, int intervalInSeconds, int repeatCount) where T : IJob
        {
 
            IJobDetail job = JobBuilder.Create<T>().WithIdentity(jobName, $"{jobName}-JobGroup").Build();
 
            ITrigger trigger = TriggerBuilder.Create().WithIdentity(jobName, $"{jobName}-TriggerGroup").StartNow()
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(intervalInSeconds).WithRepeatCount(repeatCount))
                .Build();
 
            return scheduler.ScheduleJob(job, trigger);
        }
 
        /// <summary>
        /// 创建一个任务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jobName"></param>
        /// <param name="intervalInSeconds"></param>
        /// <param name="repeatCount"></param>
        /// <param name="dataMap"></param>
        /// <returns></returns>
        public static DateTimeOffset CreateJob<T>(string jobName, int intervalInSeconds, int repeatCount,
            JobDataMap dataMap) where T : IJob
        {
 
            IJobDetail job = JobBuilder.Create<T>().WithIdentity(jobName, $"{jobName}-JobGroup").UsingJobData(dataMap)
                .Build();
 
            ITrigger trigger = TriggerBuilder.Create().WithIdentity(jobName, $"{jobName}-TriggerGroup").StartNow()
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(intervalInSeconds).WithRepeatCount(repeatCount))
                .Build();
 
            return scheduler.ScheduleJob(job, trigger);
        }
 
        /// <summary>
        /// 创建一个任务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jobName"></param>
        /// <param name="cronTime"></param>
        /// <param name="dataMap"></param>
        /// <returns></returns>
        public static DateTimeOffset CreateJob<T>(string jobName, string cronTime, JobDataMap dataMap) where T : IJob
        {
 
            IJobDetail job = JobBuilder.Create<T>().WithIdentity(jobName, $"{jobName}-JobGroup").UsingJobData(dataMap)
                .Build();
 
 
            ICronTrigger trigger = new CronTriggerImpl(jobName, $"{jobName}-TriggerGroup", cronTime);
 
 
            return scheduler.ScheduleJob(job, trigger);
        }
 
        /// <summary>
        /// 停止执行的job,并删除此job
        /// </summary>
        /// <param name="jobName"></param>
        /// <returns></returns>
        public static bool UnscheduleJob(string jobName)
        {
            JobKey existKey = JobKey.Create(jobName, $"{jobName}-JobGroup");
            TriggerKey exisTriggerKey = new TriggerKey(jobName, $"{jobName}-TriggerGroup");
 
            scheduler.PauseJob(existKey);
            return scheduler.UnscheduleJob(exisTriggerKey) && scheduler.DeleteJob(existKey);
 
        }
 
        /// <summary>
        /// 是否存在相关名称的Job
        /// </summary>
        /// <param name="jobName"></param>
        /// <returns></returns>
        public static bool ExistJob(string jobName)
        {
            JobKey key = JobKey.Create(jobName, $"{jobName}-JobGroup");
            return scheduler.CheckExists(key);
        }
 
    }
}

 

posted @ 2018-12-20 15:25  芝麻学问  阅读(237)  评论(0编辑  收藏  举报