Quartz.net open job scheduling framework using open source job scheduling framework Quartz.net Detailed Explanation use

Original: Quartz.net open source job scheduling framework uses Detailed

 

Foreword

quartz.net job scheduling .net framework is a great organization OpenSymphony developed quartz scheduler project extends portable version. Support  cron-like expression , clustering, database. Not to mention the powerful performance features.

Download project documentation official website: http://www.quartz-scheduler.net/

Project to be quoted: Common.Logging.dll, Common.Logging.Core.dll, Quartz.dll 

Here to tell a little about the project planning and scheduling of decomposition everyone I did recently, to quartz.net functions and common methods aid in the understanding.

 

The simple use of quartz.net - Getting Started

If you are a novice to use quartz.net, the console entry here, recommended that follow do next, then 10 minutes to get to know quartz.net is so easy to do.

1. Create a once every 3 seconds to perform planning and scheduling

Copy the code
RunMain class public 
    { 
        static void the Main (String [] args) 
        { 
            Console.WriteLine (DateTime.Now.ToString ( "R & lt")); 
            .. 1 // first create a job scheduler pool 
            ISchedulerFactory schedf the StdSchedulerFactory new new = (); 
            IScheduler sched schedf.GetScheduler = (); 
            . 2 // Create out a particular job 
            IJobDetail job = JobBuilder.Create <JobDemo> () the Build ();.            
            .. 3 // Create and configure a trigger 
            ISimpleTrigger trigger = (ISimpleTrigger) TriggerBuilder .. .create () WithSimpleSchedule (X => x.WithIntervalInSeconds (. 3) .WithRepeatCount (Int.MaxValue)) the Build (); 
            // Add job scheduling cell. 4  
            sched.ScheduleJob (job, Trigger);
            . @ 5 runs 
            sched.Start (); 
            the Console.ReadKey (); 
        } 
    } 
    public class JobDemo: IJob 
    { 
        /// <Summary> 
        /// This is the method of performing the job scheduling timing of each 
        /// </ Summary> 
        /// <param = name "context"> </ param> 
        public void the Execute (IJobExecutionContext context) 
        { 
            Console.WriteLine (DateTime.Now.ToString ( "R & lt")); 
        } 
    }
Copy the code

Note: 1, note the order of jobs created scheduling. 2, the results of the code above is performed once every three seconds in the Execute JobDemo, if the program does not stop, the endless performed forever, Oh, tear egg ah.

2. Improved (rich scheduling): on a job, I want him executed once every three seconds, perform a total of 100 times, the execution time is set to start at the current time, I set the end time after 2 hours, but 100 times 2 not executing the child are no longer executed.

Copy the code
    RunMain class public 
    { 
        static void the Main (String [] args) 
        { 
            Console.WriteLine (DateTime.Now.ToString ( "R & lt")); 
            // first create a job scheduler pool 
            ISchedulerFactory schedf the StdSchedulerFactory new new = (); 
            IScheduler sched = schedf .GetScheduler (); 
            // Create out a particular job 
            IJobDetail job = JobBuilder.Create <JobDemo> () the Build ();. 
            // NextGivenSecondDate: If the first argument is null then the current time table two seconds later postponed the point in time. 
            The startTime = DateBuilder.NextGivenSecondDate the DateTimeOffset (DateTime.Now.AddSeconds (. 1), 2); 
            the DateTimeOffset endTime = DateBuilder.NextGivenSecondDate (DateTime.Now.AddHours (2),. 3); 
            // create and configure a trigger
            Trigger = ISimpleTrigger (ISimpleTrigger) TriggerBuilder.Create () StartAt (the startTime) .EndAt (endTime).                 
                                        .WithSimpleSchedule (X => x.WithIntervalInSeconds (. 3) .WithRepeatCount (100)) 
                                        .build (); 
            // Add job scheduling Pool in 
            sched.ScheduleJob (job, Trigger); 
            // start running 
            sched.Start (); 
            the Console.ReadKey (); 
        } 
    } 
    public class JobDemo: IJob 
    { 
        /// <Summary> 
        /// this is the job scheduling timing of each perform a method 
        /// </ Summary>  
        /// <param name = "context"> </ param>
        public void the execute (IJobExecutionContext context) 
        {
            Console.WriteLine(DateTime.Now.ToString("r"));
        }
    }
