1、前言
目录:
最近项目需要做一写任务作业调度的工作,最终选择了quartz.net这个插件,它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而 创建简单的或复杂的调度。它有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等等.对于quartz.net在这就不进行过的介绍 了,下面针对这个插件的封装具体如下。
quartz.net的封装主要包括:
1.任务的基本操作(创建,删除,暂停,继续,状态查询,数量查询等)
2.任务执行触发动作的回调,其中回调用有两种委托雷响Action,Action<IQjob>
3.持久化的处理,持久化文件保存到xml文件中(一个任务一个xml文件)
2、定义对象接口
对象分为对位接口(IQJob)和内部操作接口(IMJob).除了对象本身,接口还包括对对象的一些简单操作,比如Remove,pause,Resume等.这样的目的是为了让对象更便与操作。
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Quartz{ ////// quartz.net接口对象 /// 作者:王延领 /// 时间:2016/5/5 /// public interface IQJob { ////// 系统代码 /// string SysCode { get; set; } ////// 任务id /// string JobId { get; set; } ////// 任务名称 /// string Name { get; set; } ////// 任务分组 /// string Group { get; set; } ////// 间隔时间 /// int Seconds { get; set; } ////// 做多执行次数,如果是<1,则无限循环 /// int MaxTimes { get; set; } ////// 开始执行时间 /// DateTime StartTime { get; set; } ////// 任务处理器 /// Action Handler { get; set; } ////// 任务处理器 /// ActionDetailHandler { get; set; } /// /// 当前执行的第几次 /// int Times { get; set; } ////// 接口执行时间 /// DateTime LastTime { get; set; } ////// 任务的当前状态 /// JobState State { get; set; } ////// 本次任务执行的动作 /// JobAction Action { get; set; } ////// 开始执行任务 /// void Start(); ////// 开始执行任务 /// /// 任务开始时间 /// 间隔时间(s) /// 执行次数 void Start(DateTime starttime, int internaltimes = 60*60, int maxtimes = 0); ////// 任务触发动作 /// 无需参数 /// /// 触发的动作 ///IQJob Handle(Action handler); /// /// 任务触发动作 /// /// 触发的动作 ///IQJob Handle(Action handler); string Key(); bool Load(); bool Save(); /// /// 下次运行时间 /// DateTime NextTime(); ////// 获取job文件地址 /// ///string Path(); /// /// 移除 /// ///bool Remove(); /// /// 挂起 /// void Pause(); //////继续执行 /// 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{ ////// quartz.net任务调度接口 /// 作者:王延领 /// 日期:2016/5/5 /// public interface IJobFactory { ////// 任务触发的动作 /// /// 系统编码 /// 需要参数的触发动作 void Trigger(string syscode, Actionhander); /// /// 任务触发的动作 /// /// 系统编码 /// 无需参数的触发动作 void Trigger(string syscode, Action hander); ////// 创建任务 /// /// IQJob ///string Build(IQJob job); /// /// 移除任务 /// /// IMyJob.Key() ///bool Remove(string jobkey); /// /// 暂停任务 /// /// IMyJob.Key() ///bool Pause(string jobkey); /// /// 继续任务 /// /// IMyJob.Key() ///bool Resume(string jobkey); /// /// 任务是否存在 /// /// IMyJob.Key() ///bool Exists(string jobkey); /// /// 移除任务 /// /// 系统编码 void RemoveAll(string systcode); ////// 暂停任务 /// /// 系统编码 void PauseAll(string syscode); ////// 继续任务 /// /// 系统编码 void ResumeAll(string syscode); ////// 任务数 /// /// 系统编码 /// 任务状态 ///int JobCount(string syscode, JobState state); /// /// 任务数 /// ///int JobCount(); /// /// 任务状态 /// /// IMyJob.Key() ///JobState State(string jobkey); /// /// 获取任务 /// /// IMyJob.Key() ///IMyJob FindByKey(string jobkey); /// /// 任务初始化 /// void Initialize(); }}
4、对象接口的实现
此接口的实现主要包括两部分:
1.对外操作的创建,删除,暂停,继续,状态和个数的查询,对象load和sava
2.执行job操作是进行动作触发的操作。
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; } //////任务执行时触发动作 /// 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) { } } //////任务暂停时触发动作 /// public void QPause() { Action = JobAction.Pend; State = JobState.Pending; Save(); Trigger(); } ////// 任务继续时触发动作 /// public void QResume() { Action = JobAction.Resume; State = JobState.Working; Save(); Trigger(); } ////// 任务移除触发动作 /// public void QRemove() { XMLProcess.Delete(Path()); Action = JobAction.Delete; Trigger(); } ////// 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/// 动作触发 /// /// JobDetail 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{ ////// quartz.net对象 /// 作者:王延领 /// 时间:2016/5/5 /// public class QJob : IQJob { ////// 构造函数 /// public QJob() { } ////// 构造函数 /// /// 系统编码 /// 任务id【需系统内唯一】 /// 任务名称 /// 任务群组 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 ActionDetailHandler { get; set; } /// 持久化保存 /// (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; } } /// /// 任务的jobkey规则 /// ///public string Key() { return SysCode + "_" + JobId; } /// /// 开始执行任务 /// public void Start() { JobFactory.Instance.Build(this); } ////// 开始执行任务 /// /// 开始执行时间 /// 时间间隔(s) /// 执行次数 public void Start(DateTime starttime, int internaltimes = 60*60, int maxtimes = 0) { StartTime = starttime; Seconds = internaltimes; MaxTimes = maxtimes; JobFactory.Instance.Build(this); } ////// 下次执行时间 /// ///public DateTime NextTime() { return LastTime.AddSeconds(Seconds); } /// ///任务触发动作 /// /// 需要任务信息的动作 ///IMyJob public IQJob Handle(Action handler) { Handler = handler; return this; } ////// 任务触发动作 /// /// 需要任务信息的动作 ///IMyJob public IQJob Handle(Actionhandler) { DetailHandler = handler; return this; } /// /// 持久化地址 /// ///【例:../job/syscode_name_group_jobid.xml】 public string Path() { return System.IO.Path.Combine(JobFactory.Instance.GetPath(), string.Format("{0}_{1}_{2}_{3}.xml", SysCode, Group, Name, JobId)); } ////// 移除任务 /// ///public bool Remove() { return JobFactory.Instance.Remove(Key()); } /// /// 暂停任务 /// public void Pause() { JobFactory.Instance.Pause(Key()); } ////// 继续执行任务 /// public void Resume() { JobFactory.Instance.Resume(Key()); } }}
5、quartz.net接口实现
1.创建任务的时候主要有散布操作:
1.创建jobdetail
2.创建jobtrigger
3.创建jobexcute
2.对任务的操作都会执行相应的静态变量jobs【list<IMJob>】
3.持久化操作包操作数据保存到xml中
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{ ////// quartz.net接任务调度口实现 /// 作者:王延领 /// 时间:2016/5/5 /// public class JobFactory : IJobFactory { ////// 单例模式 /// 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; ////// 任务持久化文件保存地址 /// 注:默认保存在@"..\..\jobs\"文件夹下 /// 直接地址结尾加"\" /// 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; } ////创建任务 // //任务对象 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(); } ////// 创建jobdetail /// /// /// 默认执行Create_Execute /// ///protected IJobDetail Create_Jobdetail(IMyJob qjob) { IJobDetail jobdetail = JobBuilder.Create () .WithIdentity(qjob.JobId, qjob.SysCode) .Build(); return jobdetail; } /// /// 创建job触发器 /// /// ///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; } /// /// 创建任务执行 /// 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); } } ////// 从任务列表中删除指定对象 /// /// ///bool JobRemove(IMyJob myjob) { return JobVariables.jobs.Remove(myjob); } /// /// 向任务列表中添加指定对象 /// /// void JobAdd(IMyJob myjob) { JobVariables.jobs.Insert(0, myjob); } ////// 获取MyJob /// /// JobDetail.JobKey ///IMyJob Find(JobKey jobkey) { return JobVariables.jobs.SingleOrDefault(j => j.JobId == jobkey.Name && j.SysCode == jobkey.Group); } /// /// 获取任务 /// /// IMyJob.Key() ///public IMyJob FindByKey(string jobkey) { var job = JobVariables.jobs.SingleOrDefault(j => j.Key() == jobkey); return job; } /// /// 初始化任务 /// 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); } } ////// 立即执行job /// /// void DoJob(IMyJob myjob) { try { JobRemove(myjob); if (myjob.State != JobState.Working) return; //获取订阅委托列表 myjob.Excute(); JobAdd(myjob); } catch (Exception ex) { } } ////// 任务持久保存 /// /// 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()); } ////// 获取所有任务 /// ///public List FindAll() { return JobVariables.jobs; } /// /// 删除任务 /// /// IMyJob.Key() ///public bool Remove(string jobkey) { var myjob = FindByKey(jobkey); ; if (myjob == null) return false; return JobsRemove(myjob); } /// /// 删除任务 /// /// 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]); } } ////// 移除任务 /// /// IQjob ///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; } } /// /// 暂停任务 /// /// IMyJob.Key() ///public bool Pause(string jobkey) { try { var myjob = FindByKey(jobkey); ; if (myjob == null) return false; return JobsPause(myjob); } catch (Exception ex) { return false; } } /// /// 暂停任务 /// /// 系统编码 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]); } } ////// 暂停任务 /// /// ///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; } } /// /// 继续任务 /// /// IMyJob.Key() public bool Resume(string jobkey) { var myjob = FindByKey(jobkey); ; if (myjob == null) return false; return JobsResume(myjob); } ////// 继续任务 /// /// 系统编码 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; } } ////// 任务是否存在 /// /// IMyJob.Key() ///public bool Exists(string jobkey) { return JobVariables.jobs.Exists(j => j.Key() == jobkey); } /// /// 任务状态 /// /// IMyJob.Key() ///public JobState State(string jobkey) { var myjob = FindByKey(jobkey); if (myjob == null) return JobState.Empty; return myjob.State; } /// /// 获取任务数量 /// ///所有任务数量 public int JobCount() { return JobVariables.jobs.Count; } ////// 获取任务数量 /// /// 系统编码 /// 任务状态 ///指定任务数量 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, Actionaction) { 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操作中的静态变量
包括三部分数据:
1.任务的相关的枚举【状态,和动作】
2.任务列表
3.触发动作列表
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Quartz{ ////// 任务状态 /// public enum JobState { Working = 0, Pending = 1, Empty = 2 } ////// 任务动作 /// public enum JobAction { NewOne = 0, Excute = 1, Delete = 2, Pend = 3, Resume = 4 } public class JobVariables { ////// 任务调度集合 /// private static List_jobs = new List (); public static List jobs { get { return _jobs; } } /// /// 任务触发动作集合 /// private static List_excutehandlers = new List (); public static List GetHandlers { get { return _excutehandlers; } } } public class JobExcuteHandler { public string systme_code { get; set; } public Action detailexcute { get; set; } public Action excute { get; set; } }}
7、XML序列化
主要包括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; } } ////// 导入XML文件 /// /// XML文件路径 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; } ////// 获取指定文件夹下的xml文件物理地址 /// /// 文件路径 ///物理地址数组 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; } } ////// 把xml字符串转为xml文件 /// /// xml字符串 /// 文件名称xml格式 ///保存的物理地址【默认:\bin\..\..\jobs\】 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) { } } ////// 删除文件 /// /// public static void Delete(string filefullpath) { System.IO.File.Delete(filefullpath); } ////// 反序列化 /// /// 类型 /// XML字符串 ///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; } } /// /// 反序列化 /// /// 类型 /// XML字符串 ///public static T Deserialize (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); } } /// /// 序列化 /// /// 类型 /// 对象 ///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; } }}
8、控制台测试
测试案例不是特别的全。
public class MyJobTests { private static MyJobTests _Instance = new MyJobTests(); public static MyJobTests Instance { get { return _Instance; } } public void JobHandler() { Console.WriteLine("job:立即执行每个2s执行一次,执行5次"); } public void Job1() { Console.WriteLine("job1:每隔2s执行,执行5次"); } public void Job2() { Console.WriteLine("job2:与job 1syscode相同,每隔2s执行,执行5次"); } public void Job3() { Console.WriteLine("job3,每隔2s执行,执行5次"); } public void Job4() { Console.WriteLine("job4 :与job3 jobid相同,每隔2s执行一次,执行5次"); } public void job5() { Console.WriteLine("两个job5 : jobid,syscode都相同, 每隔2s执行一次,执行5次"); } public void JobDetailHandler(IQJob job) { Console.WriteLine("系统启动40s后,无限次执行,需要参数的动作" + job.Times + "_" + job.Action + "_" + job.State); } public void JobPending() { Console.WriteLine("jobpending:立即执行,没2s执行1次,执行10次,用于测试pending,pengding的触发会在5s后执行。"); } public void JobResume() { Console.WriteLine("jobresume:立即执行,没2s执行1次,执行5次,pengding的触发会在5s后执行,5后resume执行"); } public void MyJobSpendingTest() { new QJob("syscodepeng", "jobidpeng", "name", "group").Handle(JobPending).Start(DateTime.Now, 2, 5); Thread.Sleep(5000); new QJob("syscodepeng", "jobidpeng", "name", "group").Pause(); } public void MyJobResumeTest() { new QJob("syscodeResume", "jobid", "name", "group").Handle(JobResume).Start(DateTime.Now, 2, 5); Thread.Sleep(5000); new QJob("syscodeResume", "jobid", "name", "group").Pause(); Thread.Sleep(5000); new QJob("syscodeResume", "jobid", "name", "group").Resume(); } ////// 立即执行,每个2s执行一次,执行5次 /// public void MyJobsTest() { new QJob("syscode1", "jobid", "name", "group").Handle(JobHandler).Start(DateTime.Now, 2, 5); } public void MyJobsTest1() { new QJob("syscode1", "jobid1", "name", "group").Handle(Job3).Start(DateTime.Now.AddSeconds(5), 2, 5); new QJob("syscode2", "jobid1", "name", "group").Handle(Job4).Start(DateTime.Now.AddSeconds(8), 2, 5); } public void MyJobsTest2() { new QJob("syscode3", "jobid3", "name", "group").Handle(Job1).Start(DateTime.Now.AddSeconds(15), 2, 5); new QJob("syscode3", "jobid4", "name", "group").Handle(Job2).Start(DateTime.Now.AddSeconds(18), 2, 5); } public void MyJobsTest3() { new QJob("syscode5", "jobid5", "name", "group").Handle(job5).Start(DateTime.Now.AddSeconds(30), 2, 5); new QJob("syscode5", "jobid5", "name", "group").Handle(job5).Start(DateTime.Now.AddSeconds(32), 2, 5); } public void MyJobsTest4() { new QJob("syscode6", "jobid6", "name", "group").Handle(JobDetailHandler).Start(DateTime.Now.AddSeconds(40), 2, 0); } public void MyJobInitialize() { int i = JobFactory.Instance.JobCount(); // Console.WriteLine("初始化后任务个数" + i); JobFactory.Instance.SetPath(@"C:\InitializeFILE"); IQJob job = new QJob("SYSCODEiNS", "JOBID"); string xml = XMLProcess.Serializer(typeof(QJob), job); XMLProcess.Write(xml, job.Path()); JobFactory.Instance.Initialize(); i = JobFactory.Instance.JobCount(); JobFactory.Instance.SetPath(""); Console.WriteLine("初始化后任务个数" + i); } }
9、源码
源码地址