博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Quartz定时任务帮助类
阅读量:4662 次
发布时间:2019-06-09

本文共 6073 字,大约阅读时间需要 20 分钟。

1 using Quartz;  2 using Quartz.Impl;  3 using System;  4 using System.Collections.Generic;  5 using System.Collections.Specialized;  6 using System.Linq;  7 using System.Text;  8 using System.Threading.Tasks;  9  10 namespace CRM.ThirdAPITools.QuartzHelper 11 { 12     ///  13     /// Quartz定时任务帮助类: Quartz表达式设计器:http://cron.qqe2.com/ 14     ///  15     public class QuartzHelper 16     { 17         // Fields 18         private static object oLock = new object(); 19         private static Dictionary
quartzCache = new Dictionary
(); 20 private const string QuartzHelperScheulerName = "QuartzHelperScheulerName"; 21 private static IScheduler sched = null; 22 private static ISchedulerFactory sf = null; 23 // Methods 24 static QuartzHelper() 25 { 26 NameValueCollection props = new NameValueCollection(); 27 props.Add("quartz.scheduler.instanceName", "QuartzHelperScheulerName"); 28 sf = new StdSchedulerFactory(props); 29 sched = sf.GetScheduler(); 30 } 31 ///
32 /// 关闭 33 /// 34 public static void Close() 35 { 36 GetScheduler().Shutdown(true); 37 } 38 ///
39 /// 删除 40 /// 41 ///
42 public static void Close(object jobKey) 43 { 44 if (jobKey is JobKey) 45 { 46 GetScheduler().DeleteJob(jobKey as JobKey); 47 } 48 } 49 ///
50 /// 指定日期执行 51 /// 52 ///
调用的方法 53 ///
指定日期 54 ///
传输数据映射 55 ///
任务名称 56 ///
57 public static QuartzKey ExecuteAtDate(Action
> action, DateTime date, Dictionary
dataMap = null, string jobName = null) 58 { 59 return Start(action, delegate (TriggerBuilder p) 60 { 61 p.WithCronSchedule(BuilderCronExpression(date)); 62 }, dataMap, jobName); 63 } 64 ///
65 /// 按表达式执行: 66 /// Quartz表达式设计器:http://cron.qqe2.com/ 67 /// 68 ///
调用的方法 69 ///
表达式 70 ///
传输数据映射 71 ///
任务名称 72 ///
73 public static QuartzKey ExecuteAtTime(Action
> action, string cronExpression, Dictionary
dataMap = null, string jobName = null) 74 { 75 return Start(action, delegate (TriggerBuilder p) 76 { 77 p.WithCronSchedule(cronExpression); 78 }, dataMap, jobName); 79 } 80 ///
81 /// 间隔执行 82 /// 83 ///
调用的方法 84 ///
时间间隔 85 ///
传输数据映射 86 public static void ExecuteInterval(Action
> action, TimeSpan interval, Dictionary
dataMap = null) 87 { 88 Action
action2 = null; 89 lock (oLock) 90 { 91 if (action2 == null) 92 { 93 action2 = p => p.WithSimpleSchedule(p1 => p1.WithInterval(interval)); 94 } 95 Start(action, action2, dataMap, null); 96 } 97 } 98 ///
99 /// 获取调度名称100 /// 101 ///
102 public static IScheduler GetScheduler()103 {104 ISchedulerFactory factory = new StdSchedulerFactory();105 return factory.GetScheduler("QuartzHelperScheulerName");106 }107 ///
108 /// 任务调度是否开始109 /// 110 ///
111 public static bool IsStart()112 {113 return ((GetScheduler() != null) && GetScheduler().IsStarted);114 }115 ///
116 /// DateTime转为Quartz表达式117 /// 118 ///
119 ///
120 public static string BuilderCronExpression(DateTime date)121 {122 string cron = string.Empty;123 cron = string.Format("{0} {1} {2} {3} {4} ?", date.Second, date.Minute, date.Hour, date.Day, date.Month);124 return cron;125 }126 ///
127 /// 启用调度任务128 /// 129 ///
方法130 ///
构建的出发实例131 ///
传输数据映射132 ///
任务名称133 ///
134 private static QuartzKey Start(Action
> action, Action
action2, Dictionary
dataMap, string jobName)135 {136 QuartzKey key = new QuartzKey();137 if (jobName != null)138 {139 if (quartzCache.ContainsKey(jobName))140 {141 key = quartzCache[jobName];142 }143 else144 {145 quartzCache.Add(jobName, key);146 }147 }148 jobName = jobName ?? Guid.NewGuid().ToString("D");149 string group = "group_" + jobName;150 string name = "trigger_" + jobName;151 IJobDetail jobDetail = JobBuilder.Create(typeof(QuartzJob)).WithIdentity(jobName, group).Build();152 TriggerBuilder builder = TriggerBuilder.Create().WithIdentity(name, group);153 action2(builder);154 ITrigger trigger = builder.Build();155 if (quartzCache.ContainsKey(jobName))156 {157 quartzCache[jobName].JobKey = jobDetail.Key;158 quartzCache[jobName].TriggerKey = trigger.Key;159 quartzCache[jobName].Logs.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 调度任务已经启动。");160 }161 jobDetail.JobDataMap.Add("dataMap", dataMap);162 jobDetail.JobDataMap.Add("action", action);163 jobDetail.JobDataMap.Add("jobName", jobName);164 jobDetail.JobDataMap.Add("quartzCache", quartzCache);165 sched.ScheduleJob(jobDetail, new Quartz.Collection.HashSet
{ trigger }, true);166 sched.Start();167 return key;168 }169 }170 public class QuartzJob : IJob171 {172 // Methods173 public void Execute(IJobExecutionContext context)174 {175 Dictionary
dictionary = context.JobDetail.JobDataMap["dataMap"] as Dictionary
;176 string key = context.JobDetail.JobDataMap["jobName"] as string;177 Dictionary
dictionary2 = context.JobDetail.JobDataMap["quartzCache"] as Dictionary
;178 try179 {180 (context.JobDetail.JobDataMap["action"] as Action
>)(dictionary);181 if (dictionary2.ContainsKey(key))182 {183 dictionary2[key].Logs.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 任务执行成功。");184 }185 }186 catch (Exception exception)187 {188 if (dictionary2.ContainsKey(key))189 {190 dictionary2[key].Logs.Add(exception.Message);191 }192 }193 }194 }195 public class QuartzKey196 {197 // Methods198 public QuartzKey()199 {200 this.Logs = new List
();201 }202 203 // Properties204 public JobKey JobKey { get; set; }205 206 public List
Logs { get; set; }207 208 public TriggerKey TriggerKey { get; set; }209 }210 }

 

转载于:https://www.cnblogs.com/Andy-Blog/p/10114942.html

你可能感兴趣的文章
mustache语法
查看>>
[Python] 怎么把HTML的报告转换为图片,利用无头浏览器
查看>>
WebGL之通过外部传入a_PontSize值改变点着色器vshader内置变量gl_PointSize的值
查看>>
怎么样调整FreeBSD时区问题
查看>>
Linux CentOS 7的图形界面安装(GNOME、KDE等)
查看>>
大理石
查看>>
python执行外部程序的常用方法小结
查看>>
微信二维码生成
查看>>
linux中 ll 和ls 区别
查看>>
有关js中能否使用equals来判断相等的问题
查看>>
(十八)多线程
查看>>
bzoj4580: [Usaco2016 Open]248
查看>>
HTML5 VS. Flash&Flex? – 浅谈Flash/Flex/HTML 5技术选型
查看>>
响应者链条
查看>>
基于定位的社交应用Foursquare开源网址(wp7)
查看>>
机电传动控制读书笔记二(书本内容)
查看>>
Address already in use: JVM_Bind<null>:8080错误的解决办法
查看>>
Vue子组件监听事件中传递参数的方法
查看>>
面向对象的几种方法详解(后)
查看>>
年龄问题
查看>>