quartz.net 类库封装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了quartz.net 类库封装相关的知识,希望对你有一定的参考价值。

1、前言

   最近项目需要做一写任务作业调度的工作,最终选择了quartz.net这个插件,它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而 创建简单的或复杂的调度。它有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等等.对于quartz.net在这就不进行过的介绍 了,下面针对这个插件的封装具体如下。

2、定义对象接口

  对象分为对位接口(IQJob)和内部操作接口(IMJob).除了对象本身,接口还包括对对象的一些简单操作,比如Remove,pause,Resume等,看代码有详细注释。这样的目的是为了让对象更便与操作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Quartz
{
    /// <summary>
    /// quartz.net接口对象
    /// 作者:王延领
    /// 时间:2016/5/5
    /// </summary>
    public interface IQJob
    {
        /// <summary>
        /// 系统代码
        /// </summary>
        string SysCode { get; set; }
        /// <summary>
        /// 任务id
        /// </summary>
        string JobId { get; set; }
        /// <summary>
        /// 任务名称
        /// </summary>
        string Name { get; set; }
        /// <summary>
        /// 任务分组
        /// </summary>
        string Group { get; set; }
        /// <summary>
        /// 间隔时间
        /// </summary>
        int Seconds { get; set; }
        /// <summary>
        /// 做多执行次数,如果是<1,则无限循环
        /// </summary>
        int MaxTimes { get; set; }
        /// <summary>
        /// 开始执行时间
        /// </summary>
        DateTime StartTime { get; set; }
        /// <summary>
        /// 任务处理器
        /// </summary>
        Action Handler { get; set; }
        /// <summary>
        /// 任务处理器
        /// </summary>
        Action<IQJob> DetailHandler { get; set; }
        /// <summary>
        /// 当前执行的第几次
        /// </summary>
        int Times { get; set; }
        /// <summary>
        /// 接口执行时间
        /// </summary>
        DateTime LastTime { get; set; }
        /// <summary>
        /// 任务的当前状态
        /// </summary>
        JobState State { get; set; }
        /// <summary>
        /// 本次任务执行的动作
        /// </summary>
        JobAction Action { get; set; }
        /// <summary>
        /// 开始执行任务
        /// </summary>
        void Start();
        /// <summary>
        /// 开始执行任务
        /// </summary>
        /// <param name="starttime">任务开始时间</param>
        /// <param name="internaltimes">间隔时间(s)</param>
        /// <param name="maxtimes">执行次数</param>
        void Start(DateTime starttime, int internaltimes = 60*60, int maxtimes = 0);
        /// <summary>
        /// 任务触发动作
        /// 无需参数
        /// </summary>
        /// <param name="action">触发的动作</param>
        /// <returns></returns>
        IQJob Handle(Action handler);
        /// <summary>
        /// 任务触发动作
        /// </summary>
        /// <param name="action">触发的动作</param>
        /// <returns></returns>
        IQJob Handle(Action<IQJob> handler);
        string Key();
        bool Load();
        bool Save();
        /// <summary>
        /// 下次运行时间
        /// </summary>
        DateTime NextTime();
        /// <summary>
        /// 获取job文件地址
        /// </summary>
        /// <returns></returns>
        string Path();
        /// <summary>
        /// 移除
        /// </summary>
        /// <returns></returns>
        bool Remove();
        /// <summary>
        /// 挂起
        /// </summary>
        void Pause();
        /// <summary>
        ///继续执行
        /// </summary>
        void Resume();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Quartz
{
    public interface IMyJob : IQJob
    {
       
        void Excute();

        void QRemove();

        void QPause();

        void QResume();
    }
}


  

 

3、quartz.net具体实现封装接口

  quartz.net对的封装主要包括:

    1.任务的基本操作(创建,删除,暂停,继续,状态查询,数量查询等)

    2.任务执行触发动作的回调,其中回调用有两种委托雷响Action,Action<IQjob>

    3.持久化的处理,持久化文件保存到xml文件中(一个任务一个xml文件)

 using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quartz
{
    /// <summary>
    /// quartz.net任务调度接口
    /// 作者:王延领
    /// 日期:2016/5/5
    /// </summary>
    public interface IJobFactory
    {
        /// <summary>
        /// 任务触发的动作
        /// </summary>
        /// <param name="syscode">系统编码</param>
        /// <param name="hander">需要参数的触发动作</param>
        void Trigger(string syscode, Action<IQJob> hander);
        /// <summary>
        /// 任务触发的动作
        /// </summary>
        /// <param name="syscode">系统编码</param>
        /// <param name="hander">无需参数的触发动作</param>
        void Trigger(string syscode, Action hander);
        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="job"> IQJob</param>
        /// <returns></returns>
        string Build(IQJob job);
        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="jobid">IMyJob.Key()</param>
        /// <returns></returns>
        bool Remove(string jobkey);
        /// <summary>
        /// 暂停任务
        /// </summary>
        /// <param name="jobkey">IMyJob.Key()</param>
        /// <returns></returns>
        bool Pause(string jobkey);
        /// <summary>
        /// 继续任务
        /// </summary>
        /// <param name="jobkey">IMyJob.Key()</param>
        /// <returns></returns>
        bool Resume(string jobkey);
        /// <summary>
        /// 任务是否存在
        /// </summary>
        /// <param name="jobkey">IMyJob.Key()</param>
        /// <returns></returns>
        bool Exists(string jobkey);
        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="systcode">系统编码</param>
        void RemoveAll(string systcode);
        /// <summary>
        /// 暂停任务
        /// </summary>
        /// <param name="syscode">系统编码</param>
        void PauseAll(string syscode);
        /// <summary>
        /// 继续任务
        /// </summary>
        /// <param name="syscode">系统编码</param>
        void ResumeAll(string syscode);
        /// <summary>
        /// 任务数
        /// </summary>
        /// <param name="syscode">系统编码</param>
        /// <param name="state">任务状态</param>
        /// <returns></returns>
        int JobCount(string syscode, JobState state);
        /// <summary>
        /// 任务数
        /// </summary>
        /// <returns></returns>
        int JobCount();
        /// <summary>
        /// 任务状态
        /// </summary>
        /// <param name="jobkey">IMyJob.Key()</param>
        /// <returns></returns>
        JobState State(string jobkey);
        /// <summary>
        /// 获取任务
        /// </summary>
        /// <param name="jobkey">IMyJob.Key()</param>
        /// <returns></returns>
        IMyJob FindByKey(string jobkey);
        /// <summary>
        /// 任务初始化
        /// </summary>
        void Initialize();
    }
}

4、对象接口的实现 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Quartz
{
    public class MyJob : QJob, IMyJob
    {
        public MyJob() { }
        public MyJob(IQJob qjob)
        {
            this.Action = qjob.Action;
            this.SysCode = qjob.SysCode;
            this.JobId = qjob.JobId;
            this.Group = qjob.Group;
            this.Name = qjob.Name;
            this.LastTime = qjob.LastTime;
            this.MaxTimes = qjob.MaxTimes;
            this.Seconds = qjob.Seconds;
            this.State = qjob.State;
            this.Times = qjob.Times;
            this.StartTime = qjob.StartTime;
            this.DetailHandler = qjob.DetailHandler;
            this.Handler = qjob.Handler;
        }
        /// <summary>
        ///任务执行时触发动作
        /// </summary>
        public void Excute()
        {
            try
            {
                Times++;
                LastTime = DateTime.Now;
                Action = JobAction.Excute;
                if (MaxTimes == 1)
                {
                    XMLProcess.Delete(Path());
                    MaxTimes = 0;
                    Trigger();
                    return;
                }
                if (MaxTimes != 0)
                    MaxTimes--;
                Save();
                Trigger();
            }
            catch (Exception ex)
            { }
        }
        /// <summary>
        ///任务暂停时触发动作
        /// </summary>
        public void QPause()
        {
            Action = JobAction.Pend;
            State = JobState.Pending;
            Save();
            Trigger();
        }
        /// <summary>
        /// 任务继续时触发动作
        /// </summary>
        public void QResume()
        {
            Action = JobAction.Resume;
            State = JobState.Working;
            Save();
            Trigger();
        }
        /// <summary>
        /// 任务移除触发动作
        /// </summary>
        public void QRemove()
        {
            XMLProcess.Delete(Path());
            Action = JobAction.Delete;
            Trigger();
        }   /// <summary>
        /// <summary>
        /// 动作触发
        /// </summary>
        /// <param name="myjob">JobDetail</param>
        void Trigger()
        {
            if (Handler != null) { Handler(); return; }
            if (DetailHandler != null) { DetailHandler(this); return; }
            //获取订阅委托列表
            var sh = JobVariables.GetHandlers.SingleOrDefault(h => h.systme_code == SysCode);
            if (sh.detailexcute != null)
            {
                sh.detailexcute(this);
                return;
            }
            if (sh.excute != null)
                sh.excute();
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quartz
{
    /// <summary>
    /// quartz.net对象
    /// 作者:王延领
    /// 时间:2016/5/5
    /// </summary>
    public class QJob : IQJob
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public QJob() { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="syscode">系统编码</param>
        /// <param name="id">任务id【需系统内唯一】</param>
        /// <param name="name">任务名称</param>
        /// <param name="group">任务群组</param>
        public QJob(string syscode, string id, string name = "", string group = "")
        {
            JobId = id;
            SysCode = syscode;
            Name = name;
            Group = group;
            Seconds = 60 * 60;
            MaxTimes = 0;
            StartTime = DateTime.Now.AddMinutes(1);
            Handler = null;
            DetailHandler = null;
        }
        public string SysCode { get; set; }
        public string JobId { get; set; }
        public string Name { get; set; }
        public string Group { get; set; }
        public int Seconds { get; set; }
        public int MaxTimes { get; set; }
        public DateTime StartTime { get; set; }
        public int Times { get; set; }
        public JobState State { get; set; }
        public JobAction Action { get; set; }
        public DateTime LastTime { get; set; }
        [System.Xml.Serialization.XmlIgnore]
        public Action Handler { get; set; }
        [System.Xml.Serialization.XmlIgnore]
        public Action<IQJob> DetailHandler { get; set; }
        /// 持久化保存
        /// </summary>
        public bool Save()
        {
            try
            {
                string filepath = JobFactory.Instance.GetPath();
                if (!File.Exists(Path())) return false;
                IQJob myjob = new QJob()
                {
                    SysCode = this.SysCode,
                    JobId = this.JobId,
                    Group = this.Group,
                    Name = this.Name,
                    LastTime = this.LastTime,
                    Handler = this.Handler,
                    MaxTimes = this.MaxTimes,
                    Seconds = this.Seconds,
                    State = this.State,
                    Times = this.Times,
                    StartTime = this.StartTime,
                    DetailHandler = this.DetailHandler,
                    Action = this.Action
                };
                string xml = XMLProcess.Serializer(typeof(QJob), myjob);
                XMLProcess.Write(xml, Path());
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool Load()
        {
            try
            {
                var job = XMLProcess.Deserialize<QJob>(XMLProcess.ReadXml(Path()));
                JobId = job.JobId;
                SysCode = job.SysCode;
                Name = job.Name;
                Group = job.Group;
                Seconds = job.Seconds;
                MaxTimes = job.MaxTimes;
                StartTime = job.StartTime;
                Times = job.Times;
                State = job.State;
                Action = job.Action;
                LastTime = job.LastTime;
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 任务的jobkey规则
        /// </summary>
        /// <returns></returns>
        public string Key()
        {
            return SysCode + "_" + JobId;
        }

        /// <summary>
        /// 开始执行任务
        /// </summary>
        public void Start()
        {
            JobFactory.Instance.Build(this);
        }
        /// <summary>
        /// 开始执行任务
        /// </summary>
        /// <param name="starttime">开始执行时间</param>
        /// <param name="internaltimes">时间间隔(s)</param>
        /// <param name="maxtimes">执行次数</param>
        public void Start(DateTime starttime, int internaltimes = 60*60, int maxtimes = 0)
        {
            StartTime = starttime;
            Seconds = internaltimes;
            MaxTimes = maxtimes;
            JobFactory.Instance.Build(this);
        }
        /// <summary>
        /// 下次执行时间
        /// </summary>
        /// <returns></returns>
        public DateTime NextTime()
        {
            return LastTime.AddSeconds(Seconds);
        }
        /// <summary>
        ///任务触发动作
        /// </summary>
        /// <param name="handler">需要任务信息的动作</param>
        /// <returns>IMyJob</returns>
        public IQJob Handle(Action handler)
        {
            Handler = handler;
            return this;
        }
        /// <summary>
        /// 任务触发动作
        /// </summary>
        /// <param name="handler">需要任务信息的动作</param>
        /// <returns>IMyJob</returns>
        public IQJob Handle(Action<IQJob> handler)
        {
            DetailHandler = handler;
            return this;
        }
        /// <summary>
        /// 持久化地址
        /// </summary>
        /// <returns>【例:../job/syscode_name_group_jobid.xml】</returns>
        public string Path()
        {
            return System.IO.Path.Combine(JobFactory.Instance.GetPath(), string.Format("{0}_{1}_{2}_{3}.xml", SysCode, Group, Name, JobId));
        }
        /// <summary>
        /// 移除任务
        /// </summary>
        /// <returns></returns>
        public bool Remove()
        {
            return JobFactory.Instance.Remove(Key());
        }
        /// <summary>
        /// 暂停任务
        /// </summary>
        public void Pause()
        {
            JobFactory.Instance.Pause(Key());
        }
        /// <summary>
        /// 继续执行任务
        /// </summary>
        public void Resume()
        {
            JobFactory.Instance.Resume(Key());
        }
    }
}


 

5、quartz.net接口实现

using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quartz
{
    /// <summary>
    /// quartz.net接任务调度口实现
    /// 作者:王延领
    /// 时间:2016/5/5
    /// </summary>
    public class JobFactory : IJobFactory
    {
        /// <summary>
        /// 单例模式
        /// </summary>
        private static JobFactory _Instance = new JobFactory();
        public static JobFactory Instance
        {
            get
            {
                return _Instance;
            }
        }
        public JobFactory()
        {
            ssf = new StdSchedulerFactory();
            _scheduler = ssf.GetScheduler();
        }
        ISchedulerFactory ssf;
        IScheduler _scheduler;
        /// <summary>
        /// 任务持久化文件保存地址
        /// 注:默认保存在@"..\..\jobs\"文件夹下
        /// 直接地址结尾加"\"
        /// </summary>
        private string _path { get; set; }
        public void SetPath(string path)
        {
            _path = path;
        }
        public string GetPath()
        {
            if (string.IsNullOrEmpty(_path))
                _path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), @"..\..\jobs\");
            return _path;
        }
        //<summary>
        //创建任务
        //</summary>
        //<param name="myjob">任务对象</param>
        public string Build(IQJob qjob)
        {
            IMyJob myjob = new MyJob(qjob);
            if (JobVariables.jobs.Exists(j => j.JobId == myjob.JobId && j.SysCode == myjob.SysCode)) return "任务与存在!!!";
            JobAdd(myjob);
            IJobDetail jobdetail = Create_Jobdetail(myjob);
            ISimpleTrigger trigger = Create_Trigger(myjob);
            _scheduler.ScheduleJob(jobdetail, trigger);
            if (_scheduler.IsShutdown || _scheduler.InStandbyMode)
                _scheduler.Start();
            StandSave(qjob);
            return qjob.Key();
        }
        /// <summary>
        /// 创建jobdetail
        /// </summary>
        /// <param name="qjob">
        /// 默认执行Create_Execute
        /// </param>
        /// <returns></returns>
        protected IJobDetail Create_Jobdetail(IMyJob qjob)
        {
            IJobDetail jobdetail = JobBuilder.Create<Create_Job>()
                .WithIdentity(qjob.JobId, qjob.SysCode)
                .Build();
            return jobdetail;
        }
        /// <summary>
        /// 创建job触发器
        /// </summary>
        /// <param name="qjob"></param>
        /// <returns></returns>
        protected ISimpleTrigger Create_Trigger(IMyJob qjob)
        {
            ISimpleTrigger trigger;
            trigger = (ISimpleTrigger)TriggerBuilder.Create().WithIdentity(qjob.JobId, qjob.SysCode)
                                                 .StartAt(qjob.StartTime).WithSimpleSchedule(x => x.WithIntervalInSeconds(qjob.Seconds)
                                                  .WithRepeatCount(qjob.MaxTimes - 1))
                                                  .Build();
            return trigger;
        }
        /// <summary>
        /// 创建任务执行
        /// </summary>
        public class Create_Job : IJob
        {
            public void Execute(Quartz.IJobExecutionContext context)
            {
                IMyJob myjob = JobFactory.Instance.Find(context.JobDetail.Key);
                if (myjob.State != JobState.Working) return;
                JobFactory.Instance.JobRemove(myjob);
                myjob.Excute();
                JobFactory.Instance.JobAdd(myjob);
            }
        }
        /// <summary>
        /// 从任务列表中删除指定对象
        /// </summary>
        /// <param name="myjob"></param>
        /// <returns></returns>
        bool JobRemove(IMyJob myjob)
        {
            return JobVariables.jobs.Remove(myjob);
        }
        /// <summary>
        /// 向任务列表中添加指定对象
        /// </summary>
        /// <param name="myjob"></param>
        void JobAdd(IMyJob myjob)
        {
            JobVariables.jobs.Insert(0, myjob);
        }
        /// <summary>
        /// 获取MyJob
        /// </summary>
        /// <param name="jobkey">JobDetail.JobKey</param>
        /// <returns></returns>
        IMyJob Find(JobKey jobkey)
        {
            return JobVariables.jobs.SingleOrDefault(j => j.JobId == jobkey.Name && j.SysCode == jobkey.Group);
        }
        /// <summary>
        /// 获取任务
        /// </summary>
        /// <param name="jobkey">IMyJob.Key()</param>
        /// <returns></returns>
        public IMyJob FindByKey(string jobkey)
        {
            var job = JobVariables.jobs.SingleOrDefault(j => j.Key() == jobkey);
            return job;
        }
        /// <summary>
        /// 初始化任务
        /// </summary>
        public void Initialize()
        {
            string[] array = XMLProcess.GetFiles();
            if (array == null) return;
            foreach (var path in array)
            {
                IQJob myjob = XMLProcess.Deserialize(typeof(QJob), XMLProcess.ReadXml(path)) as QJob;
                IMyJob qjob = new MyJob(myjob);
                JobFactory.Instance.Build(myjob);
                DateTime nowtime = Convert.ToDateTime(string.Format("{0}:{1}", DateTime.Now.Hour, DateTime.Now.Minute));
                DateTime jobtime = Convert.ToDateTime(string.Format("{0}:{1}", myjob.StartTime.Hour, qjob.StartTime.Minute));
                if (DateTime.Compare(nowtime, Convert.ToDateTime(jobtime)) > 0)
                    DoJob(qjob);
            }
        }
        /// <summary>
        /// 立即执行job
        /// </summary>
        /// <param name="job"></param>
        void DoJob(IMyJob myjob)
        {
            try
            {
                JobRemove(myjob);
                if (myjob.State != JobState.Working) return;
                //获取订阅委托列表
                myjob.Excute();
                JobAdd(myjob);
            }
            catch (Exception ex)
            { }
        }
        /// <summary>
        /// 任务持久保存
        /// </summary>
        /// <param name="job"></param>
        protected void StandSave(IQJob job)
        {
            job.State = JobState.Working;
            job.Action = JobAction.NewOne;
            string xml = XMLProcess.Serializer(typeof(QJob), job);
            XMLProcess.Write(xml, job.Path());
        }
        /// <summary>
        /// 获取所有任务
        /// </summary>
        /// <returns></returns>
        public List<IMyJob> FindAll()
        {
            return JobVariables.jobs;
        }
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="jobkey">IMyJob.Key()</param>
        /// <returns></returns>
        public bool Remove(string jobkey)
        {
            var myjob = FindByKey(jobkey); ;
            if (myjob == null) return false;
            return JobsRemove(myjob);
        }
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="syscode"></param>
        public void RemoveAll(string syscode)
        {
            for (int i = JobVariables.jobs.Count - 1; i >= 0; i--)
            {
                if (JobVariables.jobs[i].SysCode == syscode)
                    JobsRemove(JobVariables.jobs[i]);
            }
        }
        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="myjob">IQjob</param>
        /// <returns></returns>
        bool JobsRemove(IMyJob myjob)
        {
            try
            {
                bool flag = _scheduler.DeleteJob(new JobKey(myjob.JobId, myjob.SysCode));
                if (flag)
                {
                    JobRemove(myjob);
                    myjob.QRemove();
                }
                return flag;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        ///  暂停任务
        /// </summary>
        /// <param name="jobkey">IMyJob.Key()</param>
        /// <returns></returns>
        public bool Pause(string jobkey)
        {
            try
            {
                var myjob = FindByKey(jobkey); ;
                if (myjob == null) return false;
                return JobsPause(myjob);
            }
            catch (Exception ex)
            {
                return false;
            }
        }
      

以上是关于quartz.net 类库封装的主要内容,如果未能解决你的问题,请参考以下文章

TopShelf+Quartz.net实现基于window服务的定时调度

Quartz.net持久化与集群部署开发详解

Quartz.net持久化与集群部署开发详解

t持久化与集群部署开发详解

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装