定时执行任务

首先  Install-Package Quartz;

using System;
using Quartz;
using System.Collections.Specialized;
using Quartz.Impl;
using Common.Logging;

namespace LisWms.Sch
{
    public class Scheduler
    {
        private static Scheduler instance;

        private static Object locker = new Object();

        public static IScheduler scheduler;

        /// <summary>
        /// 创建Scheduler
        /// </summary>
        /// <returns></returns>
        public static Scheduler GetInstance()
        {
            lock (locker)
            {
                if (instance == null)
                {
                    instance = new Scheduler();
                }
            }
            return instance;
        }

        private Scheduler()
        {
            //工厂方式创建 IScheduler接口
            ISchedulerFactory sf = new StdSchedulerFactory();
            scheduler = sf.GetScheduler();
        }

        public static void Start()
        {
            scheduler.Start();
        }
        /// <summary>
        /// 
        /// </summary>
        public static void Stop()
        {
            scheduler.Shutdown(true);
            instance = null;
        }
        /// <summary>
        /// 暂停所有动做
        /// </summary>
        public static void Pause()
        {
            scheduler.PauseAll();
        }
        /// <summary>
        /// 恢复所有动做
        /// </summary>
        public static void Resume()
        {
            scheduler.ResumeAll();
        }
        /// <summary>
        /// 添加多个job任务
        /// </summary>
        /// <param name="jobDetail"></param>
        /// <param name="trigger"></param>
        public static void AddJob(IJobDetail jobDetail, ITrigger trigger)
        {
            // 当Trigger认为应该触发的时候就会调用(实际上是Scheduler调用)job.execute方法了。    
       
            scheduler.ScheduleJob(jobDetail, trigger);
          
        }
        /// <summary>
        /// 删除相关的job任务
        /// </summary>
        /// <param name="jobKey"></param>
        /// <returns></returns>
        public static bool DeleteJob(JobKey jobKey)
        {
            return scheduler.DeleteJob(jobKey);
        }

    }
}
View Code

如上 Scheduler 类

设置 定时调度任务

 //初始化

            Scheduler.GetInstance();
          
            #region AppUserJob
            //==========AppUserJob===========

            IJobDetail AppUserJob = JobBuilder.Create<AppUserJob>()  //创建一个作业
                .WithIdentity("AppUserJob", "lisWmsSyn")
                .Build();

            ITrigger AppUserJobTri = TriggerBuilder.Create()
                                        .WithIdentity("AppUserJobTri", "lisWmsSyn")
                                        .StartNow()
                                        .WithSimpleSchedule(x => x         //触发时间,60秒一次。
                                            .WithIntervalInSeconds(60)
                                            .WithMisfireHandlingInstructionFireNow()
                                            .RepeatForever())              //不间断重复执行
                                        .Build();
            //帮助类
            HelpEvent help = new HelpEvent();
            help.SetMsgEvent -= new HelpEvent.SetMsg(AddMsg);
            help.SetMsgEvent += new HelpEvent.SetMsg(AddMsg);
            Scheduler.scheduler.Context.Put("HelpEvent", help);



            Scheduler.AddJob(AppUserJob, AppUserJobTri);      //把作业,触发器加入调度器。
            #endregion
View Code

所有的任务类都要继承 IJob接口 不要对 Execute重写

posted @ 2015-08-09 15:51  蚂蚁金服官方博客  阅读(316)  评论(0编辑  收藏  举报