Quartz. NET open-source Job scheduling framework series (4): Plugin Job, quartz. netplugin
What if we want to dynamically modify the binding relationship between the Job and Trigger while the Quartz. NET Job is running and modify some parameters at the same time? Quartz. NET provides plug-in technology. You can configure the Job and Trigger parameters in the XML file, and then regularly load the configuration file to instantiate the task and Trigger. This solves this problem.
1 PlugInJobExample
using System;
using System.Collections.Specialized;
using System.Threading;
using Common.Logging;
using Quartz;
using Quartz.Impl;
using Quartz.Job;
using System.Windows.Forms;
namespace QuartzDemo
{
public class PlugInJobExample
{
public string Name
{
get {return GetType (). Name;}
}
public virtual IScheduler Run ()
{
var properties = new NameValueCollection ();
properties ["quartz.plugin.triggHistory.type"] = "Quartz.Plugin.History.LoggingJobHistoryPlugin";
properties ["quartz.plugin.jobInitializer.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin";
// Configuration file name
properties ["quartz.plugin.jobInitializer.fileNames"] = "quartz_jobs.xml";
properties ["quartz.plugin.jobInitializer.failOnFileNotFound"] = "true";
// Proceed every 120 seconds to see if the configuration file has changed
properties ["quartz.plugin.jobInitializer.scanInterval"] = "120";
// Instantiate an ISchedulerFactory with a propertytie defined by the plugin XML
ISchedulerFactory sf = new StdSchedulerFactory (properties);
IScheduler sched = sf.GetScheduler ();
//start up
sched.Start ();
//return
return sched;
}
}
}
2 SimpleJob1
1 using System;
2 using System.Collections.Generic;
3
4 using Common.Logging;
5 using Quartz;
6 using Quartz.Impl;
7 using Quartz.Job;
8 using System.Windows.Forms;
9 namespace QuartzDemo
10 {
11
12 public class SimpleJob1: IJob
13 {
14
15 public virtual void Execute (IJobExecutionContext context)
16 {
17 JobKey jobKey = context.JobDetail.Key;
18 if (isOpen ("FrmConsole"))
19 {
20 try
twenty one {
22 // Get the current Form1 instance
23 __instance = (FrmConsole) Application.OpenForms ["FrmConsole"];
24 // Get the thread ID of the current execution
25 __instance.SetInfo ("-" + jobKey + "Thread ID" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString ());
26
27 // Update message through method
28 __instance.SetInfo (string.Format ("-{0} exec at {1}",
29 jobKey,
30 System.DateTime.Now.ToString ()));
31
32}
33 catch (Exception ex)
34 {
35 Console.WriteLine (ex.Message);
36}
37}
38 // This job simply prints out its job name and the
39
40 if (context.MergedJobDataMap.Count> 0)
41 {
42 ICollection <string> keys = context.MergedJobDataMap.Keys;
43 foreach (string key in keys)
44 {
45 String val = context.MergedJobDataMap.GetString (key);
46 __instance.SetInfo (string.Format ("-MergedJobDataMap entry: {0} = {1}", key, val));
47
48}
49}
50 context.Result = "exec ok";
51}
52
53 private static FrmConsole __instance = null;
54
55 /// <summary>
56 /// determine if the form is open
57 /// </ summary>
58 /// <param name = "appName"> </ param>
59 /// <returns> </ returns>
60 private bool isOpen (string appName)
61 {
62 FormCollection collection = Application.OpenForms;
63 foreach (Form form in collection)
64 {
65 if (form.Name == appName)
66 {
67 return true;
68}
69}
70 return false;
71}
72}
73}
3. xml configuration file
The first is simple Trigger configuration, and the second is CronTrigger:
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2
3 <job-scheduling-data xmlns = "http://quartznet.sourceforge.net/JobSchedulingData"
4 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
5 version = "2.0">
6
7 <processing-directives>
8 <overwrite-existing-data> true </ overwrite-existing-data>
9 </ processing-directives>
10
11 <schedule>
12
13 <job>
14 <name> jobName1 </ name>
15 <group> jobGroup1 </ group>
16 <description> jobDesciption1 </ description>
17 <job-type> QuartzDemo.SimpleJob1, QuartzDemo </ job-type>
18 <durable> true </ durable>
19 <recover> false </ recover>
20 <job-data-map>
21 <entry>
22 <key> key0 </ key>
23 <value> value0 </ value>
24 </ entry>
25 <entry>
26 <key> key1 </ key>
27 <value> value1 </ value>
28 </ entry>
29 <entry>
30 <key> key2 </ key>
31 <value> value2 </ value>
32 </ entry>
33 </ job-data-map>
34 </ job>
35
36 <trigger>
37 <simple>
38 <name> simpleName </ name>
39 <group> simpleGroup </ group>
40 <description> SimpleTriggerDescription </ description>
41 <job-name> jobName1 </ job-name>
42 <job-group> jobGroup1 </ job-group>
43 <start-time> 2015-12-02T10: 15: 00.0Z </ start-time>
44 <end-time> 2020-05-04T18: 13: 51.0Z </ end-time>
45 <misfire-instruction> SmartPolicy </ misfire-instruction>
46 <repeat-count> 100 </ repeat-count>
47 <repeat-interval> 1000 </ repeat-interval>
48 </ simple>
49 </ trigger>
50
51 </ schedule>
52
53 </ job-scheduling-data>
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 version="2.0">
6
7 <processing-directives>
8 <overwrite-existing-data>true</overwrite-existing-data>
9 </processing-directives>
10 <schedule>
11 <job>
12 <name>SimpleJob1</name>
13 <group>myJobGroup1</group>
14 <description>SimpleJob1</description>
15 <job-type>QuartzDemo.SimpleJob1, QuartzDemo</job-type>
16 <durable>true</durable>
17 <recover>false</recover>
18 <job-data-map>
19 <entry>
20 <key>author</key>
21 <value>jackwangcumt</value>
22 </entry>
23 <entry>
24 <key>blog</key>
25 <value>isaboy</value>
26 </entry>
27 <entry>
28 <key>jobType</key>
29 <value>XML Plugin Job</value>
30 </entry>
31 </job-data-map>
32 </job>
33 <trigger>
34 <cron>
35 <name>trigger1</name>
36 <group>myTriggerGroup</group>
37 <job-name>SimpleJob1</job-name>
38 <job-group>myJobGroup1</job-group>
39 <cron-expression>0/2 * * * * ?</cron-expression>
40 </cron>
41 </trigger>
42 </schedule>
43 </job-scheduling-data>
4. Effect
(It can be opened on another tab to see images without compression)