Quartz - 作業(yè)調(diào)度框架-插件化開發(fā)
背景
大部分業(yè)務(wù)都是基于定時的任務(wù),特別適合使用quartz這類框架解決定時問題。具體quartz的使用,看官方文檔就可以了。下面談?wù)剬uartz插件化的封裝。我們使用quartz.plugin。然后在quartz_jobs.xml方法里面定義了schedule,其中靈活的地方在于,里面定義了Jobs的屬性,在QuartzPlugin的start方法執(zhí)行的時候,會去加載quartz_jobs文件,逐個job信息進(jìn)行加載。
解決思路
在實際使用中,開發(fā)就變得相對簡單了,不需要關(guān)注job任務(wù)是如何被調(diào)度的。只需要在程序中定義一個類實現(xiàn)job接口,填充業(yè)務(wù)代碼,然后在文件里面填寫該job屬性:
[]public class AnalysisJob : IJob{public void Execute(IJobExecutionContext context){xxxxxxxxxx}}
<job><name>名稱name><group>分組group>??????<description>描述description><job-type>類庫job-type><durable>truedurable><recover>falserecover>job>
?這樣的封裝就賦予框架新的技能,大大提高了開發(fā)人員的開發(fā)效率。

主要代碼
using System;using System.Collections.Generic;using System.Linq;using Topshelf;namespace HSCP.Task{class Program{static void Main(string[] args){HostFactory.Run(x =>{x.Service((s) => {s.ConstructUsing(settings => new MainService());s.WhenStarted(tr => tr.Start());s.WhenStopped(tr => tr.Stop());});x.RunAsLocalSystem();x.SetDescription("");x.SetDisplayName("xxxx任務(wù)管理器");x.SetServiceName("HSCP.Task");});}}}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Quartz;using Quartz.Impl;namespace HSCP.Task{class MainService{static IScheduler sched;public void Start(){try{ISchedulerFactory factory = new StdSchedulerFactory();sched = factory.GetScheduler();sched.Start();Console.WriteLine($"共 {sched.GetJobGroupNames().Count} 任務(wù)");foreach (string gn in sched.GetJobGroupNames())Console.WriteLine(gn);}catch (Exception exc){Console.WriteLine(exc.ToString());}// NLogger.Info(string.Format("啟動成功 {0}", DateTime.Now));}public void Stop(){sched.Shutdown(true);}}}
開源地址
https://github.com/quartznet/quartznet
評論
圖片
表情
