This article is mainly for you to introduce the Quartz.net scheduling framework configuration method, with a certain reference value, interested in small partners can refer to
In peacetime work, most of the estimates have done polling scheduling tasks, such as scheduled polling database synchronization, scheduled mail notification and so on. You have implemented such tasks through Windows Scheduled Tasks, Windows services, and so on, and even implemented a customized framework of your own configuration. Let's introduce an open source dispatch framework quartz.net (the implementation of the configuration is introduced, because a friend has asked such a question). Dispatch implementation code is very simple, in the source code has a large number of demo, here is skipped.
Quartz.net Current Latest Version Quartz.net 2.0 Beta 1 released
First, file-based configuration
Let's take a look at the simple implementation code
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;using Quartz;using quartz.impl;using Common.logging;namespace demo{class Program { static void Main (string[] args) { //First we must get a reference to a scheduler Ischedulerfactory SF = new Stdschedulerfactory (); IScheduler sched = sf. Getscheduler (); Sched. Start (); Sched. Shutdown (True); } }}
The code is simple, the Quartz basic configuration in the configuration file, and how the Job,trigger information is loaded? This process is occurring IScheduler sched = sf. Getscheduler (); process, mainly reflected in the source code this paragraph
public void Initialize () {//short-circuit if already initialized if (cfg! = null) {return; } if (initexception! = null) {throw initexception; } NameValueCollection props = (NameValueCollection) configurationmanager.getsection ("quartz"); String requestedfile = Environment.getenvironmentvariable (propertiesfile); String Propfilename = Requestedfile! = null && Requestedfile.trim (). Length > 0? Requestedfile: "~/quartz.config"; Check for Specials Propfilename = Fileutil.resolvefile (propfilename); if (props = = null && file.exists (propfilename)) {//File system try {propertiesparser PP = Proper Tiesparser.readfromfileresource (Propfilename); props = pp. Underlyingproperties; Log.info (String. Format ("Quartz.net properties loaded from configuration file ' {0} '", Propfilename)); } catch (Exception ex) {log.error ("Could not load properties for Quartz from file {0}: {1}". Formatinvariant (Propfilename, ex.Message), ex); }} if (props = = null) {//read from assembly try {Propertiesparser PP = Propertiesparser.readfromemb Eddedassemblyresource ("Quartz.quartz.config"); props = pp. Underlyingproperties; Log.info ("Default quartz.net properties loaded from Embedded Resource file"); } catch (Exception ex) {log.error ("Could not load default properties for Quartz from Quartz assembly: {0}". Formatinvariant (ex. Message), ex); }} if (props = = null) {throw new Schedulerconfigexception (@ "Could not find <quartz> configuration SE Ction from your application config or load the default configuration from assembly. Please add the configuration to your application config file to correctly initialize Quartz. "); Initialize (Overridewithsysprops (props)); }
With the above code analysis, initialization first checks the system config for the <quartz> configuration section node (config refers to the app.config,web.config), If the system config has a quartz node, the configuration information is loaded directly here. If the system config does not have Quartz basic configuration information, it will continue to find out if there are quartz.config/quartz.quartz.config for both profiles, and if so, load the configuration information, and if not, throw out the initialization configuration exception.
While Jobs.xml (Scheduled tasks and triggers plugin node profile)
Plugin configuration in App.config/web.config
<quartz> <add key= "Quartz.scheduler.instanceName" value= "Exampledefaultquartzscheduler"/> <add key= "Quartz.threadPool.type" value= "Quartz.Simpl.SimpleThreadPool, quartz"/> <add key= " Quartz.threadPool.threadCount "value="/> <add key= "quartz.threadPool.threadPriority" value= "2"/> <add key= "Quartz.jobStore.misfireThreshold" value= "60000"/> <add key= "Quartz.jobStore.type" Value= "Quartz.Simpl.RAMJobStore, Quartz"/> <!--******************************plugin configuration ********************* --<add key= "Quartz.plugin.xml.type" value= " Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz "/> <add key=" Quartz.plugin.xml.fileNames "value=" Quartz_jobs.xml "/> </quartz>
Quartz.config plugin configuration point (quartz.plugin.xml.type/quartz.plugin.xml.filenames)
# You can configure your scheduler in either <quartz> configuration section# or in quartz Properties file# Configuration section have precedencequartz.scheduler.instanceName = serverscheduler# Configure thread P Ool Infoquartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartzquartz.threadPool.threadCount = 10quartz.threadpool.threadpriority = normal#--------------------------------************* Plugin Configuration------------------------------------# Job initialization plugin handles our XML reading, without it defaults is U Sedquartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartzquartz.plugin.xml.fileNames = ~/quartz_jobs.xml# Export This server 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
Second, the code-based approach
This happens directly through the code, and the official demo is a lot of it, so let's give an example
Using system;using system.collections.generic;using system.linq;using system.text; Using quartz;using quartz.impl;using system.threading;using common.logging;namespace Demo{class Program {static void Ma In (string[] args) {ILog log = Logmanager.getlogger (typeof (Demo.hellojob)); Log. Info ("-------Initializing----------------------"); First we must get a reference to a scheduler ischedulerfactory SF = new Stdschedulerfactory (); IScheduler sched = sf. Getscheduler (); Log. Info ("-------Initialization complete-----------"); ---------------------------------------code add job and trigger//Computer A time that's on the next round minute Datetimeo Ffset runTime = datebuilder.evenminutedate (Datetimeoffset.utcnow); Log. Info ("-------scheduling Job-------------------"); Define the job and tie it to our Hellojob class Ijobdetail job = Jobbuilder.create
In fact, the code has been implemented and the configuration file mashup way. But this alignment is done by configuring the associated load job with the trigger configuration, and we have a third way to load the job and the trigger configuration file ourselves.
Third, manually load the configuration file
Using system;using system.collections.generic;using system.linq;using system.text; Using quartz;using quartz.xml;using quartz.impl;using quartz.simpl;using system.threading;using Common.Logging;using System.io;namespace demo{class Program { static void Main (string[] args) { xmlschedulingdataprocessor processor = new Xmlschedulingdataprocessor (new Simpletypeloadhelper ()); Ischedulerfactory SF = new Stdschedulerfactory (); IScheduler Scheduler = SF. Getscheduler (); Stream s = new StreamReader ("~/quartz.xml"). BaseStream; Processor. Processstream (s, null); Processor. Schedulejobs (scheduler); Scheduler. Start (); Scheduler. Shutdown (); } }}
Or so.
Using System.Text; Using quartz;using quartz.xml;using quartz.impl;using quartz.simpl;using system.threading;using Common.Logging;using System.io;namespace demo{class Program { static void Main (string[] args) { xmlschedulingdataprocessor processor = new Xmlschedulingdataprocessor (new Simpletypeloadhelper ()); Ischedulerfactory SF = new Stdschedulerfactory (); IScheduler Scheduler = SF. Getscheduler (); Processor. Processfileandschedulejobs ("~/quartz.xml", scheduler); Scheduler. Start (); Scheduler. Shutdown (); } }}
At present, according to the source code analysis of these several configuration methods, very flexible can be any combination. The use of the source code for Quartz is very detailed, and the amount of the garden has a lot of learning articles.
Remember that quartz's configuration reads first app.config/web.config---->quartz.config/quartz.quartz.config---->quartz_jobs.xml.
By way of code we can change Quartz_jobs.xml's point to its own new named XML file
(The default quartz_jobs.xml is specified in xmlschedulingdataprocessor.quartzxmlfilename = "Quartz_jobs.xml")
Way one, through the Quartz node/quartz.config points to the specified jobs.xml.
Mode two, loading the specified jobs.xml via Xmlschedulingdataprocessor