Quartz. NET management class,

Source: Internet
Author: User

Quartz. NET management class,

I recently designed the project to Quartz. NET and wrote a Quartz. NET management class, which is recorded here.

1 public class QuartzManager <T> where T: class, IJob 2 {3 # region variable 4 private static ISchedulerFactory schedulerFactory = new StdSchedulerFactory (); // scheduler factory 5 private static String JOB_GROUP_NAME = "JOBGROUP_NAME"; // Job group name 6 private static String TRIGGER_GROUP_NAME = "TRIGGERGROUP_NAME "; // trigger group name 7 # endregion 8 9 # add, delete, and modify Job Method 10 /// <summary> 11 /// Add a scheduled task, use the default task group name, trigger name, and trigger group name 1 2 /// </summary> 13 /// <param name = "pStrJobName"> Task name </param> 14 /// <param name = "pStrCronExpress"> trigger expression </param> 15 public static void addJob (string pStrJobName, string pStrCronExpress, IDictionary <string, object> pDictionary) 16 {17 try 18 {19 IScheduler sched = schedulerFactory. getScheduler (); 20 // Create task 21 IJobDetail job = JobBuilder. create <T> () 22. withIdentity (pStrJobName, JOB_GROUP_NAME) 23. Build (); 24 25 // create trigger 26 ITrigger trigger = TriggerBuilder. create () 27. withIdentity (pStrJobName, TRIGGER_GROUP_NAME) 28. withCronSchedule (pStrCronExpress) 29. build (); 30 31 // pass the parameter 32 foreach (KeyValuePair <string, object> kvp in pDictionary) to the task 33 {34 job. jobDataMap. put (kvp. key, kvp. value); 35} 36 37 sched. scheduleJob (job, trigger); 38} 39 catch (Exception e) 40 {41 throw new Exception (e. M Essage); 42} 43} 44 45 // <summary> 46 // remove a task (use the default task group name, trigger name, and trigger group name) 47 // </summary> 48 // <param name = "pStrJobName"> Task name </param> 49 public static void RemoveJob (string pStrJobName) 50 {51 try 52 {53 isched1_sched = schedulerFactory. getScheduler (); 54 JobKey jobKey = new JobKey (pStrJobName); 55 TriggerKey triggerKey = new TriggerKey (pStrJobName, TRIGGER_GROUP_NAME); 56 sched. pauseTri Gger (triggerKey); // stop the trigger 57 sched. unscheduleJob (triggerKey); // remove trigger 58 sched. deleteJob (jobKey); // Delete task 59} 60 catch (Exception e) 61 {62 throw new Exception (e. message); 63} 64} 65 66 // <summary> 67 // modify the trigger time of a task (use the default task group name, trigger name, and trigger group name) 68 // </summary> 69 // <param name = "pStrJobName"> Task name </param> 70 // <param name = "pStrCronExpress"> trigger expression </param> 71 public static void ModifyJobTime (strin G pStrJobName, string pStrCronExpress, IDictionary <string, object> pDictionary) 72 {73 try 74 {75 isched1_sched = schedulerFactory. getScheduler (); 76 TriggerKey triggerKey = new TriggerKey (pStrJobName, TRIGGER_GROUP_NAME); 77 ICronTrigger trigger = (ICronTrigger) sched. getTrigger (triggerKey); 78 if (trigger = null) 79 {80 return; 81} 82 RemoveJob (pStrJobName); 83 addJob (pStrJobName, pStrCronE Xpress, pDictionary); 84} 85 catch (Exception e) 86 {87 throw new Exception (e. message); 88} 89} 90 # endregion 91 92 # region startup, disable Job 93 // <summary> 94 // start all scheduled tasks 95 // </summary> 96 public static void startJobs () 97 {98 try 99 {100 isched1_sched = schedulerFactory. getScheduler (); 101 sched. start (); 102} 103 catch (Exception e) 104 {105 throw new Exception (e. message); 106} 107} 108 109 /// <Summary> 110 // close all scheduled tasks 111 /// </summary> 112 public static void ShutdownJobs () 113 {114 try115 {116 IScheduler sched = schedulerFactory. getScheduler (); 117 if (! Sched. isShutdown) 118 {119 sched. shutdown (); 120} 121} 122 catch (Exception e) 123 {124 throw new Exception (e. message); 125} 126} 127 # endregion128 129}

 

The call method is also very simple.

1 QuartzManager<MyJob>.addJob(jobnName, cronExpress, dictionary);2 3 QuartzManager<MyJob>.startJobs();

 


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.

LuceneNet, Log4Net, and QuartzNet libraries are all ported from Java. Why?

Not without a good class library, Because JAVA is much longer than. NET in history, some are better, and stable direct transplantation is very good. Why repeat the wheel?

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.