Quartz.Net simple timed task scheduling

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.

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.

 

 

 Input command to install the package:

Install-Package Quartz

Installation results are as follows:

 

 

 

3, Quartz.NET application

Suppose you want to achieve every 5 seconds to record the current time to the console.

Because this is a console application, I would like to begin the work started, here we need to write code in static void Main (string [] args) method.

3.1, the definition of the task to be performed

Define a class that implements Quartz.IJob interface, the Execute method, the code TestJob.cs file is as follows:

TestJob class public: IJob 
    { 
        public the async the Execute the Task (IJobExecutionContext context) 
        { 
            the await Task.Run (() => 
            { 
                Console.WriteLine ( "test scheduling print job ID:" + Thread.CurrentThread.ManagedThreadId + "Time:" + DateTime + Environment.NewLine .Now); 
            }); 
        } 
    }


Then define a class code as follows DispatcherManager create a schedule () method in the Init:

DispatcherManager class public 
    { 
        public static the Task the async the Init () 
        { 
            Console.WriteLine ( "start initialization"); 
            // IScheduler unit / task completion timing example here arranged 
            // only unit starts, the job to which the normal operation 
            #region initialization scheduler 
            the StdSchedulerFactory factory's new new the StdSchedulerFactory = (); // dispatcher factory 
            IScheduler scheduler = await factory.GetScheduler (); // create a scheduler 
            
            #endregion 


            // IJob task execution timing of the action is the job 
            // ITrigger timing policy 


            Console.WriteLine ( "begin to create jobs"); 
            // create jobs 
            IJobDetail detail = JobBuilder.Create <TestJob> ( )
                 .WithIdentity ( "TestJob", "group1") 
                 .WithDescription ( "This is a test of scheduled tasks") .build (); 


            Console.WriteLine ( "Time to start creating policies"); 
            // Create time strategy 
            ITrigger trigger = TriggerBuilder .create () 
                .WithIdentity ( "tigger", "tgriup1") 
                //. startnow () 
                .WithCronSchedule ( "0/5 * * * *?") // executed once every five seconds 
                .WithDescription ( "time tested strategy ") 
                .build (); 
        
            the await scheduler.ScheduleJob (the Detail, the Trigger); // policy to the task and time scheduler 

            Console.WriteLine (" started "); 
            the await scheduler.Start ();
        }
    }

3.2 Main method calls

the Main void static (String [] args) 
        { 
            Console.WriteLine ( "test item schedule timer"); 
            DispatcherManager.Init () GetAwaiter () GetResult ();.. 

            Console.Read (); 
        }

3.3 operating results

 

 

4, Quartz cron expressions of

Cron expression is a string of 5 or 6 separated by a space, is divided into six or seven domains, each domain representing a meaning, Cron following two syntax:

(1) Seconds Minutes Hours DayofMonth Month DayofWeek Year

(2)Seconds Minutes Hours DayofMonth Month DayofWeek

structure

corn left to right (separated by spaces): Date Year seconds minute hour day of the month the month of the week (can be empty)

Cases "0 0 12? * WED" execute (the year is usually omitted) every Wednesday at 12:00 pm

Cron meaning of each field

 

 

Wildcard Description

An asterisk (*): can be used in all fields, each represents a time corresponding to the time domain, for example, when the minute field indicates "minute";

(?) Question mark: This character is used only date and day of week field, it is usually designated as "meaningless value," the equivalent of point symbols;

Minus (-): the expression of a range, such as using "10-12" in the hour field, represents from 10 to 12, i.e., 10, 11;

Comma (,): the expression of a list of values, such as the use of "MON, WED, FRI" in the week field, said Monday, Wednesday and Friday;

Slash (/): x / y expression of a sequence of steps and the like, x is the starting value, y is the increment step value. As used in the field 0/15 minutes, then to 15, 30 and 45 seconds, and 5/15 in minutes represents 5,20,35,50 field, you can use * / y, which is equivalent to 0 / y;

L: The only character in the day and date fields, representing the "Last" means, but it is a different meaning in both fields. L in the date field, represents the last day, as of January 31, 28 non-leap year the month of February; if L is used in the week, said Saturday, equivalent to 7. However, if L occurs in the week field, and in front of a value X, it said, "The last day of the month X", for example, 6L represents the last Friday of the month;