Copy the code
View Code

3. Continue to improve (cron-like use): The first two are too simple job scheduling, if I want to first 10,20,25,26,33,54 minutes past the hour, every minute of the first 10, 14 seconds to execute once. Then the above is clearly not satisfied. This is what I put cron-like expressions introduced with in, in order to achieve a variety of call time latitude.

Copy the code
RunMain class public 
    { 
        static void the Main (String [] args) 
        { 
            Console.WriteLine (DateTime.Now.ToString ( "R & lt")); 
            // first create a job scheduler pool 
            ISchedulerFactory schedf the StdSchedulerFactory new new = (); 
            IScheduler sched = schedf .GetScheduler (); 
            // Create out a particular job 
            IJobDetail job = JobBuilder.Create <JobDemo> () the Build ();. 
            // NextGivenSecondDate: If the first argument is null then the current time table two seconds later postponed the point in time. 
            The startTime = DateBuilder.NextGivenSecondDate the DateTimeOffset (DateTime.Now.AddSeconds (. 1), 2); 
            the DateTimeOffset endTime = DateBuilder.NextGivenSecondDate (DateTime.Now.AddYears (2),. 3); 
            // create and configure a trigger
            The Trigger = ICronTrigger (ICronTrigger) TriggerBuilder.Create (). StartAt (startTime) .EndAt (endTime) 
                                        .WithCronSchedule ( "10, 14 10,20,25,26,33,54 * * *?") 
                                        .Build ( ); 
            // Add job scheduling cell 
            sched.ScheduleJob (job, Trigger); 
            // start running 
            sched.Start (); 
            // suspend 2 days 
            the Thread.Sleep (TimeSpan.FromDays (2)); 
            // 2 days Close job scheduling, execution will not 
            sched.Shutdown (); 
            the Console.ReadKey (); 
        } 
    } 
    public class JobDemo: IJob 
    { 
        /// <Summary> 
        /// this is the method of performing the job scheduling timing of each 
        /// </ Summary> 
        /// <param name = "context"> </ param> 
        public void the Execute (IJobExecutionContext context) 
        { 
            Console.WriteLine (the DateTime.Now. the ToString ( "R & lt")); 
        } 
    }
Copy the code

Detailed job scheduling based on the project Quartz.net

As shown in the final result of the first opening in FIG.

Here mainly to talk about the job scheduler in how to target specific job scheduling and job scheduling to packet, naming, add, start, stop.

The following table shows first structure, a project I'm working schedule for the task scheduling.

1, the new job scheduling.

