Z.net usage summary, Quartz.net Summary
In this project, task scheduling is used to implement the SMS sending gateway. Therefore, we will share the experience of Quartz.net. Quartz.net is used for task scheduling. Previously, the window service installed in C # was used. This time, we plan to use Topshel as the window service. In fact, it feels similar. OK. Next let's talk about the usage of Quartz.net in development:
Quartz. NET Introduction
The goal of Job Scheduling is to ensure efficient data processing processes based on predefined time and specified sequence, so as to maximize the use of system resources. Batch Processing is an operation that runs in sequence in the background without end user intervention.
Quartz. NET is an open-source job scheduling framework and is a. NET port of the Quartz API of OpenSymphony. It is written in C # and can be used in winform and asp.net applications. It provides great flexibility without sacrificing simplicity. You can use it to create simple or complex scheduling for executing a job. It has many features, such as database support, clusters, plug-ins, and cron-like expressions.
Quartz. NET is an open-source job scheduling framework that can be used in small applications or even enterprise applications. It has the following features:
- API operations are simple. With just a few lines of simple code, you can schedule your jobs in the application and monitor job execution in real time.
- The trigger function is powerful and provides a finer trigger granularity than the Windows task plan. You can use the "Cron expression (which will be introduced later)" to implement it, for example, am from Monday to Friday, pm (Working Hours) to execute a task
- Good scalability. It is based on Interface Programming. You can implement your own Schedule scheduler, Job, Trigger, etc.
- Jobs can be stored in RAM or persisted to databases. Multiple database types are supported: SqlServer, Oracle, MySql, etc.
- Cluster, an advanced application that allows you to create load balancing and fault tolerance between multiple computers.
Use Quartz. NET
First, reference the following dll
The first four areQuartz. NETUse required, ToshelfIs used for the window service.
Add QuartzHelp class library
Add JobDemo. cs to implement the IJob interface.
1 namespace QuartzHelp 2 {3 public class JobDemo: IJob 4 {5 // log object 6 private static readonly ILog logger = LogManager. getLogger (typeof (JobDemo); 7 8 public void Execute (IJobExecutionContext context) 9 {10 logger. info ("JobDemo started to operate, simulating a Ms program"); 11 Thread. sleep (200); 12 logger. info ("JobDemo finished"); 13} 14} 15}View Code
Add WindowControl Console
Add the Service. cs file as the task scheduling entry
1 namespace WindowControl 2 {3 public class Service 4 {5 private readonly ILog logger; 6 private IScheduler schedger; 7 public Service () 8 {9 logger = LogManager. getLogger (typeof (Service); 10 ISchedulerFactory schedulerFactory = new StdSchedulerFactory (); 11 scheduler = schedulerFactory. getScheduler (); 12} 13 14 public void Start () 15 {16 scheduler. start (); 17 logger. info ("Quartz service started successfully"); 18} 19 20 public void Stop () 21 {22 scheduler. shutdown (true); 23 logger. info ("Quartz Service terminated"); 24} 25 26} 27}View Code
In the Program. cs file, start the Task Scheduling
1 namespace WindowControl 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 HostFactory. run (x => 8 {9 x. service <Service> (s) => 10 {11 s. setServiceName ("ser"); 12 s. constructUsing (name => new Service (); 13 s. whenStarted (t) => t. start (); 14 s. whenStopped (t) => t. stop (); 15}); 16 17 x. runAsLocalSystem (); 18 19 // service description 20 x. setDescription ("Task Service Installation test"); 21 // service display name 22 x. setDisplayName ("MyDisplayName"); 23 // service name 24 x. setServiceName ("MyServiceName"); 25 26}); 27} 28} 29}View Code
Add configuration file
Create a new Configs folder, first add quartz_jobs.xml (as the configuration file of the scheduling task)
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 3 <! -- This file contains job definitions in schema version 2.0 format --> 4 5 <job-scheduling-data xmlns =" http://quartznet.sourceforge.net/JobSchedulingData "Xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "Version =" 2.0 "> 6 7 <processing-directives> 8 <! -- Commands and principles to be followed when you plan jobs and triggers --> 9 <overwrite-existing-data> true </overwrite-existing-data> 10 </processing-directives> 11 12 <schedule> 13 <job> 14 <name> myJob </name> 15 <group> myJobGroup </group> 16 <description> first job </description> 17 <job-type> QuartzHelp. jobDemo, quartzHelp </job-type> 18 <durable> true </durable> 19 <recover> false </recover> 20 </job> 21 <trigger> 22 <cron> 23 <name> Trigger </name> 24 <group> TriggerGro Up </group> 25 <description> Simple trigger to simply fire sample job </description> 26 <job-name> myJob </job-name> 27 <job-group> myJobGroup </job-group> 28 <! -- Execute the command once every 10 seconds --> 29 <cron-expression> 0/01 ****? </Cron-expression> 30 </cron> 31 </trigger> 32 </schedule> 33 </job-scheduling-data>View Code
The specific quartz_jobs.xml configuration items, you can refer to: http://www.cnblogs.com/h20064528/archive/2012/07/17/2595636.html
Configure the app. config file
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <configSections> 4 <section name = "quartz" type = "System. configuration. nameValueSectionHandler, System, Version = 1.0.5000.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 "/> 5 <sectionGroup name =" common "> 6 <section name =" logging "type =" Common. logging. configurationSectionHandler, Common. logging "/> 7 </sectionGroup> 8 </configSections> 9 <common> 10 <logging> 11 <factoryAdapter typ E = "Common. Logging. Log4Net. Log4NetLoggerFactoryAdapter, Common. Logging. Log4Net"> 12 <! -- Inline: The log4net node is in the App. config/Web. config file: use an external configuration file (which must be used with the configFile parameter, <arg key = "configFile" value = "external configuration file") file-watch: like "file", it only adds a function to monitor changes to external configuration files. If there is any change, reload the configuration. External: IBatis will not try to configure Log4Net. --> 13 <arg key = "configType" value = "file-watch"/> 14 <! -- Make sure that the folder Configs is under the generated directory --> 15 <arg key = "configFile" value = "~ /Configs/quartz_log4net.config "/> 16 </factoryAdapter> 17 </logging> 18 </common> 19 <quartz> 20 <add key =" quartz. scheduler. instanceName "value =" TaskScheduler "/> 21 <add key =" quartz. scheduler. instanceId "value =" AUTO "/> 22 <add key =" quartz. threadPool. type "value =" Quartz. simpl. simpleThreadPool, Quartz "/> 23 <add key =" quartz. threadPool. threadCount "value =" 5 "/> 24 <add key =" quartz. threadPool. threadPriority "val Ue = "Normal"/> 25 <add key = "quartz. plugin. xml. type" value = "Quartz. Plugin. Xml. XMLSchedulingDataProcessorPlugin, Quartz"/> 26 <! -- Make sure that the Configs folder is under the generated directory --> 27 <add key = "quartz. plugin. xml. fileNames" value = "~ /Configs/quartz_jobs.config "/> 28 <add key =" quartz. plugin. xml. scanInterval "value =" 600 "/> 29 </quartz> 30 <startup> 31 <supportedRuntime version =" v4.0 "sku = ". NETFramework, Version = v4.0 "/> 32 </startup> 33 </configuration>View Code
Finally, configure the quartz_log4net.config file for the diary record. Of course, you can also directly configure it in the app. config file without having to come out independently.
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <log4net> 3 <appender name = "FileAppender" type = "log4net. appender. rollingFileAppender "> 4 <param name =" File "value =" Log \ "/> 5 <param name =" AppendToFile "value =" true "/> 6 <param name = "MaxFileSize" value = "10240"/> 7 <param name = "MaxSizeRollBackups" value = "100"/> 8 <param name = "StaticLogFileName" value = "false"/> 9 <param name = "DatePattern" value = "yyyyMMdd '. log' "/> 10 <param name =" RollingStyle "value =" Date "/> 11 <layout type =" log4net. layout. patternLayout "> 12 <param name =" ConversionPattern "value =" % n log time: % d [% t] % n log level: %-5 p % n log class: % c [% L] % n % m % n "/> 13 </layout> 14 </appender> 15 <appender name =" leleappender "type =" log4net. appender. leleappender "> 16 <layout type =" log4net. layout. patternLayout "> 17 <conversionPattern value =" % 5 level [% thread]-% message % newline "/> 18 </layout> 19 </appender> 20 <root> 21 <level value = "INFO"/> 22 <appender-ref = "leleappender"/> 23 <appender-ref = "FileAppender"/> 24 </root> 25 </ log4net>View Code
Now, run the program and you can see that the task has been started:
Install window service
After toshelfis used, installutil.exe is not used to install the service. In a simple sentence, run the following command directly on the console:WindowControl.exe installThat's all.
Every development process is a progress and accumulation, and every sharing is an opportunity to learn and review. I hope you can share and learn better things together.
How to add the quartznet framework to the aspnet project
There are two DLL references to the project, Quartz. dll, Common. Logging. dll
You can use the task scheduling framework in the project.
C # quartznet configuration file for task scheduling. multiple tasks are triggered at the same time and there are public resources occupied. How can I execute each task in sequence?
What about your project code? Send me a look
1720406057@qq.com