zoukankan      html  css  js  c++  java
  • Quartz.NET管理类

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

      1     public class QuartzManager<T> where T : class,IJob
      2     {
      3         #region 变量
      4         private static ISchedulerFactory schedulerFactory = new StdSchedulerFactory();  //scheduler工厂
      5         private static String JOB_GROUP_NAME = "JOBGROUP_NAME"; //Job群组名
      6         private static String TRIGGER_GROUP_NAME = "TRIGGERGROUP_NAME"; //触发器群组名
      7         #endregion
      8 
      9         #region 添加,删除,修改Job方法
     10         /// <summary>
     11         /// 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 
     12         /// </summary>
     13         /// <param name="pStrJobName">任务名</param>
     14         /// <param name="pStrCronExpress">触发器表达式</param>
     15         public static void addJob(string pStrJobName, string pStrCronExpress,IDictionary<string,object> pDictionary)
     16         {
     17             try
     18             {
     19                 IScheduler sched = schedulerFactory.GetScheduler();
     20                 // 创建任务
     21                 IJobDetail job = JobBuilder.Create<T>()
     22                     .WithIdentity(pStrJobName, JOB_GROUP_NAME)
     23                     .Build();
     24 
     25                 // 创建触发器
     26                 ITrigger trigger = TriggerBuilder.Create()
     27                     .WithIdentity(pStrJobName, TRIGGER_GROUP_NAME)
     28                     .WithCronSchedule(pStrCronExpress)
     29                     .Build();
     30 
     31                 //给任务传参数
     32                 foreach (KeyValuePair<string, object> kvp in pDictionary)
     33                 {
     34                     job.JobDataMap.Put(kvp.Key, kvp.Value);
     35                 }
     36 
     37                 sched.ScheduleJob(job, trigger);
     38             }
     39             catch (Exception e)
     40             {
     41                 throw new Exception(e.Message);
     42             }
     43         }
     44 
     45         /// <summary>
     46         /// 移除一个任务(使用默认的任务组名,触发器名,触发器组名) 
     47         /// </summary>
     48         /// <param name="pStrJobName">任务名称</param>
     49         public static void RemoveJob(string pStrJobName)
     50         {
     51             try
     52             {
     53                 IScheduler sched = schedulerFactory.GetScheduler();
     54                 JobKey jobKey = new JobKey(pStrJobName);
     55                 TriggerKey triggerKey = new TriggerKey(pStrJobName, TRIGGER_GROUP_NAME);
     56                 sched.PauseTrigger(triggerKey);// 停止触发器  
     57                 sched.UnscheduleJob(triggerKey);// 移除触发器  
     58                 sched.DeleteJob(jobKey);// 删除任务  
     59             }
     60             catch (Exception e)
     61             {
     62                 throw new Exception(e.Message);
     63             }
     64         }
     65 
     66         /// <summary>
     67         /// 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名) 
     68         /// </summary>
     69         /// <param name="pStrJobName">任务名</param>
     70         /// <param name="pStrCronExpress">触发器表达式</param>
     71         public static void ModifyJobTime(string pStrJobName, string pStrCronExpress,IDictionary<string,object> pDictionary)
     72         {
     73             try
     74             {
     75                 IScheduler sched = schedulerFactory.GetScheduler();
     76                 TriggerKey triggerKey = new TriggerKey(pStrJobName, TRIGGER_GROUP_NAME);
     77                 ICronTrigger trigger = (ICronTrigger)sched.GetTrigger(triggerKey);
     78                 if (trigger == null)
     79                 {
     80                     return;
     81                 }
     82                 RemoveJob(pStrJobName);
     83                 addJob(pStrJobName, pStrCronExpress,pDictionary);
     84             }
     85             catch (Exception e)
     86             {
     87                 throw new Exception(e.Message);
     88             }
     89         }
     90         #endregion
     91 
     92         #region 启动,关闭Job
     93         /// <summary>
     94         /// 启动所有定时任务 
     95         /// </summary>
     96         public static void startJobs()
     97         {
     98             try
     99             {
    100                 IScheduler sched = schedulerFactory.GetScheduler();
    101                 sched.Start();
    102             }
    103             catch (Exception e)
    104             {
    105                 throw new Exception(e.Message);
    106             }
    107         } 
    108 
    109         /// <summary>
    110         /// 关闭所有定时任务
    111         /// </summary>
    112         public static void ShutdownJobs()
    113         {
    114             try
    115             {
    116                 IScheduler sched = schedulerFactory.GetScheduler();
    117                 if (!sched.IsShutdown)
    118                 {
    119                     sched.Shutdown();
    120                 }
    121             }
    122             catch (Exception e)
    123             {
    124                 throw new Exception(e.Message);
    125             }
    126         }  
    127         #endregion
    128 
    129     }

    调用方法也很简单

    1 QuartzManager<MyJob>.addJob(jobnName, cronExpress, dictionary);
    2 
    3 QuartzManager<MyJob>.startJobs();
  • 相关阅读:
    Linux curl命令详解
    drools 7 日期时间段校验
    java程序员常用查询和学习的网站
    springboot+mybatis+分页插件的使用
    Vue. 之 替换 左上角 title标签处的图标
    Github访问速度很慢的原因,以及解决方法
    vue插件总结——总有你能用上的插件
    在Java中如何高效的判断数组中是否包含某个元素
    解决vue刷新页面以后丢失store的数据
    vue路由跳转时组件data数据刷新
  • 原文地址:https://www.cnblogs.com/wumian1360/p/4079751.html
Copyright © 2011-2022 走看看