Quartz.net Open Source Job scheduling framework Series (iii): Ijobexecutioncontext parameter Transfer-Go

Source: Internet
Author: User
Tags rowcount

In front of the introduction to the Quartz.net Open source job scheduling framework and Cron Trigger, this time continue this series, this time to discuss how the job in quartz.net through the execution context (execution Contex) for parameter passing, Some parameters want to save the state what to do with it. You can use Jobdatamap for parameter passing in Quartz.net. This example uses the task of Quartz.net to poll the database table periodically, and when the database entry reaches a certain number, it is alerted. (You can actually configure the read table and the alert conditions in the alert criteria table in the database, so that you can easily implement an automatic alert alert for the platform).

1 jobwithparametersexample
 1 using System; 2 using System.Threading; 3 4 using Common.logging; 5 using Quartz; 6 using Quartz.impl; 7 using Quartz.job; 8 using Quartz.Impl.Calendar;         9 using quartz.impl.matchers;10 namespace QuartzDemo11 {public class Jobwithparametersexample 14 {15 public string Name16 {+ Get {return GetType (). Name;         }18}19 private IScheduler sched = null;20 public jobwithparametersexample (IScheduler _sched) 21             {sched = _sched;23}24 public virtual void Run () 25 {26 27 2S after execution DateTimeOffset startTime = Datebuilder.nextgivenseconddate (null, 2); Ijobdetail J Ob1 = jobbuilder.create<jobwithparameters> () 30. Withidentity ("Job1", "group1") 31.                                                            Build (); Isimpletrigger trigger1 = (Isimpletrigger) triggerbuilder.create () 34 . WithidEntity ("Trigger1", "group1") 35. StartAt (StartTime) 36. Withsimpleschedule (x = X.withintervalinseconds (5). Withrepeatcount (100)) 37. Build (); 38 39//Set initial parameter job1. Jobdatamap.put (Jobwithparameters.tsql, "select * from [Act_id_user]"); Job1. Jobdatamap.put (Jobwithparameters.executioncount, 1); 42 43//Set listener Joblistener listener = new Jo Blistener (); imatcher<jobkey> matcher = Keymatcher<jobkey>. Keyequals (JOB1. Key); Sched. Listenermanager.addjoblistener (Listener, Matcher); 47 48//BIND trigger and Job49 sched. Schedulejob (JOB1, trigger1); 50//Start Sched. Start (); 52 53}54}55}

Jobwithparametersexample is used to configure the job and trigger, and a listener is defined to listen for the defined job.

2 Jobwithparameters
 1 using System; 2 using Common.logging; 3 using Quartz; 4 using Quartz.impl; 5 using Quartz.job; 6 using System.Windows.Forms; 7 namespace Quartzdemo 8 {9 [persistjobdataafterexecution]11 [Disallowconcurrentexecution]12 public class JOBWITHPARAMETERS:IJOB13 {14 15//define parameter constants-Public const string TSQL = "TSQL"; Const string Executioncount = "Count"; public const string RowCount = "RowCount"; Tablealert = "Talert"; //Quartz A class is re-instantiated each time it executes, so a non-static variable in the job class cannot store state information. private int counter = 1;//122//private            The static int counter = 1;//can save the state of the virtual void Execute (Ijobexecutioncontext context) 24 {25 Jobkey Jobkey = context. JOBDETAIL.KEY;27//Get the parameters passed over jobdatamap data = context. jobdetail.jobdatamap;29 string SQL = data. GetString (tSQL); int count = data.GetInt (Executioncount), if (IsOpen ("Frmconsole")) (try35 {36//Get current Form1 instance PNs __instance = (frmconsole) application.openforms["Frmconsole"]; 38//Gets the currently executing thread ID39 __instance. SetInfo (Jobkey + "Thread ID" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString ()); 40/ /database operation System.Data.DataTable Talert = sqlhelper.getdatetable (SQL, NULL); 42//write-back Bar Count data. Put (RowCount, TAlert.Rows.Count); 44//Update the message via method __instance. SetInfo (String. Format ("{0} exec {1} = {2} get {3} rows\r\n execution count (from job map) is {4}\r\n execution count                      (from job member variable) is {5} ", jobkey,47 tsql,48 sql,49             talert.rows.count,50        Count, counter)); 51//How do I remove a DataTable? JSON to datatable52//data.            Put (Tablealert, Talert); }54 catch (Exception ex) @ Console.WriteLine (ex .             Message); 57}58}59//Modify execution count and write back to job data map in count++;61 Data.            Put (Executioncount, Count); 62//Modify local variable, if non-static variable, cannot store state counter++;64}65-a private static Frmconsole __ins Tance = null;67///<summary>69//Determine if the form is open///</summary>71///&L T;param name= "AppName" ></param>72//<returns></returns>73 private bool IsOpen (Strin G AppName) (formcollection collection = application.openforms;76 foreach (Form form I N collection) (form. Name = = AppName) (true;81 return}82}83 return false;84 }85 86}87}
Quartz A class is re-instantiated each time it executes, so non-static variables in the job class cannot store state information. How to save state information can be handled with static variables, or you can use parameter values for incoming and outgoing implementations.
3 Joblistener
 1 using System; 2 using Common.logging; 3 using Quartz; 4 using Quartz.impl; 5 using Quartz.job; 6 namespace Quartzdemo 7 {8 public class Joblistener:ijoblistener 9 {Ten public virtual string Nam E12 {get {return ' Joblistener ';} }15 public virtual void jobtobeexecuted (Ijobexecutioncontext incontext) 17 {18// Executed before execution of Console.WriteLine ("jobtobeexecuted");}21 public virtual void jobexecutionvetoed (          Ijobexecutioncontext Incontext) 23 {24//veto when executed at Console.WriteLine ("Jobexecutionvetoed"); 26 }27 public virtual void jobwasexecuted (Ijobexecutioncontext incontext, Jobexecutionexception inexcepti             On) {Jobkey Jobkey = incontext.jobdetail.key;31//Get passed over parameter 32 Jobdatamap data = incontext.jobdetail.jobdatamap;33//Gets the number of database table entries for callbacks. int rowCount = datA.getint (Jobwithparameters.rowcount), try37 (RowCount > 9) 39 {inContext.Scheduler.PauseAll (); System.Windows.Forms.MessageBox.Sh                 OW ("Warning has been over 9"); InContext.Scheduler.ResumeAll (); 43 44}45 Console.WriteLine (Rowcount.tostring ());}47 catch (Schedulerexception e) 48 {4 9 Console.Error.WriteLine (E.stacktrace); 51}52}53 54}55}
4 effects

Quartz.net Open Source Job scheduling framework Series (iii): Ijobexecutioncontext parameter Transfer-Go

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.