1. 程式人生 > >Quartz定時任務幫助類

Quartz定時任務幫助類

itoo 是否 att ESS sage {0} RKE obb esp

using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CRM.ThirdAPITools.QuartzHelper
{
/// <summary>
/// Quartz定時任務幫助類: Quartz表達式設計器:http://cron.qqe2.com/
/// </summary>

public class QuartzHelper
{
// Fields
private static object oLock = new object();
private static Dictionary<string, QuartzKey> quartzCache = new Dictionary<string, QuartzKey>();
private const string QuartzHelperScheulerName = "QuartzHelperScheulerName";
private static IScheduler sched = null;
private static ISchedulerFactory sf = null;
// Methods
static QuartzHelper()
{
NameValueCollection props = new NameValueCollection();
props.Add("quartz.scheduler.instanceName", "QuartzHelperScheulerName");
sf = new StdSchedulerFactory(props);
sched = sf.GetScheduler();
}
/// <summary>
/// 關閉
/// </summary>
public static void Close()
{
GetScheduler().Shutdown(true);
}
/// <summary>
/// 刪除
/// </summary>
/// <param name="jobKey"></param>
public static void Close(object jobKey)
{
if (jobKey is JobKey)
{
GetScheduler().DeleteJob(jobKey as JobKey);
}
}
/// <summary>
/// 指定日期執行
/// </summary>
/// <param name="action">調用的方法</param>
/// <param name="date">指定日期</param>
/// <param name="dataMap">傳輸數據映射</param>
/// <param name="jobName">任務名稱</param>
/// <returns></returns>
public static QuartzKey ExecuteAtDate(Action<Dictionary<string, object>> action, DateTime date, Dictionary<string, object> dataMap = null, string jobName = null)
{
return Start(action, delegate (TriggerBuilder p)
{
p.WithCronSchedule(BuilderCronExpression(date));
}, dataMap, jobName);
}
/// <summary>
/// 按表達式執行:
/// Quartz表達式設計器:http://cron.qqe2.com/
/// </summary>
/// <param name="action">調用的方法</param>
/// <param name="cronExpression">表達式</param>
/// <param name="dataMap">傳輸數據映射</param>
/// <param name="jobName">任務名稱</param>
/// <returns></returns>
public static QuartzKey ExecuteAtTime(Action<Dictionary<string, object>> action, string cronExpression, Dictionary<string, object> dataMap = null, string jobName = null)
{
return Start(action, delegate (TriggerBuilder p)
{
p.WithCronSchedule(cronExpression);
}, dataMap, jobName);
}
/// <summary>
/// 間隔執行
/// </summary>
/// <param name="action">調用的方法</param>
/// <param name="interval">時間間隔</param>
/// <param name="dataMap">傳輸數據映射</param>
public static void ExecuteInterval(Action<Dictionary<string, object>> action, TimeSpan interval, Dictionary<string, object> dataMap = null)
{
Action<TriggerBuilder> action2 = null;
lock (oLock)
{
if (action2 == null)
{
action2 = p => p.WithSimpleSchedule(p1 => p1.WithInterval(interval));
}
Start(action, action2, dataMap, null);
}
}
/// <summary>
/// 獲取調度名稱
/// </summary>
/// <returns></returns>
public static IScheduler GetScheduler()
{
ISchedulerFactory factory = new StdSchedulerFactory();
return factory.GetScheduler("QuartzHelperScheulerName");
}
/// <summary>
/// 任務調度是否開始
/// </summary>
/// <returns></returns>
public static bool IsStart()
{
return ((GetScheduler() != null) && GetScheduler().IsStarted);
}
/// <summary>
/// DateTime轉為Quartz表達式
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static string BuilderCronExpression(DateTime date)
{
string cron = string.Empty;
cron = string.Format("{0} {1} {2} {3} {4} ?", date.Second, date.Minute, date.Hour, date.Day, date.Month);
return cron;
}
/// <summary>
/// 啟用調度任務
/// </summary>
/// <param name="action">方法</param>
/// <param name="action2">構建的出發實例</param>
/// <param name="dataMap">傳輸數據映射</param>
/// <param name="jobName">任務名稱</param>
/// <returns></returns>
private static QuartzKey Start(Action<Dictionary<string, object>> action, Action<TriggerBuilder> action2, Dictionary<string, object> dataMap, string jobName)
{
QuartzKey key = new QuartzKey();
if (jobName != null)
{
if (quartzCache.ContainsKey(jobName))
{
key = quartzCache[jobName];
}
else
{
quartzCache.Add(jobName, key);
}
}
jobName = jobName ?? Guid.NewGuid().ToString("D");
string group = "group_" + jobName;
string name = "trigger_" + jobName;
IJobDetail jobDetail = JobBuilder.Create(typeof(QuartzJob)).WithIdentity(jobName, group).Build();
TriggerBuilder builder = TriggerBuilder.Create().WithIdentity(name, group);
action2(builder);
ITrigger trigger = builder.Build();
if (quartzCache.ContainsKey(jobName))
{
quartzCache[jobName].JobKey = jobDetail.Key;
quartzCache[jobName].TriggerKey = trigger.Key;
quartzCache[jobName].Logs.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 調度任務已經啟動。");
}
jobDetail.JobDataMap.Add("dataMap", dataMap);
jobDetail.JobDataMap.Add("action", action);
jobDetail.JobDataMap.Add("jobName", jobName);
jobDetail.JobDataMap.Add("quartzCache", quartzCache);
sched.ScheduleJob(jobDetail, new Quartz.Collection.HashSet<ITrigger> { trigger }, true);
sched.Start();
return key;
}
}
public class QuartzJob : IJob
{
// Methods
public void Execute(IJobExecutionContext context)
{
Dictionary<string, object> dictionary = context.JobDetail.JobDataMap["dataMap"] as Dictionary<string, object>;
string key = context.JobDetail.JobDataMap["jobName"] as string;
Dictionary<string, QuartzKey> dictionary2 = context.JobDetail.JobDataMap["quartzCache"] as Dictionary<string, QuartzKey>;
try
{
(context.JobDetail.JobDataMap["action"] as Action<Dictionary<string, object>>)(dictionary);
if (dictionary2.ContainsKey(key))
{
dictionary2[key].Logs.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 任務執行成功。");
}
}
catch (Exception exception)
{
if (dictionary2.ContainsKey(key))
{
dictionary2[key].Logs.Add(exception.Message);
}
}
}
}
public class QuartzKey
{
// Methods
public QuartzKey()
{
this.Logs = new List<string>();
}

// Properties
public JobKey JobKey { get; set; }

public List<string> Logs { get; set; }

public TriggerKey TriggerKey { get; set; }
}
}

Quartz定時任務幫助類