星空网 > 软件开发 > ASP.net

Quartz.NET开源作业调度框架系列(四):Plugin Job

  如果在Quartz.NET作业运行时我们想动态修改Job和Trigger的绑定关系,同时修改一些参数那么该怎么办呢?Quartz.NET提供了插件技术,可以通过在

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.";      //配置文件名称      properties["quartz.plugin.jobInitializer.fileNames"] = "quartz_jobs.";      properties["quartz.plugin.jobInitializer.failOnFileNotFound"] = "true";      //每隔120秒进行探查,看配置文件是否更改      properties["quartz.plugin.jobInitializer.scanInterval"] = "120";      // 用插件      ISchedulerFactory sf = new StdSchedulerFactory(properties);      IScheduler sched = sf.GetScheduler();      //启动      sched.Start();      //返回      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 QuartzDemo10 {11 12   public class SimpleJob1 : IJob13   {14    15     public virtual void Execute(IJobExecutionContext context)16     {17       JobKey jobKey = context.JobDetail.Key;18       if (isOpen("FrmConsole"))19       {20         try21         {22           //获取当前Form1实例23           __instance = (FrmConsole)Application.OpenForms["FrmConsole"];24           //获取当前执行的线程ID25           __instance.SetInfo(" - "+jobKey + "Thread ID " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());26      27           //通过方法更新消息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 the39 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     /// 判断窗体是否打开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

下面第一个是简单的Trigger配置,第二个是用CronTrigger:

 1 <??> 2  3 <job-scheduling-data ="http://quartznet.sourceforge.net/JobSchedulingData" 4     ="http://www.w3.org/2001/ 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 <??> 2  3 <job-scheduling-data ="http://quartznet.sourceforge.net/JobSchedulingData" 4     ="http://www.w3.org/2001/ 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></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 效果

Quartz.NET开源作业调度框架系列(四):Plugin Job

 (可以将下图在另一个页签打开,看无压缩GIF图片)

Quartz.NET开源作业调度框架系列(四):Plugin Job




原标题:Quartz.NET开源作业调度框架系列(四):Plugin Job

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流