A: Introduction
Quartz.net is an open source job scheduling framework, which is very suitable for routine polling of database synchronization, timed email notification, and timed processing of data. Quartz.net allows developers to schedule jobs based on time intervals (or days). It implements a many-to-many relationship between jobs and triggers, and also associates multiple jobs with different triggers. Applications that integrate quartz.net can reuse jobs from different events and can combine multiple jobs for an event.
Two: Reference
Official Learning Document: http://www.quartz-scheduler.net/documentation/index.html
Official source code Download: http://sourceforge.net/projects/quartznet/files/quartznet
III: Installed class library files
Common.Logging.Core.dll
Common.Logging.dll
Quartz.dll
Four: Use instances
① to build a common console project
② Add a Quartz_jobs.xml file, note that its properties must be selected as always copied
<job-scheduling-data xmlns="Http://quartznet.sourceforge.net/JobSchedulingData"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.0"> <processing-directives> <overwrite-existing-data>true</overwrite-existing-data> </processing-directives> <schedule> <!--defining a sample Task 1 job--> <job& Gt <name>UseJob</name>//Task name, required, and same as the class name of the task <group>UseJobGroup</group>//(optional) The task belongs to the group < ;d escription>testusequratjob</description> <job-type>testusequrat.usejob,testusequrat</ Job-type>//(required) task type, specific type of task and owning assembly, format: class name that implements the Ijob interface, contains the full namespace, assembly name <durable>true</durable>//(optional) The specific function is not known, the official example of the default is True <recover>false</recover>//(optional) The specific role is not known, the official example defaults to False </job> <!--define a sample Task 1 trigger every 1 seconds to perform DEMOJOB1 task--<TRIGGER&G T <cron> <name>usejobtrigger</name>//(required) The name of the trigger, which must be different in the same group <group>usejobtriggergrou p</group>//(optional) trigger group <job-name>usejob</job-name>//(required) The name of the task to dispatch, which must be exactly the same as name in the corresponding job node Job-name <job-group>usejobgroup</job-group>//(optional) the group to which the dispatch task (Job) belongs, which must be exactly the same as the group in the job <cron-expression>0/1* * * *?</cron-expression> </cron> </trigger> </schedule></job-scheduling-data>
Examples of ③cron expressions:
executes every 5 seconds:*/5* * * * ?run every 1 minutes:0*/1* * * ?executed at 23 o ' Day:0 0 at* * ?once daily 1 o'clock in the morning:0 0 1* * ?every month, 1th, 1 o'clock in the morning, is executed once:0 0 1 1* ?The last day of the month is performed at 23 points:0 0 atL *?once a week, Sunday 1 o'clock in the morning:0 0 1? *L Execute at 26, 29, and 33 minutes:0 -, in, -* * * ?every day, 0, 13, 18, 21 points are executed once:0 0 0, -, -, +* * ?
④ Add a quartz.config, note: Its properties must be selected as always copied
# You can configure your schedulerincheither <quartz>configuration section# orinchQuartz Properties file# Configuration section have Precedencequartz.scheduler.instanceName=serverscheduler# Configure thread pool Infoquartz.threadPool.type=Quartz.Simpl.SimpleThreadPool, Quartzquartz.threadPool.threadCount=Tenquartz.threadPool.threadPriority=normal#--------------------------------*************plugin Configuration------------------------------------# Job Initialization plugin handles our XML reading, without it defaults is Usedquartz.plugin.xml.type=Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartzquartz.plugin.xml.fileNames= ~/quartz_jobs.xml# Export Thisserver to Remoting Contextquartz.scheduler.exporter.type=Quartz.Simpl.RemotingSchedulerExporter, Quartzquartz.scheduler.exporter.port=555Quartz.scheduler.exporter.bindName=QuartzSchedulerquartz.scheduler.exporter.channelType=Tcpquartz.scheduler.exporter.channelName= Httpquartz
⑤ implementation Method Class
Public classUsejob:ijob { Public voidExecute (Ijobexecutioncontext context) {Action a= () + = { varPath =@"C:\Users\pc-1\Desktop\time.txt"; varstr = $"task execution, current time {datetime.now}! "; File.appendalltext (path, str); }; A.begininvoke (NULL,NULL); } }
⑥ Execution method
Class Program
{
static void Main (string[] args)
{ Console execution:#regionMethod One
No need to add Quartz.config file//Xmlschedulingdataprocessor processor = new Xmlschedulingdataprocessor (new Simpletypeloadhelper ()); //ischedulerfactory SF = new Stdschedulerfactory (); //IScheduler Scheduler = SF. Getscheduler (); //Stream s = new StreamReader (".. /.. /quartz_jobs.xml "). BaseStream; //processor. Processstream (s, null); //processor. Schedulejobs (scheduler); //Scheduler. Start (); #endregionConsole Execution:#regionMethod Two//ischedulerfactory SF = new Stdschedulerfactory (); //IScheduler sched = sf. Getscheduler (); //Sched. Start (); #endregion if(!environment.userinteractive) {//WinDOS Service ExecutionServicebase.run (NewTestuseservice ()); } Else { //Console ExecutionIschedulerfactory SF =Newstdschedulerfactory (); IScheduler sched=sf. Getscheduler (); Sched. Start (); }
}
}
⑦ Execution Results
Task execution, current time 2016/10/11 17:36:44!
Task execution, current time 2016/10/11 17:36:45!
Task execution, current time 2016/10/11 17:36:46!
Task execution, current time 2016/10/11 17:36:47!
Task execution, current time 2016/10/11 17:36:48!
Task execution, current time 2016/10/11 17:36:49!
Task execution, current time 2016/10/11 17:36:50!
Task execution, current time 2016/10/11 17:36:51!
Task Scheduling Framework Quartz.net