• 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);
            }
     
        }
    }
    作者:芝麻科技
    出处:芝麻麻雀-Asp.Net学习之路
    技术:C++,C#
    向我打赏
    加我微信,聊一聊技术
  • 相关阅读:
    启用Netlogon debug,查看服务器验证瓶颈
    Windows Server 2016调整网卡顺序
    AD用户添加到组
    客户端查看/修改所属站点
    OpenCV相关库
    《塔木德》笔记
    《如何阅读一本书》笔记
    《创业维艰》笔记
    《社会心理学》笔记
    《智能商业》笔记
  • 原文地址:https://www.cnblogs.com/mzy-google/p/10149624.html
Copyright © 2020-2023  润新知