A good head is worse than a rotten pen.-quartz Use Summary

Source: Internet
Author: User

Quartz is an open-source job scheduling framework for the Java platform. Quartz.net is a convenient time to use Quartz to perform scheduled tasks such as batch processing, from a Java version to a. NET version of a. NET project.

(1) Quartz.net can be installed from NuGet

(2) Quartz configuration:

<configsections>  < Sectionname= "Quartz"type= "System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,culture=neutral, publickeytoken= b77a5c561934e089 "/></configsections><Quartz>  <AddKey= "Quartz.scheduler.instanceName"value= "Exampledefaultquartzscheduler"/>  <AddKey= "Quartz.threadPool.type"value= "Quartz.Simpl.SimpleThreadPool, Quartz"/>  <AddKey= "Quartz.threadPool.threadCount"value= "Ten"/>  <AddKey= "Quartz.threadPool.threadPriority"value= "2"/>  <AddKey= "Quartz.jobStore.misfireThreshold"value= "60000"/>  <AddKey= "Quartz.jobStore.type"value= "Quartz.Simpl.RAMJobStore, Quartz"/></Quartz><appSettings>  <!--cronexpression expression: Starts at the 2nd second of every minute and executes 5 seconds every interval -  <AddKey= "cronexpr"value= "2/5 * * * * *?"/></appSettings>

(3) Create a generic class to implement the Quartz.ijob interface

