Quartz. NET open-source job scheduling framework series (III): IJobExecutionContext parameter transfer, C language parameter transfer

Source: Internet
Author: User

Quartz. NET open-source job scheduling framework series (III): IJobExecutionContext parameter transfer, C language parameter transfer

Previously I wrote about Quartz. NET open-source job scheduling framework entry and Cron Trigger, continue this series, this time I want to discuss Quartz. NET Job, how to pass parameters through the Execution context (Execution Contex), how to handle some parameters to save the status. in Quartz. in. NET, JobDataMap can be used to transmit parameters. in this example, Quartz is used. NET tasks to regularly poll database tables. When the number of database entries reaches a certain number, an alert is issued. (In fact, you can configure read tables and warning conditions to the warning condition table in the database, so that you can easily implement a small platform for automatic warning and warning ).

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 {12 13 public class JobWithParametersExample 14 {15 public string Name16 {17 get {return GetType (). name;} 18} 19 private IScheduler sched = null; 20 public JobWithParametersExample (IScheduler _ sched) 21 {22 sched = _ sched; 23} 24 public virtual void Run () 25 {26 27 // after 2 s, run 28 DateTimeOffset startTime = DateBuilder. nextGivenSecondDate (null, 2); 29 IJobDetail job1 = JobBuilder. create <JobWithParameters> () 30. withIdentity ("job1", "group1") 31. build (); 32 33 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 the initial parameter 40 job1.JobDataMap. put (JobWithParameters. tSQL, "SELECT * FROM [ACT_ID_USER]"); 41 job1.JobDataMap. put (JobWithParameters. executionCount, 1); 42 43 // set listener 44 JobListener listener = new JobListener (); 45 IMatcher <JobKey> matcher = KeyMatcher <JobKey>. keyEquals (job1.Key); 46 sched. listenerManager. addJobListener (listener, matcher); 47 48 // bind trigger and job49 sched. scheduleJob (job1, trigger1); 50 // start 51 sched. start (); 52 53} 54} 55}

JobWithParametersExample is used to configure the job and trigger, and a listener is defined to listen to 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 10 [PersistJobDataAfterExecution] 11 [activities] 12 public class JobWithParameters: IJob13 {14 15 // defines the parameter constant 16 public const string tSQL = "tSQL "; 17 public const string ExecutionCount = "count"; 18 public const string RowCount = "r OwCount "; 19 public const string tableAlert =" tAlert "; 20 // every time Quartz is executed, a class will be re-instantiated, therefore, non-static variables in the Job class cannot store status information 21 private int counter = 1; // All are 122 // private static int counter = 1; // save status 23 public virtual void Execute (IJobExecutionContext context) 24 {25 26 JobKey jobKey = context. jobDetail. key; 27 // get the passed parameter 28 JobDataMap data = context. jobDetail. jobDataMap; 29 string SQL = data. getString (tSQL); 30 int Count = data. getInt (ExecutionCount); 31 32 if (isOpen ("FrmConsole") 33 {34 try35 {36 // get the current Form1 instance 37 _ instance = (FrmConsole) Application. openForms ["FrmConsole"]; 38 // gets the currently executed thread ID39 _ instance. setInfo (jobKey + "Thread ID" + System. threading. thread. currentThread. managedThreadId. toString (); 40 // database operation 41 System. data. dataTable tAlert = SqlHelper. getDateTable (SQL, null); 42 // Number of write records 43 data. put (RowCount, tA Lert. rows. count); 44 // update the message by means of 45 _ 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} ", 46 jobKey, 47 tSQL, 48 SQL, 49 tAlert. rows. count, 50 count, counter); 51 // how to retrieve the Datatable? Json to datatable52 // data. put (tableAlert, tAlert); 53} 54 catch (Exception ex) 55 {56 Console. writeLine (ex. message); 57} 58} 59 // modify the execution count and write it back to 60 count ++ in job data map; 61 data. put (ExecutionCount, count); 62 // modify the local variable. If it is a non-static variable, the state of 63 counter ++ cannot be stored; 64} 65 66 private static FrmConsole _ instance = null; 67 68 // <summary> 69 // determine whether the form is opened 70 // </summary> 71 // <param name = "appName"> </param> 72 /// <returns> </returns> 73 private bool isOpen (string appName) 74 {75 FormCollection collection = Application. openForms; 76 foreach (Form form in collection) 77 {78 if (form. name = appName) 79 {80 return true; 81} 82} 83 return false; 84} 85 86} 87}
Every execution of Quartz will re-instantiate a class, so non-static variables in the Job class cannot store status information. to save status information, you can use static variables or parameter values for data transfer.
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 {10 11 public virtual string Name12 {13 get {return "JobListener ";} 14} 15 16 public virtual void JobToBeExecuted (IJobExecutionContext inContext) 17 {18 // run 19 Console before execution. writeLine ("JobToBeExecuted"); 20} 21 22 public virtual void JobExecutionVetoed (IJobExecutionContext inContext) 23 {24 // execute 25 Console upon rejection. writeLine ("JobExecutionVetoed"); 26} 27 28 public virtual void JobWasExecuted (IJobExecutionContext inContext, JobExecutionException inException) 29 {30 JobKey jobKey = inContext. jobDetail. key; 31 // get the passed parameter 32 JobDataMap data = inContext. jobDetail. jobDataMap; 33 // obtain the number of returned database table entries 34 int rowCount = data. getInt (JobWithParameters. rowCount); 35 36 try37 {38 if (rowCount> 9) 39 {40 inContext. scheduler. pauseAll (); 41 System. windows. forms. messageBox. show ("more than 9 alerts"); 42 inContext. scheduler. resumeAll (); 43 44} 45 Console. writeLine (rowCount. toString (); 46} 47 catch (SchedulerException e) 48 {49 50 Console. error. writeLine (e. stackTrace); 51} 52} 53 54} 55}
4. Effect

 

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.