Copy the code
 /// <summary>
        /// 任务计划
        /// </summary>
        public static IScheduler scheduler = null;
        public static IScheduler GetScheduler()
        {
            if (scheduler != null)
            {
                return scheduler;
            }
            else
            {
                ISchedulerFactory schedf = new StdSchedulerFactory();
                IScheduler sched = schedf.GetScheduler();
                return sched;
            }
        }
 /// <summary>
        /// 添加任务计划
        /// </summary>
        /// <returns></returns>
        public bool AddScheduleJob(WJ_ScheduleEntity m)
        {
            try
            {
                if (m != null)
                {
                    if (m.StarRunTime == null)
                    {
                        m.StarRunTime = DateTime.Now;
                    }
                    DateTimeOffset starRunTime = DateBuilder.NextGivenSecondDate(m.StarRunTime, 1);
                    if (m.EndRunTime == null)
                    {
                        m.EndRunTime = DateTime.MaxValue.AddDays(-1);
                    }
                    DateTimeOffset endRunTime = DateBuilder.NextGivenSecondDate(m.EndRunTime, 1);
                    scheduler = GetScheduler();
                    IJobDetail job = JobBuilder.Create<HttpJob>()
                      .WithIdentity(m.JobName, m.JobGroup)
                      .Build();
                    ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                                                 .StartAt(starRunTime)
                                                 .EndAt(endRunTime)
                                                 .WithIdentity(m.JobName, m.JobGroup)
                                                 .WithCronSchedule(m.CronStr)
                                                 .Build();
                    scheduler.ScheduleJob(job, trigger);
                    scheduler.Start();
                    StopScheduleJob(m.JobGroup, m.JobName);
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                //DoApplication.WriteLogFile(ex.Message + "\r\n" + ex.StackTrace);
                return false;
            }
        }
Copy the code

Note:. 1 herein job scheduling function performed are as follows. 2. The above WithIdentity (m.JobName, m.JobGroup), is scheduled to be added to the work group, and name, for which a job to help us plan, start and stop.

 

Copy the code
 public class HttpJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            ThreadPool.QueueUserWorkItem(delegate(Object o)
            {
                try
                {
                    //DoApplication.WriteLogFile(context.JobDetail.Key.Group + "---" + context.JobDetail.Key.Name + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "---" + context.NextFireTimeUtc.Value.DateTime.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss"));
                    var sm = new WJ_ScheduleManage().GetScheduleModel(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name });
                    new WJ_ScheduleManage().UpdateScheduleRunStatus(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name,RunStatus=(int)ADJ.Job.Entity.EnumType.JobRunStatus.执行中 });
                    ESBRequest req = new ESBRequest(sm.ServiceCode, sm.ApiCode);
                    DataResult result = req.Request();
                    new WJ_ScheduleManage().UpdateScheduleRunStatus(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name, RunStatus = (int)ADJ.Job.Entity.EnumType.JobRunStatus.待运行 });
                    if (result.Code == 1)
                    {
                        #region 加入执行明细
                        WJ_ScheduleDetailsEntity dm = new WJ_ScheduleDetailsEntity();
                        dm.ActionDescribe = "执行完成:" + result.Message ;
                        dm.ActionStep = (int)ADJ.Job.Entity.EnumType.JobStep.执行完成;
                        dm.CreateTime = DateTime.Now;
                        dm.JobGroup = context.JobDetail.Key.Group;
                        dm.JobName = context.JobDetail.Key.Name;
                        dm.IsSuccess = 1;
                        new WJ_ScheduleManage().AddScheduleDetails(dm);
                        #endregion
                    }
                    else
                    {
                        #region 加入执行明细
                        WJ_ScheduleDetailsEntity dm = new WJ_ScheduleDetailsEntity();
                        dm.ActionDescribe = "执行任务计划中,执行计划过程出错."+result.Message;
                        dm.ActionStep = (int)ADJ.Job.Entity.EnumType.JobStep.执行任务计划中;
                        dm.CreateTime = DateTime.Now;
                        dm.JobGroup = context.JobDetail.Key.Group;
                        dm.JobName = context.JobDetail.Key.Name;
                        dm.IsSuccess = 0;
                        new WJ_ScheduleManage().AddScheduleDetails(dm);
                        #endregion
                    }
                    new WJ_ScheduleManage().UpdateScheduleNextTime(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name, NextTime = context.NextFireTimeUtc.Value.DateTime.AddHours(8) });
                }
                catch (Exception ex)
                {
                    #region 加入执行明细
                    WJ_ScheduleDetailsEntity dm = new WJ_ScheduleDetailsEntity();
                    dm.ActionDescribe = "执行任务计划中,执行计划过程出错:" + ex.Message + "/r/n" + ex.StackTrace;
                    dm.ActionStep = (int)ADJ.Job.Entity.EnumType.JobStep.执行任务计划中;
                    dm.CreateTime = DateTime.Now;
                    dm.JobGroup = context.JobDetail.Key.Group;
                    dm.JobName = context.JobDetail.Key.Name;
                    dm.IsSuccess = 0;
                    new WJ_ScheduleManage().AddScheduleDetails(dm);
                    #endregion
                    DoApplication.WriteLogFile(ex.Message + "\r\n" + ex.StackTrace);
                }
            });
        }
    }
