标签:
源码地址https://github.com/kmonkey9006/Quatrz.Task
最近项目需要做一写任务作业调度的工作,最终选择了quartz.net这个插件,它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而 创建简单的或复杂的调度。它有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等等.对于quartz.net在这就不进行过的介绍 了,下面针对这个插件的封装具体如下。
quartz.net的封装主要包括:
1.任务的基本操作(创建,删除,暂停,继续,状态查询,数量查询等)
2.任务执行触发动作的回调,其中回调用有两种委托雷响Action,Action<IQjob>
3.持久化的处理,持久化文件保存到xml文件中(一个任务一个xml文件)
对象分为对位接口(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();
}
}
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();
}
}
此接口的实现主要包括两部分:
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;
}
/// <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());
}
}
}
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
{
/// <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);
}
}
}
包括三部分数据:
1.任务的相关的枚举【状态,和动作】
2.任务列表
3.触发动作列表
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; }
}
}
主要包括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;
}
}
}
测试案例不是特别的全。
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();
}
/// <summary>
/// 立即执行,每个2s执行一次,执行5次
/// </summary>
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);
}
}
标签:
原文地址:http://www.cnblogs.com/kmonkeywyl/p/5607070.html