The interface is simple, there is only one execute () method (as in Java), in which you write the processing logic you want to do.

 Public class myjob:quartz.ijob{    publicvoid  Execute (quartz.ijobexecutioncontext context)    {        //  your processing logic, i.e. "work"        Console.WriteLine (DateTime.Now);    }}

(4) Start job scheduling

Using Quartz;
Using Quartz.impl;
Using Quartz.Impl.Triggers;

classprogram{Static voidMain (string[] args) {                    //Initializes a new instance of the Quartz.Impl.StdSchedulerFactory class.Ischedulerfactory SF =NewQuartz.Impl.StdSchedulerFactory (); //Returns a client-usable handle to a quartz.ischeduler.IScheduler sched =SF.            Getscheduler (); //define a job (i.e. your processing logic, i.e. "work")Ijobdetail job =NewJobdetailimpl ("Job1","group1",typeof(MyJob)); //defining triggers (reading appsettings)            stringcronexpr = configurationmanager.appsettings["cronexpr"]; Itrigger Trigger=NewCrontriggerimpl ("Trigger1","group1","Job1","group1", cronexpr); //add a given job to the schedulerSched. AddJob (Job,true,true); //assigning a trigger to a jobSched.            Schedulejob (trigger); //start the Scheduler threadSched.            Start ();    Console.read (); }}

I am here a console program. For a Web program or service program, when Application_End, you need to call Scheduler's shutdown () method to turn off Quartz's work.

Note that the IScheduler has two overloaded AddJob methods:

namespacequartz{ Public InterfaceIScheduler {//        //Summary://Add The given quartz.ijob to the Scheduler-with no associated quartz.itrigger. //the quartz.ijob'll be ' dormant ' until it's scheduled with a Quartz.itrigger,//or Quartz.IScheduler.TriggerJob (Quartz.jobkey) is called for it. //        //remark://the quartz.ijob must by definition being ' durable ', if it is not, schedulerexception//would be thrown.        voidAddJob (Ijobdetail Jobdetail,BOOLreplace); //        //Summary://Add The given quartz.ijob to the Scheduler-with no associated quartz.itrigger. //the quartz.ijob'll be ' dormant ' until it's scheduled with a Quartz.itrigger,//or Quartz.IScheduler.TriggerJob (Quartz.jobkey) is called for it. //        //remark://with the storenondurablewhileawaitingscheduling parameter set to True, a//non-durable job can be stored. Once It is scheduled, it would resume normal//non-durable behavior (i.e. be deleted once there is no remaining associated//triggers).        voidAddJob (Ijobdetail Jobdetail,BOOLReplaceBOOLstorenondurablewhileawaitingscheduling); }}

Above the addjob to call void AddJob (Ijobdetail jobdetail, BOOL Replace, BOOL storenondurablewhileawaitingscheduling); Set the torenondurablewhileawaitingscheduling parameter to True. Otherwise, the Schedulerexception exception is thrown:

quartz.schedulerexception HResult not processed=-2146233088Message=Jobs added with no trigger must is durable. Source=Quartz StackTrace: In Quartz.Core.QuartzScheduler.AddJob (Ijobdetail jobdetail, Boolean replace, Boolean Storenon durablewhileawaitingscheduling) Location C:\Program Files (x86) \jenkins\workspace\quartz.net\src\quartz\core\ QuartzScheduler.cs: Line number788in Quartz.Core.QuartzScheduler.AddJob (Ijobdetail jobdetail, Boolean replace) location C:\Program Files (x86) \jenkins\w Orkspace\quartz.net\src\quartz\core\quartzscheduler.cs: Line number779in Quartz.Impl.StdScheduler.AddJob (Ijobdetail jobdetail, Boolean replace) location C:\Program Files (x86) \jenkins\work Space\quartz.net\src\quartz\impl\stdscheduler.cs: Line number286in UnitTest.Program.Main (string[] args) position D:\SourceProject\infrastructure.sms\trunk\UnitTest\Program.cs: line number  $In system.appdomain._nexecuteassembly (runtimeassembly Assembly, string[] args) in System.AppDomain.ExecuteA ssembly (String assemblyfile, Evidence assemblysecurity, string[] args) in Microsoft.VisualStudio.HostingProcess.HostP Roc. Runusersassembly () in System.Threading.ThreadHelper.ThreadStart_Context (Object state) in System.Threading.Execut Ioncontext.runinternal (ExecutionContext ExecutionContext, ContextCallback callback, Object State, Boolean PRESERVESYNCCTX) in System.Threading.ExecutionContext.Run (ExecutionContext executioncontext, ContextCallback Callbac  K, Object State, Boolean Preservesyncctx) in System.Threading.ExecutionContext.Run (ExecutionContext ExecutionContext, ContextCallback callback, Object State) in System.Threading.ThreadHelper.ThreadStart ()

(5) Next, you can allow the program to see the effect.

About the cronexpression expression for quartz:

Common examples:

0 0 12 * *? 12-point trigger per day

0 15 10? * * Daily 10:15 Trigger

0 15 10 * *? Triggered 10:15 daily

0 15 10 * *? * Triggered 10:15 Daily

0 15 10 * *? 2005 2005 Daily 10:15 Trigger

0 * 14 * *? Every afternoon from 2 to 2:59 per minute.

0 0/5 14 * *? Every afternoon from 2 to 2:59 (the whole point starts, every 5 minutes trigger)

0 0/5 14,18 * *? Every afternoon from 2 to 2:59, 18 to 18:59 (the whole point starts, every 5 points trigger)

0 0-5 14 * *? Every afternoon from 2 to 2:05 per minute.

0 10,44 14? 3 WED March every Wednesday pm 2:10 and 2:44 Trigger

0 15 10? * Mon-fri from Monday to Friday every morning 10:15

0 15 10 15 *? 15th 10:15 A.M. per month, trigger

0 L *? 10:15 trigger on the last day of the month

0 15 10? * 6L of the last week of the month of Friday 10:15 Trigger

0 15 10? * 6L 2002-2005 triggered from 2002 to 2005, last week of Friday 10:15

0 15 10? * 6#3 starts in Friday of the third week of the month

0 0 12 1/5 *? The first noon of each month starts every 5 days

0 11 11 11 11? Every November 11 11:11 trigger (Singles Day)

A good head is worse than a rotten pen.-quartz Use Summary

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.