public Task Execute(IJobExecutionContext context) { try { ExeEndQueryTime = DateTime.Now; this.jobDetail = context.MergedJobDataMap[jobDetailMad] as ScheduleJob_Details; this.jobDetailTrigger = context.MergedJobDataMap[triggerMad] as IEnumerable <ScheduleJob_Details_Triggers>; if (this.jobDetail == null) { context.Put(this.ExecResult, "取不到作业计划"); throw new Exception(string.Format("【{0}】的[Execute]从[IJobExecutionContext]读取不到作业计划信息,本次执行失败!", this.JobName)); } Log4.Info(string.Format("【{0}】开始执行IJOB的[Execute]...", this.JobName)); ScheduleJob_Details jobDetailNew = schedulebll.GetScheduleDetail(jobDetail.id); IEnumerable <ScheduleJob_Details_Triggers> jobDetailTriggerNew = schedulebll.ListScheduleDetailsTriggers(jobDetail.sched_name, jobDetail.job_name); //检查新查询出来的计划是否能执行 if (jobDetailNew == null || jobDetailTriggerNew == null || jobDetailTriggerNew.Count() == 0) { context.Scheduler.DeleteJob(context.JobDetail.Key); context.Put(this.ExecResult, "完成"); throw new Exception(string.Format("【{0}】作业计划为空,该记录可能已经被删除。", this.jobDetail.sched_name)); } if (!jobDetailNew.is_durable) { Log4.Info(string.Format("【{0}】作业计划不允许使用,跳过此次执行。", this.jobDetail.description)); context.Put(this.ExecResult, "完成"); return(Task.FromResult(true)); } if (!jobDetailNew.scheEquals(jobDetail) || IsChangedTrigger(jobDetailTrigger, jobDetailTriggerNew)) { context.Scheduler.DeleteJob(context.JobDetail.Key); Log4.Info(string.Format("【{0}】的作业计划属性已更改,将删除该计划的实现作业,然后重新创建一个作业。", this.jobDetail.description)); context.Put(this.ExecResult, "重新创建一个作业"); var JobHelper = context.MergedJobDataMap[jobHelperMad] as IJobHelper; Tuple <IJobDetail, List <ITrigger> > tuple = JobHelper.RestartJob2(context.Scheduler, jobDetail, jobDetailNew); Log4.Info(string.Format("【{0}】的重新创建一个作业完毕,[IJOB.Execute]退出。作业计划:{1},作业:{2},触发器:{3},表达式:{4}。", this.jobDetail.description, jobDetailNew.sched_name, jobDetailNew.description, tuple.Item1.Key, tuple.Item1.Key.Name, tuple.Item2[0].Key.Name, "")); return(Task.FromResult(true)); } //执行具体作业的业务逻辑 _Execute(context); context.Put(this.ExecResult, "成功"); } catch (Exception ex) { Log4.Info(string.Format("【{0}】执行作业失败,消息:{1}", JobName, ex.Message + ex.StackTrace)); } finally { WirteScheduleLog(context); } return(Task.FromResult(true)); }
public bool EditScheduleDetail(ScheduleJob_Details data) { using (IDbConnection connection = SqlConnectionHelper.GetSQLiteConnection()) { var sql = string.Format( @"UPDATE ScheduleJob_Details set sched_name='{1}',job_name='{2}',job_group='{3}',outAssembly='{4}',job_class_name= '{5}',is_durable='{6}',description='{7}',startTime={8},endTime={9},platformMonitoring={10} where id={0};", data.id, data.sched_name, data.job_name, data.job_group, data.outAssembly, data.job_class_name, data.is_durable ? 1 : 0, data.description, data.startTime == null ? "NULL" : data.startTime.ToString(), data.endTime == null ? "NULL" : data.endTime.ToString(), data.platformMonitoring ? 1 : 0); return(connection.Execute(sql) > 0); } }
/// <summary> /// 写入数据 /// </summary> /// <returns></returns> public bool SaveScheduleDetail(ScheduleJob_Details data) { using (IDbConnection connection = SqlConnectionHelper.GetSQLiteConnection()) { var sql = string.Format( @"INSERT INTO ScheduleJob_Details(sched_name,job_name,job_group,outAssembly,job_class_name,is_durable,description,startTime,endTime,platformMonitoring) VALUES( '{0}', '{1}', '{2}', '{3}', '{4}','{5}','{6}',{7},{8},{9}); ", data.sched_name, data.job_name, data.job_group, data.outAssembly, data.job_class_name, data.is_durable ? 1 : 0, data.description, data.startTime == null ? "NULL" : data.startTime.ToString(), data.endTime == null ? "NULL" : data.endTime.ToString(), data.platformMonitoring ? 1 : 0); return(connection.Execute(sql) > 0); } }
/// <summary> /// 根据数据采集计划来创建JOB明细 /// </summary> /// <param name="jobDetail">采集计划</param> /// <param name="clMethod">数据采集方法</param> /// <param name="attachMap">附加的数据</param> /// <returns></returns> public static IJobDetail CreateJobDetail(ScheduleJob_Details jobDetail, IDictionary <string, object> attachMap = null) { if (jobDetail == null) { throw new ArgumentNullException("『CreateJobDetail』的jobDetail参数为空!"); } Type jobType = Type.GetType(jobDetail.job_class_name); if (jobType == null) { throw new NotImplementedException( string.Format("{0}调用的类型『{1}』未实现!", jobDetail.job_name, jobDetail.job_class_name)); } var jobTrigger = _iSchedule.ListScheduleDetailsTriggers(jobDetail.sched_name, jobDetail.job_name); //作业执行上下文携带数据 IDictionary <string, object> dataMap = new Dictionary <string, object>() { { jobDetailMad, jobDetail }, { triggerMad, jobTrigger }, { jobHelperMad, new JobHelper() } }; if (attachMap != null) { foreach (KeyValuePair <string, object> kv in attachMap) { if (!dataMap.ContainsKey(kv.Key)) { dataMap.Add(kv); } } } IJobDetail job = JobBuilder .Create(jobType) .WithDescription(jobDetail.description) .WithIdentity(GetJobKey(jobDetail)) .UsingJobData(new JobDataMap(dataMap)) .Build(); return(job); }
//使用采集计划来创建作业 private void ScheduleJobByPlan(IScheduler sched, ScheduleJob_Details JobDetail) { JobHelper.ScheduleJobByPlan(_QtzScheduler, JobDetail); }
/// <summary> /// 使用采集计划来创建作业 /// </summary> /// <param name="qtzScheduler">调度器</param> /// <param name="jobDetail">采集计划</param> /// <returns>二元组</returns> public static Tuple <IJobDetail, List <ITrigger> > ScheduleJobByPlan(IScheduler qtzScheduler, ScheduleJob_Details jobDetail) { Tuple <IJobDetail, List <ITrigger> > tuple = null; List <ITrigger> triggerList = new List <ITrigger>(); if (jobDetail.is_durable) { IJobDetail ij = CreateJobDetail(jobDetail); var jobTriggers = _iSchedule.ListScheduleDetailsTriggers(jobDetail.sched_name, jobDetail.job_name); foreach (var trigger in jobTriggers) { ITrigger ig = CreateTrigger(trigger); qtzScheduler.ScheduleJob(ij, ig); triggerList.Add(ig); } tuple = new Tuple <IJobDetail, List <ITrigger> >(ij, triggerList); } return(tuple); }
/// <summary> /// 根据采集计划产生作业的唯一标识 /// </summary> /// <param name="jobDetail">采集计划</param> /// <param name="clMethod">采集方式</param> /// <returns></returns> public static JobKey GetJobKey(ScheduleJob_Details jobDetail) { return(new JobKey( string.Format(JOBNameFormat, jobDetail.sched_name, jobDetail.job_name, jobDetail.job_group, true), jobDetail.job_group)); }
/// <summary> /// 当采集计划各属性发生改变时,需要重新启动此作业 /// </summary> /// <param name="qtzScheduler">调度器</param> /// <param name="jobDetailOld">旧的采集计划</param> /// <param name="jobDetailNew">新采集计划</param> /// <param name="clMethod">采集方法</param> /// <returns></returns> public Tuple <IJobDetail, List <ITrigger> > RestartJob2(IScheduler qtzScheduler, ScheduleJob_Details jobDetailOld, ScheduleJob_Details jobDetailNew) { //采集计划属性发生变更,则从调度器中删除此作业 JobKey jobKey = GetJobKey(jobDetailOld); IJobDetail ij = CreateJobDetail(jobDetailNew); List <ITrigger> triggerList = new List <ITrigger>(); qtzScheduler.DeleteJob(jobKey); //删除旧的作业 var jobTriggers = _iSchedule.ListScheduleDetailsTriggers(jobDetailNew.sched_name, jobDetailNew.job_name); foreach (var trigger in jobTriggers) { ITrigger ig = CreateTrigger(trigger, ij); qtzScheduler.ScheduleJob(ij, ig);//调度新的作业 //创建一个立即执行的触发器 ig = JobHelper.CreateOnceTrigger(5, ij); qtzScheduler.ScheduleJob(ig); triggerList.Add(ig); } return(new Tuple <IJobDetail, List <ITrigger> >(ij, triggerList)); }