Copy the code
View Code

note:这里执行的Execute方法参数IJobExecutionContext 中,会自动把作业调度的详细信息带过来,作业名称,作业组名,作业下次执行时间,作业执行时间等等,这里的内容也是至关重要的,比如根据作业组,作业名称我们可以从数据库找到相应的作业调度详细,更新操作数据库。

2、针对某个作业计划进行停止,启动。

Copy the code
 /// <summary>
        /// 暂停指定任务计划
        /// </summary>
        /// <returns></returns>
        public JsonResult StopScheduleJob(string jobGroup, string jobName)
        {
            try
            {
                scheduler = GetScheduler();                
                scheduler.PauseJob(new JobKey(jobName, jobGroup));
                new WJ_ScheduleManage().UpdateScheduleStatus(new WJ_ScheduleEntity() { JobName = jobName, JobGroup = jobGroup, Status = (int)ADJ.Job.Entity.EnumType.JobStatus.已停止 });               
                return Json(new StatusView() { Status = 0, Msg = "停止任务计划成功!" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {               
                DoApplication.WriteLogFile(ex.Message + "/r/n" + ex.StackTrace);
                return Json(new StatusView() { Status = -1, Msg = "停止任务将计划失败!" }, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 开启指定的任务计划
        /// </summary>
        /// <returns></returns>
        public JsonResult RunScheduleJob(string jobGroup, string jobName)
        {
            try
            {
                var sm = new WJ_ScheduleManage().GetScheduleModel(new WJ_ScheduleEntity() { JobName = jobName, JobGroup = jobGroup });
                AddScheduleJob(sm);
                sm.Status = (int)ADJ.Job.Entity.EnumType.JobStatus.已启用;
                new WJ_ScheduleManage().UpdateScheduleStatus(sm);
                scheduler = GetScheduler();
                scheduler.ResumeJob(new JobKey(jobName, jobGroup));
                return Json(new StatusView() { Status = 0, Msg = "启动成功!" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
              
                DoApplication.WriteLogFile(ex.Message + "/r/n" + ex.StackTrace);
                return Json(new StatusView() { Status = -1, Msg = "启动失败!" }, JsonRequestBehavior.AllowGet);
            }
        }
Copy the code

最后注意:

1、这个项目完全使用啦cron-like表达式实现触发器配置,如果你对cron不了解,那么我上篇中有针对cron做介绍讲解,如果你对cron了解而没有一个合适的生成工具,那么入左上方2个群,找我。

2、这个项目部署在IIS中,那么就要设置应用程序池的回收机制为,永不回收,配置下,如果不会配置,入左上方2个群,找我。

前言

quartz.net作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本。支持 cron-like表达式,集群,数据库。功能性能强大更不用说。

下载项目文档官网:http://www.quartz-scheduler.net/

项目中需引用:Common.Logging.dll , Common.Logging.Core.dll , Quartz.dll 

下面给大家分解下我最近做的关于计划调度的一个小项目,来辅助理解quartz.net的功能和常用方法。

 

quartz.net的简单用法 -入门

如果你是quartz.net的使用新手,控制台入门这里,建议跟着做下,那么10分钟搞懂quartz.net也是 so easy 的事.

1.创建一个每隔3秒钟执行一次的计划调度

Copy the code
public class RunMain
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Now.ToString("r"));
            //1.首先创建一个作业调度池
            ISchedulerFactory schedf = new StdSchedulerFactory();
            IScheduler sched = schedf.GetScheduler();
            //2.创建出来一个具体的作业
            IJobDetail job = JobBuilder.Create<JobDemo>().Build();           
            //3.创建并配置一个触发器
            ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create().WithSimpleSchedule(x=>x.WithIntervalInSeconds(3).WithRepeatCount(int.MaxValue)).Build();
            //4.加入作业调度池中
            sched.ScheduleJob(job, trigger);
            //5.开始运行
            sched.Start();
            Console.ReadKey();
        }
    }
    public class JobDemo : IJob
    {
        /// <summary>
        /// 这里是作业调度每次定时执行方法
        /// </summary>
        /// <param name="context"></param>
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine(DateTime.Now.ToString("r"));
        }
    }
Copy the code

Note:1、记下作业调度创建的顺序。2、上述代码执行结果是,每三秒执行一次JobDemo中的Execute,如果程序不停止,无休无止执行到天荒地老,呵呵,扯下蛋啊。

2.改进(丰富调度计划):上一个作业,我想让他每三秒执行一次,一共执行100次,开始执行时间设定在当前时间,结束时间我设定在2小时后,不过100次执行完没2小时候都不再执行。

Copy the code
    public class RunMain
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Now.ToString("r"));
            //首先创建一个作业调度池
            ISchedulerFactory schedf = new StdSchedulerFactory();
            IScheduler sched = schedf.GetScheduler();
            //创建出来一个具体的作业
            IJobDetail job = JobBuilder.Create<JobDemo>().Build();
            //NextGivenSecondDate:如果第一个参数为null则表名当前时间往后推迟2秒的时间点。
            DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddSeconds(1), 2);
            DateTimeOffset endTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddHours(2), 3);
            //创建并配置一个触发器
            ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create().StartAt(startTime).EndAt(endTime)                
                                        .WithSimpleSchedule(x=>x.WithIntervalInSeconds(3).WithRepeatCount(100))
                                        .Build();
            //加入作业调度池中
            sched.ScheduleJob(job, trigger);
            //开始运行
            sched.Start();
            Console.ReadKey();
        }
    }
    public class JobDemo : IJob
    {
        /// <summary>
        /// 这里是作业调度每次定时执行方法
        /// </summary>
        /// <param name="context"></param>
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine(DateTime.Now.ToString("r"));
        }
    }
