(转)Quartz.NET管理类

最近做项目设计到Quartz.NET,写了一个Quartz.NET管理类,在此记录下。

    public class QuartzManager<T> where T : class,IJob
    {
        #region 变量
       private static ISchedulerFactory schedulerFactory = new StdSchedulerFactory();  //scheduler工厂
        private static String JOB_GROUP_NAME = "JOBGROUP_NAME"; //Job群组名
       private static String TRIGGER_GROUP_NAME = "TRIGGERGROUP_NAME"; //触发器群组名
        #endregion
 
       #region 添加,删除,修改Job方法
      /// <summary>
        /// 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 
        /// </summary>
         /// <param name="pStrJobName">任务名</param>
        /// <param name="pStrCronExpress">触发器表达式</param>
         public static void addJob(string pStrJobName, string pStrCronExpress,IDictionary<string,object> pDictionary)
        {
            try
            {
                IScheduler sched = schedulerFactory.GetScheduler();
               // 创建任务
                IJobDetail job = JobBuilder.Create<T>()
                    .WithIdentity(pStrJobName, JOB_GROUP_NAME)
                     .Build();
 
                 // 创建触发器
                ITrigger trigger = TriggerBuilder.Create()
                     .WithIdentity(pStrJobName, TRIGGER_GROUP_NAME)
                    .WithCronSchedule(pStrCronExpress)
                    .Build();

                //给任务传参数
                foreach (KeyValuePair<string, object> kvp in pDictionary)
                {
                    job.JobDataMap.Put(kvp.Key, kvp.Value);
                }
                sched.ScheduleJob(job, trigger);
            }
            catch (Exception e)
             {
                 throw new Exception(e.Message);
             }
         }

         /// <summary>
        /// 移除一个任务(使用默认的任务组名,触发器名,触发器组名) 
         /// </summary>
         /// <param name="pStrJobName">任务名称</param>
         public static void RemoveJob(string pStrJobName)
         {
             try
             {
                 IScheduler sched = schedulerFactory.GetScheduler();
                 JobKey jobKey = new JobKey(pStrJobName);
                 TriggerKey triggerKey = new TriggerKey(pStrJobName, TRIGGER_GROUP_NAME);
                 sched.PauseTrigger(triggerKey);// 停止触发器  
                  sched.UnscheduleJob(triggerKey);// 移除触发器  
                 sched.DeleteJob(jobKey);// 删除任务  
            }
             catch (Exception e)
             {
                 throw new Exception(e.Message);
             }
         }
 
         /// <summary>
         /// 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名) 
          /// </summary>
         /// <param name="pStrJobName">任务名</param>
         /// <param name="pStrCronExpress">触发器表达式</param>
         public static void ModifyJobTime(string pStrJobName, string pStrCronExpress,IDictionary<string,object> pDictionary)
         {
             try
             {
                  IScheduler sched = schedulerFactory.GetScheduler();
                 TriggerKey triggerKey = new TriggerKey(pStrJobName, TRIGGER_GROUP_NAME);
                 ICronTrigger trigger = (ICronTrigger)sched.GetTrigger(triggerKey);
                 if (trigger == null)
                 {
                    return;
                 }
                 RemoveJob(pStrJobName);
                 addJob(pStrJobName, pStrCronExpress,pDictionary);
             }
            catch (Exception e)
             {
                  throw new Exception(e.Message);
             }
          }
         #endregion
 
         #region 启动,关闭Job
          /// <summary>
         /// 启动所有定时任务 
         /// </summary>
         public static void startJobs()
        {
            try
             {
                 IScheduler sched = schedulerFactory.GetScheduler();
                sched.Start();
             }
             catch (Exception e)
            {
                throw new Exception(e.Message);
            }
         } 
 
        /// <summary>
         /// 关闭所有定时任务
         /// </summary>
         public static void ShutdownJobs()
         {
             try
             {
                IScheduler sched = schedulerFactory.GetScheduler();
                if (!sched.IsShutdown)
                 {
                     sched.Shutdown();
                }
             }
             catch (Exception e)
             {
                 throw new Exception(e.Message);
            }
        }  
         #endregion
 
     }

 

调用方法也很简单

1 QuartzManager<MyJob>.addJob(jobnName, cronExpress, dictionary);
2 
3 QuartzManager<MyJob>.startJobs();
posted @ 2014-12-29 15:43  海上浪子  阅读(364)  评论(6编辑  收藏  举报