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;            }        }        /// <summary>        /// 暂停任务        /// </summary>        /// <param name="syscode">系统编码</param>        public void PauseAll(string syscode)        {            for (int i = JobVariables.jobs.Count - 1; i >= 0; i--)            {                if (JobVariables.jobs[i].SysCode == syscode)                    JobsPause(JobVariables.jobs[i]);            }        }        /// <summary>        /// 暂停任务        /// </summary>        /// <param name="myjob"></param>        /// <returns></returns>        bool JobsPause(IMyJob myjob)        {            try            {                _scheduler.PauseJob(new JobKey(myjob.JobId, myjob.SysCode));                JobRemove(myjob);                myjob.QPause();                JobAdd(myjob);                return true;            }            catch (Exception ex)            {                return false;            }        }        /// <summary>        /// 继续任务        /// </summary>        /// <param name="jobkey">IMyJob.Key()</param>        public bool Resume(string jobkey)        {            var myjob = FindByKey(jobkey); ;            if (myjob == null) return false;            return JobsResume(myjob);        }        /// <summary>        /// 继续任务        /// </summary>        /// <param name="syscode">系统编码</param>        public void ResumeAll(string syscode)        {            for (int i = JobVariables.jobs.Count - 1; i >= 0; i--)            {                if (JobVariables.jobs[i].SysCode == syscode)                    JobsResume(JobVariables.jobs[i]);            }        }        bool JobsResume(IMyJob myjob)        {            try            {                _scheduler.ResumeJob(new JobKey(myjob.JobId, myjob.SysCode));                JobRemove(myjob);                myjob.QResume();                JobAdd(myjob);                return true;            }            catch (Exception ex)            {                return false;            }        }        /// <summary>        /// 任务是否存在        /// </summary>        /// <param name="jobkey">IMyJob.Key()</param>        /// <returns></returns>        public bool Exists(string jobkey)        {            return JobVariables.jobs.Exists(j => j.Key() == jobkey);        }        /// <summary>        /// 任务状态        /// </summary>        /// <param name="jobkey">IMyJob.Key()</param>        /// <returns></returns>        public JobState State(string jobkey)        {            var myjob = FindByKey(jobkey);            if (myjob == null) return JobState.Empty;            return myjob.State;        }        /// <summary>        /// 获取任务数量        /// </summary>        /// <returns>所有任务数量</returns>        public int JobCount()        {            return JobVariables.jobs.Count;        }        /// <summary>        /// 获取任务数量        /// </summary>        /// <param name="syscode">系统编码</param>        /// <param name="state">任务状态</param>        /// <returns>指定任务数量</returns>        public int JobCount(string syscode, JobState state)        {            return JobVariables.jobs.FindAll(j => j.State == state && j.SysCode == syscode).Count;        }        public void Trigger(string systme_code, Action<IQJob> action)        {            var sh = JobVariables.GetHandlers.SingleOrDefault(h => h.systme_code == systme_code);            if (sh == null) sh = new JobExcuteHandler() { systme_code = systme_code };            JobVariables.GetHandlers.Remove(sh);            sh.detailexcute = action;            JobVariables.GetHandlers.Add(sh);        }        public void Trigger(string systme_code, Action action)        {            var sh = JobVariables.GetHandlers.SingleOrDefault(h => h.systme_code == systme_code);            if (sh == null) sh = new JobExcuteHandler() { systme_code = systme_code };            JobVariables.GetHandlers.Remove(sh);            sh.excute = action;            JobVariables.GetHandlers.Add(sh);        }    }}

6、job操作中的静态变量

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

namespace Quartz
{
    /// <summary>
    /// 任务状态
    /// </summary>
    public enum JobState
    {
        Working = 0,
        Pending = 1,
        Empty = 2
    }
    /// <summary>
    /// 任务动作
    /// </summary>
    public enum JobAction
    {