Copy the code
View Code

3.继续改进(cron-like使用):前两个作业调度都太简单,如果我想在每小时的第10,20,25,26,33,54分钟,每分钟的第1,10,14秒执行一次。那么上面显然是不能满足的。这是我就把cron-like表达式引入进来,以实现各种时间纬度的调用。

Copy the code
 public class RunMain
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Now.ToString("r"));
            //首先创建一个作业调度池
            ISchedulerFactory schedf = new StdSchedulerFactory();
            IScheduler sched = schedf.GetScheduler();
            //创建出来一个具体的作业
            IJobDetail job = JobBuilder.Create<JobDemo>().Build();
            //NextGivenSecondDate:如果第一个参数为null则表名当前时间往后推迟2秒的时间点。
            DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddSeconds(1), 2);
            DateTimeOffset endTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddYears(2), 3);
            //创建并配置一个触发器
            ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create().StartAt(startTime).EndAt(endTime)
                                        .WithCronSchedule("1,10,14 10,20,25,26,33,54 * * * ? ")
                                        .Build();
            //加入作业调度池中
            sched.ScheduleJob(job, trigger);
            //开始运行
            sched.Start();
            //挂起2天
            Thread.Sleep(TimeSpan.FromDays(2));
            //2天后关闭作业调度,将不在执行
            sched.Shutdown();
            Console.ReadKey();
        }
    }
    public class JobDemo : IJob
    {
        /// <summary>
        /// 这里是作业调度每次定时执行方法
        /// </summary>
        /// <param name="context"></param>
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine(DateTime.Now.ToString("r"));
        }
    }
Copy the code

基于Quartz.net的作业调度项目详解

最终效果如开篇的第一个图所示。

下面主要说说,作业调度的中怎么定位到具体的作业调度,并给作业调度分组,命名,添加,启动,停止。

首先展示下表结构,项目中我叫作业调度为任务调度。

1、新增作业调度。

