Detailed timing of scheduling for job Quartz.Net

Original: Quartz.Net implement job scheduling timing of Explanation

1, Quartz.NET Introduction

Quartz.NET is a powerful, open source job scheduling framework, lightweight, and you can use it to create simple or complex job scheduling to perform a job. It has many features, such as: database support, clustering, plug-ins, support for cron-like expression, and so on. Very suitable in the daily work, the timing polling database synchronization, the timing of the notification message, processing the timing data.

Quartz.NET allows developers to schedule jobs time interval (or days) in accordance with. It implements many relationships and triggers the job, but also to multiple jobs associated with different triggers. Integrated Quartz.NET application can reuse jobs from different events, one event can also combine multiple jobs.

Official website: http://www.quartz-scheduler.net/

Source: https://github.com/quartznet/quartznet

Example: https://www.quartz-scheduler.net/documentation/quartz-3.x/quick-start.html

Quartz.NET is a strong, open framework job scheduling, lightweight, portable .NET is the OpenSymphony Quartz API, and rewrite C #, and can be used winform Web applications. It is complex and not flexible, you can use it to create simple or complex job scheduling to perform a job. Quartz.NET 3.0 has begun to support .NET Core / .NET Standard 2.0.

Job operations for the interface, is used to describe the JobDetail Job implementation class and other relevant static information; Trigger management tool as the timing of operations, a Trigger example corresponds to only one job, and a job instance may correspond to a plurality of Trigger; do Scheduler the timing task container, which contains all the flip-flops and jobs, there are JobDetail each Scheduler and trigger
registered, a Scheduler can register multiple JobDetail and multiple trigger.

2, frame-dependent

Framework for the introduction of a very simple method you can also add a reference to the direct use of nuget management pack in the project. In order to meet different customer needs, we in the most simple way to explain how to use Quartz.NET correct in Visual Studio.

2.1, use Add Nuget use

2.1.0 Create a project

Create a new project, either ASP.NET MVC, WebForms, Winforms, .NET Core .Net and other items used here is VS2017, create a console application project.

To use Quartz.NET we need to install Quartz.NET package, the easiest way is to download the dll file from Quartz.NET pipe network references can be. In this paper we use Nuget be a reference to the dll file and management. To use Nuget must ensure that you have installed, the easiest way is through VS "Tools" menu to see if there are package management console, if a note has been installed, as shown below.

If none is found then we have to be installed.

2.1.2, the installation Nuget

The new version of Visual Studio is Nuget installed by default, such as Visual Studio 2015+, if not installed, open the VS menu "Tools" -> "extension and renewal."

After searching in the expansion and upgrade update "nuget", may be a new install or uninstall.

2.1.3, using nuget installation Quartz.NET

Click on "Tools" -> "NuGet Package Manager" -> "Package Manager Console"

Input command to install the package:

Install-Package Quartz

Installation results are as follows:

时包管理器中就下载了需要的程序集与相关文件,同时程序中也添加了引用。

3、Quartz.NET应用

假定要实现每隔5秒钟向控制台记录当前时间。

因为这是一个控制台应用,我想一启动时就开始该项工作,这里我们需要将代码写在static void Main(string[] args)方法中。

3.1、定义要执行的任务

定义一个类,实现Quartz.IJob接口,实现方法Execute,TimeJob.cs文件的代码如下:

using System;
using System.Threading.Tasks;

namespace QuartzTest
{
    using Quartz;

    public class TimeJob : IJob
    {
        /// <summary>
        /// 作业调度定时执行的方法
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Execute(IJobExecutionContext context)
        {
            await Console.Out.WriteLineAsync("Hello QuartzNet..." + DateTime.Now + Environment.NewLine);           
        }
    }
}

3.2、创建一个调度器

调度器负责管理与控制任务的执行,在Main方法中添加如下代码:

//调度器
IScheduler scheduler;
//调度器工厂
ISchedulerFactory factory;

//创建一个调度器
factory = new StdSchedulerFactory();
scheduler = factory.GetScheduler();
scheduler.Start();

3.3、创建一个任务对象

这个任务对象就是我们将要执行的工作,job1是名称,group1是组名。
//2、创建一个任务
IJobDetail job = JobBuilder.Create ().WithIdentity("job1", "group1").Build();

3.4、创建一个触发器

触发器定义了什么时间任务开始或每隔多久执行一次。

//3、创建一个触发器
//DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("trigger1", "group1")
    .WithCronSchedule("0/5 * * * * ?")     //5秒执行一次
    .Build();

3.5、将任务与触发器添加到调度器中并执行

//4、将任务与触发器添加到调度器中
scheduler.ScheduleJob(job, trigger);
//5、开始执行
scheduler.Start();

3.6、运行结果

3.7 Main方法完整代码

using Quartz;
using Quartz.Impl;
using System;

namespace QuartzTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //1、调度器
            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sched = sf.GetScheduler();
            //2、创建一个任务
            IJobDetail job = JobBuilder.Create<TimeJob>()
              .WithIdentity("job1", "group1")
              .Build();

            //3、创建一个触发器
            //DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")
                .WithCronSchedule("0/5 * * * * ?")     //5秒执行一次                                                      
                .Build();

            sched.ScheduleJob(job, trigger);
            //启动任务
            sched.Start();
        }
    }
}

4、Quartz的cron表达式

 Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:

  (1) Seconds Minutes Hours DayofMonth Month DayofWeek Year

  (2)Seconds Minutes Hours DayofMonth Month DayofWeek

结构

  corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份(可为空)

  例  "0 0 12 ? * WED" 在每星期三下午12:00 执行(年份通常 省略)