W: The character can only appear in the date field, a modification of the preamble date indicates the most recent days from that date. For example 15W indicate the nearest working day of the month No. 15, if the number is Saturday May 15, the Friday the match 14; if 15 is a Sunday, the match No. 16 Monday; if the 15th is Tuesday, and that the result is 15 Tuesday numbers. However, care must be taken not able to match the date associated with the plan period, if you specify 1W, if No. 1 is a Saturday, the result of the match is No. 3 on Monday, rather than the last day of last month. W string can only specify a single date, but can not specify a date range;

LW combination: LW can be used in combination in the date field, it means the last working day of the month;

Pound sign (#): This character can only be used in the Days field, indicate the month on a weekday. 6 # 3 as represent the third Friday of the month (Friday 6 represents, # 3 indicates that the current third), and 4 # 5 fifth Wednesday of the month, assuming that there is no fifth Wednesday of the month, do not ignore the trigger;

C: The character used only in the day and date fields, representing the "Calendar" means. It means the date of the associated program, if the date is not associated with, the calendar equivalent of all dates. For example 5C in the date field is equivalent to the first calendar day after the 5th. 1C corresponds to the field of the week Sunday after the first day.

Cron expression special characters are not case sensitive, nor insensitive to the day of the week abbreviation of the English case.

Some examples:

Expression Description

0012 * *? 12:00 each day running

01510? * * Run every day at 10:15

01510 * *? Run daily at 10:15

01510 * *? * Running daily at 10:15

01510 * *? 2008 run every day at 10:15 in 2008

0 14 * * *? Run every minute every day between 14:00 to 15:00, beginning in 14:00 and ends at 14:59.

0/5 0 14 * *? 14:00 to 15:00 every day to run once every 5 minutes, starting at 14:00 and ends at 14:55.

0 0/5 * 14, 18 *? 14:00 to 15:00 every day to run once every 5 minutes, in addition to 18 points to 19 points every day for every 5 clock run time.

00-514 * *? 14:00 to 14:05 every day, running every minute.

0 10,44 14? ​​3 WED March every Wednesday 14:10 assigned to 14:44, running every minute.

0 15 10? * MON-FRI Monday, two, three, four, five of 10:15 run.

0,151,015 *? 15th of each month 10:15 run.

0 15 10 L *? 10:15 run on the last day of each month.

0 15 10? * 6L last Friday of every month 10:15 run.

0 15 10? * 6L 2007-2009 run in 2007, 2008, the last Friday of each month 10:15 in 2009.

01510? * 6 # 3 on every third Friday of 10:15 run.

note:

(1) Some sub-expressions can contain a range or list

For example: subexpression (days (weeks)) can be "MON-FRI", "MON, WED, FRI", "MON-WED, SAT"

"*" Character represents all possible values

Therefore, "" indicates the meaning of each month in the sub-expression (month), the "" in the sub-expression (days (weeks)) represent each day of the week

"/" Character is used to increment a specified value
  , for example: in the subexpression (minutes) in the "0/15" starts from 0 minutes, every 15 minutes
in the sub-expression (minutes) in the "3/20 "starts from the first three minutes, every 20 minutes (and it" 3,23,43 ") as meaning

"?" Character is used only day (month) and day (week) two sub-expression that do not specify a value
  when one of the two sub-expressions which are assigned a value in the future, in order to avoid conflict, you need to express another child style value is set to "?"

"L" character is used only day (month) and day (week) two sub-expressions, which is an abbreviation of the word "last" of
  it meaning in the two sub-expressions is different.
  Day (month) sub-expression, "L" represents the last day of the month
  in the day (week) from the expression, "L" represents the last day of the week, which is the SAT

If there are specific content in the "L" before, it has other meanings of

For example: "6L" the reciprocal of the sixth day of the month, "FRIL" represents the best Friday of the month
  Note: When using the "L" parameter, do not specify the list or range, as this can cause problems

Expression Builder

http://cron.qqe2.com/

Most reproduced in  https://blog.csdn.net/chinahuyong/article/details/93752942

Guess you like

Origin www.cnblogs.com/shenghuotaiai/p/12484927.html