public class QuarztHelper { #region 字段 /// <summary> /// 调度器 /// </summary> IScheduler scheduler; /// <summary> /// 工作作业 /// </summary> IJobDetail job; /// <summary> /// 创建触发器 /// </summary> ITrigger trigger; /// 身份名称 /// </summary> string jobKeyName; /// <summary> /// 分组名称 /// </summary> string groupName; #endregion #region 构造函数 public QuarztHelper() { } #endregion #region 创建调度器,并打开任务 /// <summary> /// 创建调度器 并打开任务 /// </summary> /// <returns></returns> public async Task CreateScheduler() { this.scheduler = await new StdSchedulerFactory().GetScheduler(); await this.scheduler.Start(); } #endregion #region 创建并绑定工作 /// <summary> /// 1.创建并绑定工作 /// </summary> /// <typeparam name="T">实现Ijob的类</typeparam> /// <param name="name">身份key</param> /// <param name="groupName">分组名称</param> /// <returns></returns> public void Definejob<T>(string name, string groupName) where T : IJob { //obDataMap jobDataMap = new JobDataMap(); //jobDataMap.Put("TaskName", "Task1"); //StoreDurably 是否持久, 调用ResumeJob 重新执行 this.jobKeyName = name; this.groupName = groupName; this.job = JobBuilder.Create<T>() .WithIdentity(name, groupName)//使用具有给定名称和组的 JobKey 来标识 JobDetail。 // .UsingJobData(jobKeyName); .Build(); } #endregion #region 创建触发器 /// <summary> /// 2.创建触发器 /// </summary> /// <param name="triggerName">触发器名称</param> /// <param name="groupName">分组名称</param> /// <param name="cron">表达式</param> /// <param name="name">jobkey名称</param> public void CreateTigger(string triggerName, string cron) { // 创建触发器 触发作业立即运行,然后每秒重复一次 this.trigger = TriggerBuilder.Create() .WithIdentity(triggerName, this.groupName) .WithCronSchedule(cron) .ForJob(this.jobKeyName, this.groupName) .Build(); } #endregion #region 关闭任务 /// <summary> /// 关闭任务 /// </summary> public void CloseScheduler() { this.scheduler.Shutdown(); } #endregion #region 安排任务 /// <summary> /// 安排任务 /// </summary> /// <returns></returns> public async Task SchedulerTask() { await scheduler.ScheduleJob(job, trigger); } #endregion #region 作业监听添加到任务调度 /// <summary> /// 将作业监听其添加到任务调度中去 /// </summary> /// <typeparam name="T">实现IJobListener接口</typeparam> public void ListenJob<T>(string jobkeyName, string GroupName) where T : IJobListener, new() { IJobListener li = (IJobListener)Activator.CreateInstance(typeof(T), new object[] { jobKeyName }); try { this.scheduler.ListenerManager.AddJobListener(li, KeyMatcher<JobKey>.KeyEquals(new JobKey(jobkeyName, GroupName))); } catch (Exception ex) { throw ex; } } #endregion #region 作业监听暂停 /// <summary> /// 将作业监听暂停 /// </summary> /// <param name="jobkeyName">监听器名称</param> /// <returns></returns> public bool RemoveListen(string jobkeyName) { return this.scheduler.ListenerManager.RemoveJobListener(jobkeyName); } #endregion #region 暂停分组下所有的工作任务 /// <summary> /// 暂停分组下所有的工作任务 /// </summary> public void PauseJobs(string groupName) { this.scheduler.PauseJobs(GroupMatcher<JobKey>.GroupEquals(groupName)); } #endregion #region 暂停某个分组下某个任务 /// <summary> /// 暂停某个分组下某个任务 /// </summary> public void PauseJob(string jobKeyName, string groupName) { this.scheduler.PauseJob(new JobKey(jobKeyName, groupName)); } #endregion #region 恢复分组下所有的工作任务 /// <summary> /// 恢复分组下所有的工作任务 /// </summary> public void ResumeJobs(string groupName) { this.scheduler.ResumeJobs(GroupMatcher<JobKey>.GroupEquals(groupName)); } #endregion #region 恢复分组下某个任务 /// <summary> /// 恢复分组下某个任务 /// </summary> public void ResumeJob(string jobKeyName, string groupName) { this.scheduler.ResumeJob(new JobKey(jobKeyName, groupName)); } #endregion #region 删除分组下所有的工作任务 /// <summary> /// 删除分组下所有的工作任务 /// </summary> public Task<bool> DeleteJobs(string groupName) { var jobKeys = scheduler.GetJobKeys(GroupMatcher<JobKey>.GroupEquals(groupName)).Result; return this.scheduler.DeleteJobs(jobKeys); } #endregion #region 删除某个分组下某个任务 /// <summary> /// 删除某个分组下某个任务 /// </summary> public Task<bool> DeleteJob(string jobKeyName, string groupName) { return this.scheduler.DeleteJob(new JobKey(jobKeyName, groupName)); } #endregion #region 添加任务 /// <summary> /// 添加任务 /// </summary> /// <typeparam name="T">集成IJOB接口</typeparam> /// <param name="jobkeyName">身份(唯一值)</param> /// <param name="groupName">分组名称</param> /// <param name="trigger">"触发器名称"</param> /// <param name="cron">定时表达式</param> public async void AddJob<T>(string jobkeyName, string groupName, string trigger, string cron) where T : IJob { Definejob<T>(jobkeyName, groupName); CreateTigger(trigger, cron); await this.scheduler.AddJob(job, true, true); await this.scheduler.ScheduleJob(this.trigger); } #endregion }
TRANSLATE with x English TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back 标签:帮助,Quartz,string,endregion,region,groupName,scheduler,public From: https://www.cnblogs.com/jiaozai891/p/17098154.html