Quartz. Net scheduled task (instance), quartz.net instance

Source: Internet
Author: User

Quartz. Net scheduled task (instance), quartz.net instance

1. What is Quartz. Net?

Quartz. Net is a scheduled task framework.

 

2. Why do I need Quartz. Net if I have Timer?

Quartz. Net is more flexible than Timer. For example, if you do things at on the last Sunday of every month, it will be a lot of trouble to implement Timer.

 

Iii. How to Use Quartz. Net?

I. No configuration implementation

1. Download and reference three DLL files: Quartz. dll, Common. Logging. dll, and Common. Logging. Core. dll.

2. Create a task operation class:

1 public class TimingJob: IJob2 {3 public void Execute (IJobExecutionContext context) 4 {5 // The logic code to be periodically executed is written here 6 Console. writeLine (DateTime. now. toString () + "\ t has been working for too long. Please rest for 10 minutes. "); 7} 8}

3. Create processing class and trigger class

1 class Program 2 {3 static void Main (string [] args) 4 {5 ISchedulerFactory sf = new StdSchedulerFactory (); 6 IScheduler schedfactory = sf. getScheduler (); // create a scheduling instance 7 // create a task instance 8 IJobDetail job = JobBuilder. create <TimingJob> (). withIdentity (new JobKey ("job1 ")). build (); 9 // create a trigger instance 10 ITrigger trigger = TriggerBuilder. create (). startAt (DateTime. now. addSeconds (0 )). withCronSchedule ("59 59 23? * 1L "). Build (); 11 scheduler. ScheduleJob (job, trigger); // bind the trigger and task 12 scheduler. Start (); // Start monitoring 13} 14}

 

II. Implementation through configuration files

1. apply three DLL types: Quartz. dll, Common. Logging. dll, and Common. Logging. Core. dll.

2. Create a work class:

1 public class PrintTimeJob: IJob2 {3 public void Execute (IJobExecutionContext context) 4 {5 Console. WriteLine (DateTime. Now. ToString () + "has been working for a long time. Have a rest! "); 6} 7}

3. configuration file for the work class and trigger class (quartz_jobs.xml)

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <job-scheduling-data xmlns =" http://quartznet.sourceforge.net/JobSchedulingData "Xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "Version =" 2.0 "> 3 <processing-directives> 4 <overwrite-existing-data> true </overwrite-existing-data> 5 </processing-directives> 6 <schedule> <! -- Job configuration --> 7 <job> 8 <name> PrintTimeJob </name> 9 <group> PrintGroup </group> 10 <description> recyclgresourcesjob </description> 11 <job- type> SettingMothod. printTimeJob, SettingMothod </job-type> 12 <durable> true </durable> 13 <recover> false </recover> 14 </job> 15 <trigger> <! -- Trigger configuration --> 16 <cron> 17 <name> PrintDateTimeTrigger </name> 18 <group> PrintDateTimeTriggerGroup </group> 19 <job-name> PrintTimeJob </job-name> 20 <job-group> PrintGroup </job-group> 21 <cron-expression> 59 59 23? * 1L </cron-expression> // 22 </cron> 23 </trigger> 24 </schedule> 25 </job-scheduling-data>

4. Set the configuration file (App. config)

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <configSections> 4 <section name = "quartz" type = "System. configuration. nameValueSectionHandler "/> 5 </configSections> 6 <quartz> 7 <add key =" quartz. scheduler. instanceName "value =" ExampleDefaultQuartzScheduler "/> 8 <add key =" quartz. threadPool. type "value =" Quartz. simpl. simpleThreadPool, Quartz "/> 9 <add key =" quartz. threadPool. threadCount "value =" 10 "/> 10 <add key =" quartz. threadPool. ThreadPriority "value =" 2 "/> 11 <add key =" quartz. jobStore. misfireThreshold "value =" 60000 "/> 12 <add key =" quartz. jobStore. type "value =" Quartz. simpl. RAMJobStore, Quartz "/> 13 <! -- ****************************** Plugin configuration ******* ************************************ --> 14 <add key = "quartz. plugin. xml. type "value =" Quartz. plugin. xml. XMLSchedulingDataProcessorPlugin, Quartz "/> 15 <add key =" quartz. plugin. xml. fileNames "value = "~ /Quartz_jobs.xml "/> 16 </quartz> 17 </configuration>

5. instantiate and start task scheduling.

1 class Program 2 {3 static void Main (string [] args) 4 {5 ISchedulerFactory sf = new StdSchedulerFactory (); 6 IScheduler sched = sf. getScheduler (); // create a scheduling instance 7 sched. start (); // enable scheduling 8 Console. writeLine ("started successfully"); 9 Console. readKey (); 10} 11}

 

If you are writing a blog for the first time, please give me more advice.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.