目錄
Quartz.NET介紹\
Quartz.NET用途\
Quartz.NET安裝\
Quartz.NET實現思路\
程式碼步驟講解\
測試\
服務安裝、啓動、停止、卸載
Quartz.NET介紹Quartz.NET是一個強大、開源、輕量的作業排程框架,是 OpenSymphony 的 Quartz API 的.NET移植,用C#改寫,可用於winform和asp.net mvc、.Net Core應用中。它靈活而不複雜。你能夠用它來為執行一個作業而建立簡單的或複雜的作業排程。它有很多特徵,如:資料庫支援,叢集,外掛,支援cron-like表示式等等。\
官網:http://www.quartz-scheduler.net/\
原始碼:https://github.com/quartznet/quartznetQuartz.NET用途可以定時發郵件通知。電商網站的定時打折活動。(比如規定11月11日 淘寶購買女朋友打八折)定時對資料更新 或者新增。自己朋友生日。 可以定時發生日祝福。 等等 (我也不一一舉例了)Quartz.NET安裝Install-Package QuartzInstall-Package Quartz.JobsInstall-Package Quartz.PluginsInstall-Package TopshelfInstall-Package Topshelf.Log4NetInstall-Package log4netQuartz.NET實現思路一:繼承並實現IJob介面,在Execute 方法中寫你要定時執行的事情(切記 )\
二:使用Quartz建立任務排程核心程式碼步驟\
1、配置Quartz,建立工廠,開啟排程。\
2、建立工作任務\
3、建立觸發器\
4、將任務加入到任務池\
三:新增任務,使用Topshelf部署Windows服務,定時執行程式碼步驟講解一:繼承並實現IJob介面,在Execute 方法中寫你要定時執行的事情(切記 )using log4net;using Quartz;using System.Threading.Tasks;namespace Soetek.PowerPlatform.Job{ public class TestJob : IJob { private readonly ILog _logger = LogManager.GetLogger(typeof(TestJob)); public Task Execute(IJobExecutionContext context) { _logger.Info("每3秒輸出"); return Task.FromResult(true); } }}二:使用Topshelf調度任務 新建ServiceRunner服務運行類用於控制調度器的啟動,停止,暫停,恢復運行4個接口方法,代碼如下:using log4net;using Quartz;using Quartz.Impl;using System;using Topshelf;namespace Soetek.PowerPlatform.Job{ public sealed class ServiceRunner : ServiceControl, ServiceSuspend { /// \ /// 定義任務調度器 /// \ private readonly IScheduler scheduler; private readonly ILog _logger = LogManager.GetLogger(typeof(ServiceRunner)); /// \ /// 構造方法 /// \ public ServiceRunner() { // 建立 scheduler // 從任務調度工廠獲取默認的任務調度器 scheduler = StdSchedulerFactory.GetDefaultScheduler().GetAwaiter().GetResult(); } /// \ ///服務啓動 /// \ /// \The host control.\ /// \\ public bool Start(HostControl hostControl) { try { scheduler.Start(); _logger.Info("服務已啓動..."); return true; } catch (Exception ex) { _logger.Info("服務啓動失敗:" + ex.Message); return false; } } /// \ /// 服務停止 /// \ /// \The host control.\ /// \\ public bool Stop(HostControl hostControl) { try { scheduler.Shutdown(false); _logger.Info("服務已停止..."); return true; } catch (Exception ex) { _logger.Info("服務停止失敗:" + ex.Message); return false; } } /// \ /// 服務繼續 /// \ /// \The host control.\ /// \\ public bool Continue(HostControl hostControl) { try { scheduler.ResumeAll(); _logger.Info("服務已繼續..."); return true; } catch (Exception ex) { _logger.Info("服務繼續失敗:" + ex.Message); return false; } } /// \ /// 服務暫停 /// \ /// \The host control.\ /// \\ public bool Pause(HostControl hostControl) { try { scheduler.PauseAll(); _logger.Info("服務已暫停..."); return true; } catch (Exception ex) { _logger.Info("服務暫停失敗:" + ex.Message); return false; } } }}三:完善主程式入口\
修改啓動項目(即控制檯項目)的Program.cs,代碼如下:using System;using System.IO;using Topshelf;namespace Soetek.PowerPlatform.Job{ class Program { /// \ /// 主程式入口 /// \ /// \The arguments.\ static void Main(string[] args) { log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config")); HostFactory.Run(x => { x.UseLog4Net(); // 註冊 WinService 至 Topshelf x.Service\(s => { s.ConstructUsing(name => new ServiceRunner()); // 註冊 WinService 初始化方法至 Topshelf s.WhenStarted((tc, hc) => tc.Start(hc)); // 註冊如何啟動 s.WhenStopped((tc, hc) => tc.Stop(hc)); // 註冊如何停止 s.WhenContinued((tc, hc) => tc.Continue(hc)); // 註冊如何繼續 s.WhenPaused((tc, hc) => tc.Pause(hc)); // 註冊如何暫停 }); // 以 `local system` 執行 x.RunAsLocalService(); x.StartAutomaticallyDelayed(); x.SetDescription("用於系統定時任務計劃執行"); // windows服務描述 x.SetDisplayName("工作排程器"); // windows服務顯示的名稱 x.SetServiceName("工作排程器"); // windows服務的名稱 x.EnablePauseAndContinue(); }); } }}四:任務調度的配置文件 在啓動項目(控制檯項目)下增加2個配置文件,並把[複製到輸出目錄]屬性設置爲"如果較新則複製"。\
1、第一個配置文件爲quartz.config,內容如下:# You can configure your scheduler in either \ configuration section# or in quartz properties file# Configuration section has precedencequartz.scheduler.instanceName = QuartzTest# configure thread pool infoquartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartzquartz.threadPool.threadCount = 10quartz.threadPool.threadPriority = Normal# job initialization plugin handles our xml reading, without it defaults are usedquartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Pluginsquartz.plugin.xml.fileNames = \~/quartz_jobs.xml# export this server to remoting context#quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz#quartz.scheduler.exporter.port = 555#quartz.scheduler.exporter.bindName = QuartzScheduler#quartz.scheduler.exporter.channelType = tcp#quartz.scheduler.exporter.channelName = httpQuartz你可以通過修改quartz.threadPool.threadCount = 2中的2改變處理定時任務的線程數。2、第2個配置文件爲quartz_jobs.xml,內容如下:\\ \ \true\ \ \ \ \ \TestJob\ \Test\ \測試任務\ \Soetek.PowerPlatform.Job.TestJob,Soetek.PowerPlatform.Job\ \true\ \false\ \ \ \ \TestJobTrigger\ \TestTrigger\ \TestJob\ \Test\ \2021-11-01T00:00:00+08:00\ \0/3 * * * * ?\ \ \ \\3、第3個log配置文件爲log4net.config,內容如下:\\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\可以通過修改0/3 * * * * ?改變觸發任務的時機,0/3 * * * * ?代表每3秒觸發一次。具體表達式的寫法請學習cron表達式。服務安裝、啓動、停止、卸載以Release方式編譯控制檯項目,把Release輸出目錄下的內容複製到一個指定目錄,比如d:\myservice下。以管理員方式運行cmd。通過以下命令進行服務的安裝、啓動、停止、卸載。1、服務安裝d:\ScheduleJobService\Soetek.PowerPlatform.Job.exe install2、服務啓動d:\ScheduleJobService\Soetek.PowerPlatform.Job.exe start3、服務停止d:\ScheduleJobService\Soetek.PowerPlatform.Job.exe stop4、服務卸載d:\ScheduleJobService\Soetek.PowerPlatform.Job.exe uninstall服務安裝後,就可以在Windows的服務列表中看到此服務,也可以在windows的服務管理面板中對服務進行啓動和停止操作,當然也可以設置爲開機啓動和禁用。