Copy the code
 /// <summary>
        /// 任务计划
        /// </summary>
        public static IScheduler scheduler = null;
        public static IScheduler GetScheduler()
        {
            if (scheduler != null)
            {
                return scheduler;
            }
            else
            {
                ISchedulerFactory schedf = new StdSchedulerFactory();
                IScheduler sched = schedf.GetScheduler();
                return sched;
            }
        }
 /// <summary>
        /// 添加任务计划
        /// </summary>
        /// <returns></returns>
        public bool AddScheduleJob(WJ_ScheduleEntity m)
        {
            try
            {
                if (m != null)
                {
                    if (m.StarRunTime == null)
                    {
                        m.StarRunTime = DateTime.Now;
                    }
                    DateTimeOffset starRunTime = DateBuilder.NextGivenSecondDate(m.StarRunTime, 1);
                    if (m.EndRunTime == null)
                    {
                        m.EndRunTime = DateTime.MaxValue.AddDays(-1);
                    }
                    DateTimeOffset endRunTime = DateBuilder.NextGivenSecondDate(m.EndRunTime, 1);
                    scheduler = GetScheduler();
                    IJobDetail job = JobBuilder.Create<HttpJob>()
                      .WithIdentity(m.JobName, m.JobGroup)
                      .Build();
                    ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                                                 .StartAt(starRunTime)
                                                 .EndAt(endRunTime)
                                                 .WithIdentity(m.JobName, m.JobGroup)
                                                 .WithCronSchedule(m.CronStr)
                                                 .Build();
                    scheduler.ScheduleJob(job, trigger);
                    scheduler.Start();
                    StopScheduleJob(m.JobGroup, m.JobName);
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                //DoApplication.WriteLogFile(ex.Message + "\r\n" + ex.StackTrace);
                return false;
            }
        }
Copy the code

Note:1.这里作业调度执行的函数如下。2.上面的WithIdentity(m.JobName, m.JobGroup) ,是给作业调度加入组,和名称,方便我们针对哪一个作业计划,进行启动停止等操作。

 

Copy the code
 public class HttpJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            ThreadPool.QueueUserWorkItem(delegate(Object o)
            {
                try
                {
                    //DoApplication.WriteLogFile(context.JobDetail.Key.Group + "---" + context.JobDetail.Key.Name + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "---" + context.NextFireTimeUtc.Value.DateTime.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss"));
                    var sm = new WJ_ScheduleManage().GetScheduleModel(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name });
                    new WJ_ScheduleManage().UpdateScheduleRunStatus(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name,RunStatus=(int)ADJ.Job.Entity.EnumType.JobRunStatus.执行中 });
                    ESBRequest req = new ESBRequest(sm.ServiceCode, sm.ApiCode);
                    DataResult result = req.Request();
                    new WJ_ScheduleManage().UpdateScheduleRunStatus(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name, RunStatus = (int)ADJ.Job.Entity.EnumType.JobRunStatus.待运行 });
                    if (result.Code == 1)
                    {
                        #region 加入执行明细
                        WJ_ScheduleDetailsEntity dm = new WJ_ScheduleDetailsEntity();
                        dm.ActionDescribe = "执行完成:" + result.Message ;
                        dm.ActionStep = (int)ADJ.Job.Entity.EnumType.JobStep.执行完成;
                        dm.CreateTime = DateTime.Now;
                        dm.JobGroup = context.JobDetail.Key.Group;
                        dm.JobName = context.JobDetail.Key.Name;
                        dm.IsSuccess = 1;
                        new WJ_ScheduleManage().AddScheduleDetails(dm);
                        #endregion
                    }
                    else
                    {
                        #region 加入执行明细
                        WJ_ScheduleDetailsEntity dm = new WJ_ScheduleDetailsEntity();
                        dm.ActionDescribe = "执行任务计划中,执行计划过程出错."+result.Message;
                        dm.ActionStep = (int)ADJ.Job.Entity.EnumType.JobStep.执行任务计划中;
                        dm.CreateTime = DateTime.Now;
                        dm.JobGroup = context.JobDetail.Key.Group;
                        dm.JobName = context.JobDetail.Key.Name;
                        dm.IsSuccess = 0;
                        new WJ_ScheduleManage().AddScheduleDetails(dm);
                        #endregion
                    }
                    new WJ_ScheduleManage().UpdateScheduleNextTime(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name, NextTime = context.NextFireTimeUtc.Value.DateTime.AddHours(8) });
                }
                catch (Exception ex)
                {
                    #region 加入执行明细
                    WJ_ScheduleDetailsEntity dm = new WJ_ScheduleDetailsEntity();
                    dm.ActionDescribe = "执行任务计划中,执行计划过程出错:" + ex.Message + "/r/n" + ex.StackTrace;
                    dm.ActionStep = (int)ADJ.Job.Entity.EnumType.JobStep.执行任务计划中;
                    dm.CreateTime = DateTime.Now;
                    dm.JobGroup = context.JobDetail.Key.Group;
                    dm.JobName = context.JobDetail.Key.Name;
                    dm.IsSuccess = 0;
                    new WJ_ScheduleManage().AddScheduleDetails(dm);
                    #endregion
                    DoApplication.WriteLogFile(ex.Message + "\r\n" + ex.StackTrace);
                }
            });
        }
    }
