Exemple #1
0
 /// <summary>
 /// 读取配置文件
 /// </summary>
 /// <returns></returns>
 public static List<TaskUtil> ReadConfig()
 {
     List<TaskUtil> list = new List<TaskUtil>();
     TaskUtil taskUtil = null;
     string text = string.Empty;
     XmlDocument dom = new XmlDocument();
     dom.Load(configPath);
     XmlNodeList nodeList = dom.SelectNodes("//TaskSet/Task");
     foreach (XmlNode node in nodeList)
     {
         taskUtil = new TaskUtil();
         foreach (XmlNode childNode in node.ChildNodes)
         {
             text = childNode.InnerText;
             switch (childNode.Name)
             {
                 case "TaskID":
                     taskUtil.TaskID = text;
                     break;
                 case "TaskName":
                     taskUtil.TaskName = text;
                     break;
                 case "TaskParam":
                     taskUtil.TaskParam = text;
                     break;
                 case "CronExpressionString":
                     taskUtil.CronExpressionString = text;
                     break;
                 case "Assembly":
                     taskUtil.Assembly = text;
                     break;
                 case "Class":
                     taskUtil.Class = text;
                     break;
                 case "IsExcute":
                     taskUtil.IsExcute = Convert.ToBoolean(text);
                     break;
                 default: break;
             }
         }
         list.Add(taskUtil);
     }
     return list;
 }
Exemple #2
0
 /// <summary>
 /// 启用任务
 /// <param name="taskUtil">任务信息</param>
 /// <returns>返回任务trigger</returns>
 /// </summary>
 public static void ScheduleJob(TaskUtil taskUtil)
 {
     JobKey jk = new JobKey(taskUtil.TaskID);
     if (scheduler.CheckExists(jk))
     {
         //任务已经存在则删除
         scheduler.DeleteJob(jk);
     }
     //验证是否正确的Cron表达式
     if (ValidExpression(taskUtil.CronExpressionString))
     {
         IJobDetail job = new JobDetailImpl(taskUtil.TaskID.ToString(), GetClassInfo(taskUtil.Assembly,taskUtil.Class));
         CronTriggerImpl trigger = new CronTriggerImpl();
         trigger.CronExpressionString = taskUtil.CronExpressionString;
         trigger.Name = taskUtil.TaskID;
         trigger.Description = taskUtil.TaskName;
         //添加任务执行参数
         job.JobDataMap.Add("TaskParam", taskUtil.TaskParam);
         scheduler.ScheduleJob(job, trigger);
     }
     else
     {
         throw new Exception(taskUtil.CronExpressionString + "不是正确的Cron表达式,无法启动该任务!");
     }
 }
Exemple #3
0
 /// <summary>
 /// 启用任务
 /// <param name="taskUtil">任务信息</param>
 /// <returns>返回任务trigger</returns>
 /// </summary>
 public static void ScheduleJob(TaskUtil taskUtil)
 {
     //先删除现有已存在任务
     DeleteJob(taskUtil.TaskID);
     //验证是否正确的Cron表达式
     if (ValidExpression(taskUtil.CronExpressionString))
     {
         IJobDetail job = new JobDetailImpl(taskUtil.TaskID.ToString(), GetClassInfo(taskUtil.Assembly,taskUtil.Class));
         CronTriggerImpl trigger = new CronTriggerImpl();
         trigger.CronExpressionString = taskUtil.CronExpressionString;
         trigger.Name = taskUtil.TaskID;
         trigger.Description = taskUtil.TaskName;
         //添加任务执行参数
         job.JobDataMap.Add("TaskParam", taskUtil.TaskParam);
         scheduler.ScheduleJob(job, trigger);
         LogHelper.WriteLog(string.Format("任务“{0}”启动成功,未来5次运行时间如下:", taskUtil.TaskName));
         List<DateTime> list = GetTaskeFireTime(taskUtil.CronExpressionString, 5);
         foreach (var time in list)
         {
             LogHelper.WriteLog(time.ToString());
         }
     }
     else
     {
         throw new Exception(taskUtil.CronExpressionString + "不是正确的Cron表达式,无法启动该任务!");
     }
 }