分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

Quartz.net入门

发布时间:2023-09-06 01:31责任编辑:蔡小小关键词:暂无标签

简介

Quartz.NET是一个开源的作业调度框架,是OpenSymphony的 Quartz API的.NET移植,
它用C#写成,可用于winform和asp.net应用中。它提供了巨大的灵活性而不牺牲简单性。
你能够用它来为执行一个作业而创建简单的或复杂的调度。它有很多特征,
如:数据库支持,集群,插件,支持cron-like表达式等等。

https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/index.html

Nuget

1.一旦调度程序被实例化,它就可以启动,进入待机模式,并关机。请注意,一旦调度程序关闭,不能重新启动而不被重新实例化。触发器不会触发(作业不会执行),直到调度程序启动,也不会处于暂停状态。

 ???public class Program ???{ ???????private static void Main(string[] args) ???????{ ???????????// construct a scheduler factory ???????????ISchedulerFactory schedFact = new StdSchedulerFactory(); ???????????// get a scheduler ???????????IScheduler sched = schedFact.GetScheduler(); ???????????sched.Start(); ???????????// define the job and tie it to our HelloJob class ???????????IJobDetail job = JobBuilder.Create<HelloJob>() ???????????????.WithIdentity("myJob", "group1") ???????????????.Build(); ???????????// Trigger the job to run now, and then every 40 seconds ???????????ITrigger trigger = TriggerBuilder.Create() ?????????????.WithIdentity("myTrigger", "group1") ?????????????.StartNow() ?????????????.WithSimpleSchedule(x => x ?????????????????.WithIntervalInSeconds(10) ?????????????????.RepeatForever()) ?????????????.Build(); ???????????sched.ScheduleJob(job, trigger); ???????????Thread.Sleep(TimeSpan.FromSeconds(30)); ???????????sched.Shutdown(); ???????????//关闭后,重新启动计划异常 ???????????//sched.Start(); ???????????Console.WriteLine($"Press any key to close the application!{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); ???????????Console.ReadKey(); ???????} ???} ???public class HelloJob : IJob ???{ ???????public void Execute(IJobExecutionContext context) ???????{ ???????????Console.WriteLine($"Greetings from HelloJob!{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); ???????} ???}
View Code

 2.接口和静态方法

IScheduler - 与调度程序交互的主要API。
IJob - 您希望由调度程序执行的组件实现的接口。
IJobDetail - 用于定义作业的实例。
ITrigger - 定义执行给定Job的时间表的组件。
JobBuilder - 用于定义/构建JobDetail实例,它定义了Jobs的实例。
TriggerBuilder - 用于定义/构建触发器实例。

3.参数传递

namespace quartz.net{ ???public class Program ???{ ???????private static void Main(string[] args) ???????{ ???????????// construct a scheduler factory ???????????ISchedulerFactory schedFact = new StdSchedulerFactory(); ???????????// get a scheduler ???????????IScheduler sched = schedFact.GetScheduler(); ???????????sched.Start(); ???????????// define the job and tie it to our HelloJob class ???????????IJobDetail job = JobBuilder.Create<HelloJob>() ???????????????.WithIdentity("myJob", "group1") ???????????????//JobDataMap可以用来保存任何数量的(可序列化的)对象,当你执行任务时,你希望这些对象可用。JobDataMap是IDictionary接口的一个实现,并且增加了一些方便的方法来存储和检索原始类型的数据。 ???????????????.UsingJobData("JobSays", "Hello World!") ??????????????????????????.UsingJobData("FloatValue", 3.141f) ???????????????.Build(); ???????????// Trigger the job to run now, and then every 40 seconds ???????????ITrigger trigger = TriggerBuilder.Create() ?????????????.WithIdentity("myTrigger", "group1") ?????????????.StartNow() ?????????????.WithSimpleSchedule(x => x ?????????????????.WithIntervalInSeconds(3) ?????????????????.RepeatForever()) ?????????????.Build(); ???????????sched.ScheduleJob(job, trigger); ???????????Thread.Sleep(TimeSpan.FromSeconds(30)); ???????????sched.Shutdown(); ???????????//关闭后,重新启动计划异常 ???????????//sched.Start(); ???????????Console.WriteLine($"Press any key to close the application!{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); ???????????Console.ReadKey(); ???????} ???} ???public class HelloJob : IJob ???{ ???????public string JobSays { private get; set; } ???????public float FloatValue { private get; set; } ???????public void Execute(IJobExecutionContext context) ???????{ ???????????JobKey key = context.JobDetail.Key; ???????????//从JobDataMap获取值 ???????????//JobDataMap dataMap = context.JobDetail.JobDataMap; ???????????//string jobSays = dataMap.GetString("JobSays"); ???????????//float myFloatValue = dataMap.GetFloat("FloatValue"); ???????????Console.Error.WriteLine("Instance " + key + " of DumbJob says: " + JobSays + ", and val is: " + FloatValue); ???????????Console.WriteLine($"Greetings from HelloJob!{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); ???????} ???}}
View Code

 4.配置任务出发时间次数

 ???????????//在特定的时刻建立一个触发器,不要重复: ???????????ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() ??????????????.WithIdentity("trigger1", "group1") ??????????????.StartAt(DateTimeOffset.Now) // some Date ???????????????.ForJob("myJob", "group1") // identify job with name, group strings ??????????????.Build();
 ???????????//建立一个触发器,将在未来5秒触发一次: ???????????ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() ???????????????.WithIdentity("trigger5", "group1") ???????????????.StartAt(DateBuilder.FutureDate(5, IntervalUnit.Second)) // use DateBuilder to create a date in the future ???????????????.ForJob(job) // identify job with its JobKey ???????????????.Build(); ???????????sched.ScheduleJob(job,trigger);
 ???????????ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() ???????????????.WithIdentity("trigger3", "group1") ???????????????.StartNow() ???????????????.WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(10)) // note that 10 repeats will give a total of 11 firings ???????????????.ForJob(myJob) // identify job with handle to its JobDetail itself ??????????????????????????????????.Build();
 ???????????var trigger = TriggerBuilder.Create() ???????????.WithIdentity("trigger7", "group1") ???????????.WithSimpleSchedule(x => x ???????????????.WithIntervalInSeconds(5) ???????????????.RepeatForever()) ???????????????.EndAt(DateBuilder.DateOf(14, 21, 0)) ???????????????.Build();

5.CronTrigger

Cron-Expressions用于配置CronTrigger的实例。Cron-Expressions是由七个子表达式组成的字符串,
它们描述了计划的各个细节。这些子表达式用空格分隔,表示:

分钟
小时
日(MON - SUN)
月(为0到11之间的值)
星期X
年份(可选字段)

*字符可以用来表示该字段的“每个”可能的值。
/字符可用于指定增量值。
例如,如果您在“分钟”字段中输入“0/15”,则意味着“每15分钟,从零开始”。
如果在“分钟”字段中使用了“3/20”,则意味着“每小时20分钟,从第三分钟开始” - 或者换句话说,就是在分钟中指定“3,23,43”领域。

?字符被允许用于日期和星期几字段。
它用来指定“没有具体的价值”。
当你需要在两个字段中的一个字段中指定某些内容时,这是非常有用的,而不是其他的。请参阅下面的示例(和CronTrigger API文档)进行说明。

月份和星期几字段允许使用“L”字符。这个角色对于“最后”来说是短暂的,但是在两个领域中的每一个都有不同的含义。例如,月份字段中的值“L”意味着“月份的最后一天”,
即非闰年的2月28日的1月31日。如果单独使用在星期几字段中,则仅表示“7”或“SAT”。但是,如果在星期几字段中使用另一个值,则表示“本月的最后一个xxx日”,
例如“6L”或“FRIL”都表示“本月的最后一个星期五”。当使用“L”选项时,不要指定列表或值的范围,因为您会得到令人困惑的结果。

“W”用于指定与指定日期最近的星期几(星期一至星期五)。例如,如果您要指定“15W”作为月份日期字段的值,则其含义是:“最近的星期几到本月15日”。

“#”用于指定该月的第n个“XXX”工作日。例如,星期几字段中的“6#3”或“FRI#3”的值表示“月的第三个星期五”。

"0 0 12?* WED" 每个星期三中午12:00触发"0 0/5 * * * ?" 每5分钟触发"0 30 10-13 ? * WED,FRI"每周三和周五的10:30,11:30,12:30和13:30发生的触发器

Quartz.net入门

原文地址:http://www.cnblogs.com/lgxlsm/p/8059048.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved