一:業務需求:html
項目須要在不一樣時刻,執行一個或不少個不一樣的做業。git
Windows執行計劃這時並不能很好的知足需求了,迫切須要一個更爲強大,方便管理,集羣部署的做業調度框架。github
二:介紹web
Quartz一個開源的做業調度框架,OpenSymphony的開源項目。Quartz.Net 是Quartz的C#移植版本。sql
特性:數據庫
1:支持集羣,做業分組,做業遠程管理。 框架
2:自定義精細的時間觸發器,使用簡單,做業和觸發分離。tcp
3:數據庫支持,能夠寄宿Windows服務,WebSite,winform等。ide
三:使用的方法:學習
1.名詞的解釋:
Scheduler 做業調度器。
IJob 做業接口,繼承並實現Execute, 編寫執行的具體做業邏輯。
JobBuilder 根據設置,生成一個詳細做業信息(JobDetail)。
TriggerBuilder 根據規則,生產對應的Trigger
2.學習的官網
Quartz.Net官方2.X教程 http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/index.html
Quartz.Net開源地址 https://github.com/quartznet/quartznet
3.Nuget安裝
PM> Install-Package Quartz
4.代碼解釋:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Quartz; using Quartz.Impl; using System.Collections.Specialized; namespace Portal.JobScheduler { public class QuartzManager<T> where T:class,IJob { //調度程序的工廠的接口中 實例一個具體的調度方法 private static ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); //JOb羣組名 private static string JOB_GROUP_NAME = "JOBGROUP_NAME"; //觸發器羣 private static string TRIGGER_GROUP_NAME = "TRIGGERGROUP_NAME"; /// <summary> /// 添加一個定時任務,使用默認的任務組名,觸發器名,觸發器組名 /// </summary> /// <param name="pStrJobName">任務名</param> /// <param name="pStrCronExpress">觸發器表達式</param> public static void AddJob(string pStrJobName, string pStrCronExpress) { try { //接口中獲取調度工廠的 GetScheduler() 方法 IScheduler sched = schedulerFactory.GetScheduler(); //建立任務 IJobDetail job = JobBuilder.Create<T>().WithIdentity(pStrJobName, JOB_GROUP_NAME).Build(); //建立觸發器 ITrigger trigger = TriggerBuilder.Create() .WithIdentity(pStrJobName, TRIGGER_GROUP_NAME) .WithCronSchedule(pStrCronExpress) .Build(); sched.ScheduleJob(job, trigger); sched.Start(); } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 移除一個任務(使用默認的任務組名,觸發器名,觸發器組名) /// </summary> /// <param name="pStrJobName"></param> public static void RemoveJob(string pStrJobName) { try { IScheduler sched = schedulerFactory.GetScheduler(); JobKey jobkey = new JobKey(pStrJobName); TriggerKey triggerKey = new TriggerKey(pStrJobName, TRIGGER_GROUP_NAME); //中止觸發器 sched.PauseTrigger(triggerKey); //移除觸發器 sched.UnscheduleJob(triggerKey); //刪除任務 sched.DeleteJob(jobkey); } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 修改一個任務的觸發時間(使用默認的任務組名,觸發器名,觸發器組名) /// </summary> /// <param name="pStrJobName">任務名</param> /// <param name="pStrCronExpress">觸發器表達式</param> public static void ModifyJobTime(string pStrJobName, string pStrCronExpress, IDictionary<string, object> pDictionary) { try { IScheduler sched = schedulerFactory.GetScheduler(); TriggerKey triggerKey = new TriggerKey(pStrJobName, TRIGGER_GROUP_NAME); ICronTrigger trigger = (ICronTrigger)sched.GetTrigger(triggerKey); if (trigger == null) { return; } RemoveJob(pStrJobName); AddJob(pStrJobName, pStrCronExpress); } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 開啓全部定時任務 /// </summary> public static void startJobs() { try { IScheduler sched = schedulerFactory.GetScheduler(); sched.Start(); } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 關閉全部的定時任務 /// </summary> public static void shutdownJobs() { try { IScheduler sched = schedulerFactory.GetScheduler(); if (!sched.IsShutdown) { sched.Shutdown(); } } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 恢復全部的任務 /// </summary> public static void ResumeAllJobs() { try { IScheduler sched = schedulerFactory.GetScheduler(); if (!sched.IsShutdown) { sched.ResumeAll(); } } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 暫停全部的做業 /// </summary> public static void PauseAllJobs() { try { IScheduler sched = schedulerFactory.GetScheduler(); sched.PauseAll(); } catch (Exception e) { throw new Exception(e.Message); } } } }
Asp。net調用:
在global文件中進行配置
private void RegisterJob() { QuartzManager<SHiddenDangerCaution>.AddJob("SHiddenDangerCautionInfo", "0 0 08 ? * *"); }
調度類的寫法:
public class SyncWeatherInfoJob:IJob { public void Execute(IJobExecutionContext context) { UpdateWeatherForecast(); } /// <summary> /// 更新氣象實況信息 /// </summary> public void UpdateWeatherForecast() { WeatherForecast.InsertWeatherInfo(); --這裏的代碼就不貼出來 } }
這樣就完成了調度任務
CrystalQuartz 遠程管理
CrystalQuartz開源的地址 https://github.com/guryanovev/CrystalQuartz
但若是想方便的知道某個做業執行狀況,須要暫停,啓動等操做行爲,這時候就須要個Job管理的界面。
接下來介紹Quartz.NET如何進行遠程job管理,如圖:
代碼:
一:做業服務端
static void Main(string[] args) { var properties = new NameValueCollection(); properties["quartz.scheduler.instanceName"] = "RemoteServerSchedulerClient"; // 設置線程池 properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"; properties["quartz.threadPool.threadCount"] = "5"; properties["quartz.threadPool.threadPriority"] = "Normal"; // 遠程輸出配置 properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz"; properties["quartz.scheduler.exporter.port"] = "556"; properties["quartz.scheduler.exporter.bindName"] = "QuartzScheduler"; properties["quartz.scheduler.exporter.channelType"] = "tcp"; var schedulerFactory = new StdSchedulerFactory(properties); var scheduler = schedulerFactory.GetScheduler(); var job = JobBuilder.Create<PrintMessageJob>() .WithIdentity("myJob", "group1") .Build(); var trigger = TriggerBuilder.Create() .WithIdentity("myJobTrigger", "group1") .StartNow() .WithCronSchedule("/10 * * ? * *") .Build(); scheduler.ScheduleJob(job, trigger); scheduler.Start(); } public class PrintMessageJob : IJob { public void Execute(IJobExecutionContext context) { Console.WriteLine("Hello!"); } }
二:做業遠程管理端,無需寫任何代碼,引用官方程序集,嵌入到已有的web網站。
PM> Install-Package CrystalQuartz.Remote
Webconfig 須要配置的地方
<configuration> <crystalQuartz> <provider> <add property="Type" value="CrystalQuartz.Core.SchedulerProviders.RemoteSchedulerProvider, CrystalQuartz.Core" /> <add property="SchedulerHost" value="tcp://127.0.0.1:556/QuartzScheduler" /> <!--TCP監聽的地址--> </provider> </crystalQuartz> <system.webServer> <!-- Handler攔截處理了,輸出做業監控頁面--> <handlers> <add name="CrystalQuartzPanel" verb="*" path="CrystalQuartzPanel.axd" type="CrystalQuartz.Web.PagesHandler, CrystalQuartz.Web" /> </handlers> </system.webServer> </configuration>