zoukankan      html  css  js  c++  java
  • 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#
    向我打赏
    加我微信,聊一聊技术
  • 相关阅读:
    《Linux内核设计与实现》读书笔记(十一) 定时器和时间管理
    《Linux内核设计与实现》读书笔记(十) 内核同步方法
    《Linux内核设计与实现》读书笔记(八) 中断下半部的处理
    《Linux内核设计与实现》读书笔记(七) 中断处理
    《Linux内核设计与实现》读书笔记(九) 内核同步介绍
    SQL Server数据库中使用sql脚本删除指定表的列
    如何删除在Excel中存在的无效的链接呢
    SQL Server游标使用快速上手
    SQL Server中查询某列所在的数据库中所在的表
    WinAPI: SetTimer、KillTimer 创建与移除高性能定时器
  • 原文地址:https://www.cnblogs.com/mzy-google/p/10149624.html
Copyright © 2011-2022 走看看