-
Quartz - 作业调度框架-插件化开发
背景
大部分业务都是基于定时的任务,特别适合使用quartz这类框架解决定时问题。具体quartz的使用,看官方文档就可以了。下面谈谈对quartz插件化的封装。我们使用quartz.plugin。然后在quartz_jobs.xml方法里面定义了schedule,其中灵活的地方在于,里面定义了Jobs的属性,在QuartzPlugin的start方法执行的时候,会去加载quartz_jobs文件,逐个job信息进行加载。
解决思路
在实际使用中,开发就变得相对简单了,不需要关注job任务是如何被调度的。只需要在程序中定义一个类实现job接口,填充业务代码,然后在文件里面填写该job属性:
[DisallowConcurrentExecution] public class AnalysisJob : IJob { public void Execute(IJobExecutionContext context) { xxxxxxxxxx } } <job> <name>名称</name> <group>分组</group> <description>描述</description> <job-type>类库</job-type> <durable>true</durable> <recover>false</recover> </job>
这样的封装就赋予框架新的技能,大大提高了开发人员的开发效率。
主要代码
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<MainService>((s) => { s.ConstructUsing(settings => new MainService()); s.WhenStarted(tr => tr.Start()); s.WhenStopped(tr => tr.Stop()); }); x.RunAsLocalSystem(); x.SetDescription(""); x.SetDisplayName("xxxx任务管理器"); 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} 任务"); 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
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数