Copy the code
View Code

note:这里执行的Execute方法参数IJobExecutionContext 中,会自动把作业调度的详细信息带过来,作业名称,作业组名,作业下次执行时间,作业执行时间等等,这里的内容也是至关重要的,比如根据作业组,作业名称我们可以从数据库找到相应的作业调度详细,更新操作数据库。

2、针对某个作业计划进行停止,启动。

Copy the code
 /// <summary>
        /// 暂停指定任务计划
        /// </summary>
        /// <returns></returns>
        public JsonResult StopScheduleJob(string jobGroup, string jobName)
        {
            try
            {
                scheduler = GetScheduler();                
                scheduler.PauseJob(new JobKey(jobName, jobGroup));
                new WJ_ScheduleManage().UpdateScheduleStatus(new WJ_ScheduleEntity() { JobName = jobName, JobGroup = jobGroup, Status = (int)ADJ.Job.Entity.EnumType.JobStatus.已停止 });               
                return Json(new StatusView() { Status = 0, Msg = "停止任务计划成功!" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {               
                DoApplication.WriteLogFile(ex.Message + "/r/n" + ex.StackTrace);
                return Json(new StatusView() { Status = -1, Msg = "停止任务将计划失败!" }, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 开启指定的任务计划
        /// </summary>
        /// <returns></returns>
        public JsonResult RunScheduleJob(string jobGroup, string jobName)
        {
            try
            {
                var sm = new WJ_ScheduleManage().GetScheduleModel(new WJ_ScheduleEntity() { JobName = jobName, JobGroup = jobGroup });
                AddScheduleJob(sm);
                sm.Status = (int)ADJ.Job.Entity.EnumType.JobStatus.已启用;
                new WJ_ScheduleManage().UpdateScheduleStatus(sm);
                scheduler = GetScheduler();
                scheduler.ResumeJob(new JobKey(jobName, jobGroup));
                return Json(new StatusView() { Status = 0, Msg = "启动成功!" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
              
                DoApplication.WriteLogFile(ex.Message + "/r/n" + ex.StackTrace);
                return Json(new StatusView() { Status = -1, Msg = "启动失败!" }, JsonRequestBehavior.AllowGet);
            }
        }
Copy the code

最后注意:

1, this project fully use it cron-like expression implementations trigger configuration, if you do not understand the cron, then I have the papers for presentation to explain cron to do, if you know about cron without a proper generation tool, then into the top left of the two groups, looking for me.

2, the project deployed in IIS, then it would set up collection mechanism for the application pool, and never recovered, Configuration, if not configured into the top left of the two groups, looking for me.

Guess you like

Origin www.cnblogs.com/zhang1f/p/11953609.html