using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Configuration;
using Quartz.Impl.Matchers;
using Quartz.Collection;
namespace HXSoft.HXBase.Business.Utils
{
public class
StdSchedulerManager
{
///
/// 用来保存调度框架属性集合对象
///
static NameValueCollection properties = new
NameValueCollection();
static StdSchedulerFactory schedulerFactory {
get; set; }
static IScheduler scheduler { get; set; }
///
/// 初始化调度框架属性
///
static StdSchedulerManager()
{
//存储类型
properties["quartz.jobStore.type"] =
"Quartz.Impl.AdoJobStore.JobStoreTX,Quartz";
//表明前缀
properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
//驱动类型
properties["quartz.jobStore.driverDelegateType"] =
"Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz";
//线程数目
properties["quartz.threadPool.threadCount"] =
ConfigurationManager.AppSettings["threadCount"];
//数据源名称
string
DBName = ConfigurationManager.AppSettings["dataSource"];
//写入数据名称
properties["quartz.jobStore.dataSource"] = DBName;
//拼接连接属性名称
string
conPropertieName =
string.Format("quartz.dataSource.{0}.connectionString",
DBName);
//连接字符串
properties[conPropertieName] =
ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
//拼接驱动属性名称
string
proPropertieName = string.Format("quartz.dataSource.{0}.provider",
DBName);
//sqlserver版本
properties[proPropertieName] = "SqlServer-20";
properties["quartz.jobStore.clustered"] = "true";
properties["quartz.scheduler.instanceId"] = "AUTO";
schedulerFactory = new StdSchedulerFactory(properties);
scheduler
= schedulerFactory.GetScheduler();
}
///
/// 初始化配置参数
///
///
public static void
Initialize(NameValueCollection props)
{
schedulerFactory.Initialize(props);
}
///
/// 调用开启方法
///
public static void Start()
{
try
{
scheduler.Start();
}
catch
(Exception)
{
throw new
Exception("确定配置的参数是否有错误");
}
}
///
/// 向调度添加作业和触发器
///
/// 作业详情
/// 触发器
public static DateTimeOffset
ScheduleJob(IJobDetail jobDetail, ITrigger trigger)
{
return
scheduler.ScheduleJob(jobDetail, trigger);
}
///
/// 停止调度
///
public static void Shutdown()
{
scheduler.Shutdown();
}
///
/// 备用
///
public static void Standby()
{
scheduler.Standby();
}
///
/// 暂停所有
///
public static void PauseAll()
{
scheduler.PauseAll();
}
///
/// 移除所有
///
public void ResumeAll()
{
scheduler.ResumeAll();
}
///
/// 根据组名移除作业
///
/// 组名
public void ResumeJobGroup(string
groupName)
{
scheduler.ResumeJobs(GroupMatcher.GroupEquals(groupName));
}
///
/// 根据组名移除触发器
///
/// 组名
public void ResumeTriggerGroup(string
groupName)
{
scheduler.ResumeTriggers(GroupMatcher.GroupEquals(groupName));
}
///
/// 根据组名暂停作业
///
/// 组名
public void PauseJobGroup(string
groupName)
{
scheduler.PauseJobs(GroupMatcher.GroupEquals(groupName));
}
///
/// 根据组名暂停触发器
///
/// 组名
public void PauseTriggerGroup(string
groupName)
{
scheduler.PauseTriggers(GroupMatcher.GroupEquals(groupName));
}
///
/// 移除全局作业监听
///
/// 监听名称
public void RemoveGlobalJobListener(string
name)
{
scheduler.ListenerManager.RemoveJobListener(name);
}
///
/// 移除全局触发器监听
///
/// 监听名称
public void RemoveGlobalTriggerListener(string
name)
{
scheduler.ListenerManager.RemoveTriggerListener(name);
}
///
/// 通过名称分组删除作业
///
/// 作业名
/// 分组名
public static bool DeleteJob(string jobName,
string groupName)
{
return
scheduler.DeleteJob(new JobKey(jobName, groupName));
}
///
/// 通过名称分组暂停作业
///
/// 作业名
/// 分组名
public void PauseJob(string jobName, string
groupName)
{
scheduler.PauseJob(new JobKey(jobName, groupName));
}
public void ResumeJob(string jobName, string
groupName)
{
scheduler.ResumeJob(new JobKey(jobName, groupName));
}
//把作业与触发器添加到调度里去
public void TriggerJob(string jobName, string
groupName)
{
scheduler.TriggerJob(new JobKey(jobName, groupName));
}
public void Interrupt(string jobName, string
groupName)
{
scheduler.Interrupt(new JobKey(jobName, groupName));
}
public void ResumeTrigger(string triggerName,
string groupName)
{
scheduler.ResumeTrigger(new TriggerKey(triggerName,
groupName));
}
public void PauseTrigger(string triggerName,
string groupName)
{
scheduler.PauseTrigger(new TriggerKey(triggerName,
groupName));
}
public void UnscheduleJob(string triggerName,
string groupName)
{
scheduler.UnscheduleJob(new TriggerKey(triggerName,
groupName));
}
///
/// 判断某组作业是否被暂停
///
/// 组名
///
public bool? IsJobGroupPaused(string
groupName)
{
try
{
return
scheduler.IsJobGroupPaused(groupName);
}
catch
(NotImplementedException)
{
return null;
}
}
///
/// 判断某组触发器是否暂停
///
///
///
public bool? IsTriggerGroupPaused(string
groupName)
{
try
{
return
scheduler.IsTriggerGroupPaused(groupName);
}
catch
(NotImplementedException)
{
return null;
}
}
///
/// 返回运行作业集合
///
///
public IEnumerable
GetCurrentlyExecutingJobs()
{
return
scheduler.GetCurrentlyExecutingJobs();
}
///
/// 获取所有作业键集合
///
///
///
public Quartz.Collection.ISet
GetJobKeys(GroupMatcher key)
{
return
scheduler.GetJobKeys(key);
}
///
/// 获取某个作业
///
///
///
public IJobDetail GetJobDetail(JobKey key)
{
return
scheduler.GetJobDetail(key);
}
///
/// 获取触发器组名
///
///
public IEnumerable GetTriggerGroupNames()
{
return
scheduler.GetTriggerGroupNames();
}
///
/// 获取工作名
///
///
public IEnumerable GetJobGroupNames()
{
return
scheduler.GetJobGroupNames();
}
public IEnumerable GetCalendarNames()
{
return
scheduler.GetCalendarNames();
}
public IListenerManager ListenerManager
{
get
{
return
scheduler.ListenerManager;
}
}
public ICalendar GetCalendar(string name)
{
return
scheduler.GetCalendar(name);
}
public SchedulerMetaData GetMetaData()
{
return
scheduler.GetMetaData();
}
public IEnumerable GetTriggersOfJob(JobKey
jobKey)
{
try
{
return
scheduler.GetTriggersOfJob(jobKey);
}
catch
(NotImplementedException)
{
return null;
}
}
public Quartz.Collection.ISet
GetTriggerKeys(GroupMatcher matcher)
{
try
{
return
scheduler.GetTriggerKeys(matcher);
}
catch
(NotImplementedException)
{
return null;
}
}
public ITrigger GetTrigger(TriggerKey
triggerKey)
{
return
scheduler.GetTrigger(triggerKey);
}
public TriggerState GetTriggerState(TriggerKey
triggerKey)
{
return
scheduler.GetTriggerState(triggerKey);
}
public string SchedulerName
{
get
{
return
scheduler.SchedulerName;
}
}
public bool InStandbyMode
{
get
{
return
scheduler.InStandbyMode;
}
}
}
}
加载中,请稍候......