        NewOne = 0,
        Excute = 1,
        Delete = 2,
        Pend = 3,
        Resume = 4
    }
    public class JobVariables
    {
        /// <summary>
        /// 任务调度集合
        /// </summary>
        private static List<IMyJob> _jobs = new List<IMyJob>();
        public static List<IMyJob> jobs
        {
            get
            {
                return _jobs;
            }
        }
        /// <summary>
        /// 任务触发动作集合
        /// </summary>
        private static List<JobExcuteHandler> _excutehandlers = new List<JobExcuteHandler>();
        public static List<JobExcuteHandler> GetHandlers
        {
            get
            {
                return _excutehandlers;
            }
        }
    }
    public class JobExcuteHandler
    {
        public string systme_code { get; set; }
        public Action<IQJob> detailexcute { get; set; }
        public Action excute { get; set; }
    }
}

7、XML序列化


 using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace Quartz
{
    public class XMLProcess
    {
        public XMLProcess()
        { }

        public XMLProcess(string strpath)
        {
            this._xmlpath = strpath;
        }

        private string _xmlpath;
        public string XMLPath
        {
            get { return this._xmlpath; }
        }

        /// <summary>
        /// 导入XML文件
        /// </summary>
        /// <param name="XMLPath">XML文件路径</param>
        private XmlDocument XMLLoad()
        {
            string XMLFile = XMLPath;
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile;
                if (File.Exists(filename)) xmldoc.Load(filename);
            }
            catch (Exception e)
            { }
            return xmldoc;
        }
        public static string ReadXml(string path)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(path);
            return xd.InnerXml;
        }

        /// <summary>
        /// 获取指定文件夹下的xml文件物理地址
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>物理地址数组</returns>
        public static string[] GetFiles()
        {
            try
            {
                string pathname = JobFactory.Instance.GetPath();
                if (Directory.Exists(pathname))
                    return Directory.GetFiles(pathname, "*.xml");
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 把xml字符串转为xml文件
        /// </summary>
        /// <param name="strxml">xml字符串</param>
        /// <param name="filename">文件名称xml格式</param>
        ///<param name="path">保存的物理地址【默认:\bin\..\..\jobs\】</param>
        public static void Write(string strxml, string filefullname)
        {
            try
            {

                var path = JobFactory.Instance.GetPath();
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                //if (File.Exists(pathname + filename) && filename.ToLower().EndsWith(".xml")) return;

                using (StreamWriter sw = new StreamWriter(filefullname, false))
                {
                    sw.Write(strxml);
                    sw.Close();
                }
            }
            catch (Exception ex)
            { }
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filefullpath"></param>
        public static void Delete(string filefullpath)
        {
            System.IO.File.Delete(filefullpath);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="xml">XML字符串</param>
        /// <returns></returns>
        public static object Deserialize(Type type, string xml)
        {
            try
            {
                using (StringReader sr = new StringReader(xml))
                {
                    XmlSerializer xmldes = new XmlSerializer(type);
                    return xmldes.Deserialize(sr);
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="xml">XML字符串</param>
        /// <returns></returns>
        public static T Deserialize<T>(string xml) where T : class,new()
        {
            try
            {
                using (StringReader sr = new StringReader(xml))
                {
                    XmlSerializer xmldes = new XmlSerializer(typeof(T));
                    return xmldes.Deserialize(sr) as T;
                }
            }
            catch (Exception e)
            {
                return default(T);
            }
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string Serializer(Type type, object obj)
        {
            string XMLSTR = string.Empty;
            MemoryStream Stream = new MemoryStream();
            XmlSerializer xml = new XmlSerializer(type);
            try
            {
                //序列化对象
                xml.Serialize(Stream, obj);
                XMLSTR = Encoding.UTF8.GetString(Stream.ToArray());
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            Stream.Dispose();
            return XMLSTR;
        }

    }
}
时间: 2024-11-04 17:33:28

quartz.net 类库封装的相关文章

quartz.net插件类库封装

1.前言 最近项目需要做一写任务作业调度的工作,最终选择了quartz.net这个插件,它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而 创建简单的或复杂的调度.它有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等等.对于quartz.net在这就不进行过的介绍 了,下面针对这个插件的封装具体如下. quartz.net的封装主要包括: 1.任务的基本操作(创建,删除,暂停,继续,状态查询,数量查询等) 2.任务执行触发动作的回调,其中回调用有两种委托雷响Ac

quartz.net插件类库封装(含源码)

1.前言 源码地址https://github.com/kmonkey9006/Quatrz.Task 最近项目需要做一写任务作业调度的工作,最终选择了quartz.net这个插件,它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而 创建简单的或复杂的调度.它有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等等.对于quartz.net在这就不进行过的介绍 了,下面针对这个插件的封装具体如下. quartz.net的封装主要包括: 1.任务的基本操作(创建,删除

9、ABPZero系列教程之拼多多卖家工具 拼团提醒类库封装

本篇开始正式做功能,我在开发拼团提醒之前,拼多多并没有放出拼团人数不足就提醒卖家的功能. 有这个想法主要来源于朋友的抱怨,我想这应该是大部分卖家的心声吧. 经过分析,拿到了几个api,不要问我api怎么拿到的,这不是本系列的内容. 接口1:http://apiv4.yangkeduo.com/mall/{0}/info?pdduid=0(传入店铺编号,可以获取店铺资料) 接口2:http://apiv4.yangkeduo.com/v2/mall/{0}/goods?page=1&size=50

用于新浪微博,腾讯QQ,淘宝 OAuth2.0 登陆的NET类库封装

目前开放OAuth2互联接口比较常用的有新浪微博,腾讯QQ,淘宝,其实还有微信,只是微信的我木有申请下来,因为手续麻烦,貌似需要扫描它的协议签字然后发过去,所以就放弃封装微信OAuth2登陆接口了,待以后给补上!关于淘宝OAuth网站接入登陆目前淘宝是暂停审核了貌似,也许是因为财大气粗比较任性吧,不过你还是可申请账号的,可以做应用提交给淘宝. 需要源代码的小伙伴请猛戳这里下载,需要已经编译好Dll的小伙伴请猛戳这里下载. 一. 下面给出各个OAuth2.0申请合作的地址: -- sinaweib

Quartz.net自定义类库接口 使用说明文档

前言: 前端时间共享了quartz.net.有给要源码的,其实贴出的代码已经写的很清楚,有可能没有类库的操作说明,所以今天就写了一个操作说明,希望能有所帮助. 先贴出quartz.net类库的地址: quartz.net插件类库封装 目的:方便任务调度 的创建,删除,暂停与继续操作:提高任务在系统的可维护性与可扩展性. 使用流程: 1.引用..\svn\ref\thirdparty\quartz\net40文件夹下所有dll. 2.配置web.config,如下图 3.在使用处调用类库方法: 3

【类库】私房干货.Net数据层方法的封装

[类库]私房干货.Net数据层方法的封装 作者:白宁超 时间:2016年3月5日22:51:47 摘要:继上篇<Oracle手边常用70则脚本知识汇总>文章的发表,引起很多朋友关注.便促使笔者收集整理此文.本文主要针是对微软技术对数据库(下文案例采用的)操作时,调用执行方法的封装,这也是数年逐渐学习.吸收.实践.完成的一个类库.其中不免有不合理之处,亦或是不符合个别读者的使用习惯.在此,共享此文,权当互相学习.(本文原创,转载注明出处:私房干货.Net数据层方法的封装) 1 概述 本文分以下几

任务调度框架-Quartz.Net

使用Quartz.Net依赖于以下3个组件:Common.Logging.dll.Common.Logging.Core.dll.Quartz.dll 简单封装 1 using Quartz; 2 using Quartz.Impl; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 na

Spring.Scheduling.Quartz的使用

最近因使用Spring.Net框架而接触.了解到其与Quartz.Net的集成,即Spring.Scheduling.Quartz模块. Spring通过对Quartz.Net的封装,采用了spring核心注入思路,给我们提供更为简单.便利的使用方式.当然直接使用Quartz.Net也是可以的. 在使用的过程中Spring.Net版本为1.3.2,而该版本的Spring.Scheduling.Quartz只支持Quartz.Net(1.0.3版),无法支持Quartz.Net(>=2.0.1版)

IOS 编程中引用第三方的方类库的方法及常见问题(转载)

//原文:http://www.th7.cn/Program/IOS/201407/244585.shtml 方法一:直接复制所有源文件到项目中 这种方法就是把第三方类库的所有源文件复制到项目中,直接把所有.h和.m文件拖到XCode项目中即可. 注意: 1. 如果第三方类库引用了一些系统自带类库,那么在项目中还需要额外引用那些类库. 2. 如果当前的项目启用了ARC,而引用的第三方类库未使用ARC,那还需要在项目信息的Targets – Build Parses 里找到第三方类库的.m文件,并