Cron各字段的含义

通配符说明

星号(*):可用在所有字段中,表示对应时间域的每一个时刻,例如, 在分钟字段时,表示“每分钟”;

问号(?):该字符只在日期和星期字段中使用,它通常指定为“无意义的值”,相当于点位符;

减号(-):表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12;

逗号(,):表达一个列表值,如在星期字段中使用“MON,WED,FRI”,则表示星期一,星期三和星期五;

斜杠(/):x/y表达一个等步长序列,x为起始值,y为增量步长值。如在分钟字段中使用0/15,则表示为0,15,30和45秒,而5/15在分钟字段中表示5,20,35,50,你也可以使用*/y,它等同于0/y;

L:该字符只在日期和星期字段中使用,代表“Last”的意思,但它在两个字段中意思不同。L在日期字段中,表示这个月份的最后一天,如一月的31号,非闰年二月的28号;如果L用在星期中,则表示星期六,等同于7。但是,如果L出现在星期字段里,而且在前面有一个数值X,则表示“这个月的最后X天”,例如,6L表示该月的最后星期五;

W:该字符只能出现在日期字段里,是对前导日期的修饰,表示离该日期最近的工作日。例如15W表示离该月15号最近的工作日,如果该月15号是星期六,则匹配14号星期五;如果15日是星期日,则匹配16号星期一;如果15号是星期二,那结果就是15号星期二。但必须注意关联的匹配日期不能够跨月,如你指定1W,如果1号是星期六,结果匹配的是3号星期一,而非上个月最后的那天。W字符串只能指定单一日期,而不能指定日期范围;

LW组合:在日期字段可以组合使用LW,它的意思是当月的最后一个工作日;

井号(#):该字符只能在星期字段中使用,表示当月某个工作日。如6#3表示当月的第三个星期五(6表示星期五,#3表示当前的第三个),而4#5表示当月的第五个星期三,假设当月没有第五个星期三,忽略不触发;

C:该字符只在日期和星期字段中使用,代表“Calendar”的意思。它的意思是计划所关联的日期,如果日期没有被关联,则相当于日历中所有日期。例如5C在日期字段中就相当于日历5日以后的第一天。1C在星期字段中相当于星期日后的第一天。

Cron表达式对特殊字符的大小写不敏感,对代表星期的缩写英文大小写也不敏感。

一些例子:

表示式 说明

0 0 12 * * ? 每天12点运行

0 15 10 ? * * 每天10:15运行

0 15 10 * * ? 每天10:15运行

0 15 10 * * ? * 每天10:15运行

0 15 10 * * ? 2008 在2008年的每天10:15运行

0 * 14 * * ? 每天14点到15点之间每分钟运行一次,开始于14:00,结束于14:59。

0 0/5 14 * * ? 每天14点到15点每5分钟运行一次,开始于14:00,结束于14:55。

0 0/5 14,18 * * ? 每天14点到15点每5分钟运行一次,此外每天18点到19点每5钟也运行一次。

0 0-5 14 * * ? 每天14:00点到14:05,每分钟运行一次。

0 10,44 14 ? 3 WED 3月每周三的14:10分到14:44,每分钟运行一次。

0 15 10 ? * MON-FRI 每周一,二,三,四,五的10:15分运行。

0 15 10 15 * ? 每月15日10:15分运行。

0 15 10 L * ? 每月最后一天10:15分运行。

0 15 10 ? * 6L 每月最后一个星期五10:15分运行。

0 15 10 ? * 6L 2007-2009 在2007,2008,2009年每个月的最后一个星期五的10:15分运行。

0 15 10 ? * 6#3 每月第三个星期五的10:15分运行。

注意:

(1)有些子表达式能包含一些范围或列表

  例如:子表达式(天(星期))可以为 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”

“*”字符代表所有可能的值

  因此,“”在子表达式(月)里表示每个月的含义,“”在子表达式(天(星期))表示星期的每一天

  “/”字符用来指定数值的增量 
  例如:在子表达式(分钟)里的“0/15”表示从第0分钟开始,每15分钟 
在子表达式(分钟)里的“3/20”表示从第3分钟开始,每20分钟(它和“3,23,43”)的含义一样

  “?”字符仅被用于天(月)和天(星期)两个子表达式,表示不指定值 
  当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?”

  “L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写 
  但是它在两个子表达式里的含义是不同的。 
  在天(月)子表达式中,“L”表示一个月的最后一天 
  在天(星期)自表达式中,“L”表示一个星期的最后一天,也就是SAT

  如果在“L”前有具体的内容,它就具有其他的含义了

  例如:“6L”表示这个月的倒数第6天,“FRIL”表示这个月的最一个星期五 
  注意:在使用“L”参数时,不要指定列表或范围,因为这会导致问题

表达式生成器
有很多的cron表达式在线生成器,这里给大家推荐几款
http://www.pdtools.net/tools/becron.jsp

或者

http://cron.qqe2.com/

5、其他文章参考


一路走来数个年头,感谢RDIFramework.NET框架的支持者与使用者,大家可以通过下面的地址了解详情。

RDIFramework.NET官方网站:http://www.rdiframework.net/

RDIFramework.NET官方博客:http://blog.rdiframework.net/

同时需要说明的,以后的所有技术文章以官方网站为准,欢迎大家收藏!

RDIFramework.NET框架由专业团队长期打造、一直在更新、一直在升级,请放心使用!

欢迎关注RDIFramework.net框架官方公众微信(微信号:guosisoft),及时了解最新动态。

扫描二维码立即关注

file

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11105748.html
Recommended