Quartz.NET开源作业调度框架系列(三):IJobExecutionContext 参数传递-转

前面写了关于Quartz.NET开源作业调度框架的入门和Cron Trigger , 这次继续这个系列, 这次想讨论一下Quartz.NET中的Job如何通过执行上下文(Execution Contex)进行参数传递 , 有些参数想保存状态该如何处理 . 在Quartz.NET中可以用JobDataMap进行参数传递.本例用Quartz.NET的任务来定期轮询数据库表,当数据库的条目达到一定的数目后,进行预警.(其实可以将读取的表和预警条件配置到数据库中的预警条件表中,这样就可以简单实现一个自动预警提醒的小平台).

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 QuartzDemo
11 {
12
13     public class JobWithParametersExample
14     {
15         public string Name
16         {
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             //2S后执行
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             // 设置初始参数
40             job1.JobDataMap.Put(JobWithParameters.tSQL, "SELECT * FROM [ACT_ID_USER]");
41             job1.JobDataMap.Put(JobWithParameters.ExecutionCount, 1);
42
43             // 设置监听器
44             JobListener listener = new JobListener();
45             IMatcher<JobKey> matcher = KeyMatcher<JobKey>.KeyEquals(job1.Key);
46             sched.ListenerManager.AddJobListener(listener, matcher);
47
48             // 绑定trigger和job
49             sched.ScheduleJob(job1, trigger1);
50             //启动
51             sched.Start();
52
53         }
54     }
55 }

  JobWithParametersExample用来配置job和trigger,同时定义了一个监听器,来监听定义的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     [DisallowConcurrentExecution]
12     public class JobWithParameters : IJob
13     {
14
15         // 定义参数常量
16         public const string tSQL = "tSQL";
17         public const string ExecutionCount = "count";
18         public const string RowCount = "rowCount";
19         public const string tableAlert = "tAlert";
20         //  Quartz 每次执行时都会重新实例化一个类, 因此Job类中的非静态变量不能存储状态信息
21          private int counter = 1;//都为1
22         //private static  int counter = 1;//可以保存状态
23         public virtual void Execute(IJobExecutionContext context)
24         {
25
26             JobKey jobKey = context.JobDetail.Key;
27             // 获取传递过来的参数
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                 try
35                 {
36                     //获取当前Form1实例
37                     __instance = (FrmConsole)Application.OpenForms["FrmConsole"];
38                     //获取当前执行的线程ID
39                     __instance.SetInfo(jobKey + "Thread ID " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
40                     //数据库操作
41                     System.Data.DataTable tAlert = SqlHelper.getDateTable(SQL, null);
42                     //回写条数
43                     data.Put(RowCount, tAlert.Rows.Count);
44                     //通过方法更新消息
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                     //怎么取出Datatable ? json to datatable
52                     //data.Put(tableAlert, tAlert);
53                 }
54                 catch (Exception ex)
55                 {
56                     Console.WriteLine(ex.Message);
57                 }
58             }
59             // 修改执行计数并回写到job data map中
60             count++;
61             data.Put(ExecutionCount, count);
62             // 修改本地变量,如果是非静态变量,不能存储状态
63             counter++;
64         }
65
66         private static FrmConsole __instance = null;
67
68         /// <summary>
69         /// 判断窗体是否打开
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 }

Quartz 每次执行时都会重新实例化一个类, 因此Job类中的非静态变量不能存储状态信息.如何要保存状态信息可以用静态变量进行处理,也可以用参数值进行传入传出来实现.

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 Name
12         {
13             get { return "JobListener"; }
14         }
15
16         public virtual void JobToBeExecuted(IJobExecutionContext inContext)
17         {
18             //执行前执行
19             Console.WriteLine("JobToBeExecuted");
20         }
21
22         public virtual void JobExecutionVetoed(IJobExecutionContext inContext)
23         {
24             //否决时执行
25             Console.WriteLine("JobExecutionVetoed");
26         }
27
28         public virtual void JobWasExecuted(IJobExecutionContext inContext, JobExecutionException inException)
29         {
30             JobKey jobKey = inContext.JobDetail.Key;
31             // 获取传递过来的参数
32             JobDataMap data = inContext.JobDetail.JobDataMap;
33             //获取回传的数据库表条目数
34             int rowCount = data.GetInt(JobWithParameters.RowCount);
35
36             try
37             {
38                 if (rowCount > 9)
39                 {
40                     inContext.Scheduler.PauseAll();
41                     System.Windows.Forms.MessageBox.Show("预警已超9条");
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 效果

时间: 2024-08-04 15:27:33

Quartz.NET开源作业调度框架系列(三):IJobExecutionContext 参数传递-转的相关文章

Quartz.NET开源作业调度框架系列

Quartz.NET是一个被广泛使用的开源作业调度框架 , 由于是用C#语言创建,可方便的用于winform和asp.net应用程序中.Quartz.NET提供了巨大的灵活性但又兼具简单性.开发人员可用它快捷的创建并执行一个自动化作业.Quartz.NET有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等. 针对Quartz.NET的使用,从基础入门,Cron表达式,不同job间进行参数传递进行了介绍,并对插件任务进行了描述,最后将AdoJobStore如何保持到数据库中进行

Quartz.NET开源作业调度框架系列(一):快速入门step by step-转

Quartz.NET是一个被广泛使用的开源作业调度框架 , 由于是用C#语言创建,可方便的用于winform和asp.net应用程序中.Quartz.NET提供了巨大的灵活性但又兼具简单性.开发人员可用它快捷的创建并执行一个自动化作业.Quartz.NET有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等. 1 为什么选择Quartz.NET 在大部分的应用中,都需要对数据库进行定期备份 , 这个备份任务可以是每天晚上12:00或者每周星期二晚上12:00,或许仅仅每个月的最

Quartz.NET开源作业调度框架系列(二):CronTrigger-转

CronTriggers比SimpleTrigger更加的灵活和有用,对于比较复杂的任务触发规则,例如"每个星期天的晚上12:00"进行备份任务,SimpleTrigger就不能胜任,只能选择CronTriggers.利用CronTrigger, 你不但能实现在"每个星期天的晚上12:00"进行备份的任务,还可以执行  "在每个星期一/星期三/星期五的上午9:00到10:00期间每隔5 分钟"进行某个自动化任务. 1 Cron Expressio

Quartz.net开源作业调度框架使用详解(转)

前言 quartz.net作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本.支持 cron-like表达式,集群,数据库.功能性能强大更不用说. 下载项目文档官网:http://www.quartz-scheduler.net/ 项目中需引用:Common.Logging.dll , Common.Logging.Core.dll , Quartz.dll 下面给大家分解下我最近做的关于计划调度的一个小项目,来辅助理解quartz.n

Quartz.net开源作业调度框架使用详解

前言 quartz.net作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本.支持 cron-like表达式,集群,数据库.功能性能强大更不用说. 下载项目文档官网:http://www.quartz-scheduler.net/ 项目中需引用:Common.Logging.dll , Common.Logging.Core.dll , Quartz.dll 下面给大家分解下我最近做的关于计划调度的一个小项目,来辅助理解quartz.n

Quartz.net开源作业调度

Quartz.net开源作业调度框架使用详解 前言 quartz.net作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本.支持 cron-like表达式,集群,数据库.功能性能强大更不用说. 下载项目文档官网:http://www.quartz-scheduler.net/ 项目中需引用:Common.Logging.dll , Common.Logging.Core.dll , Quartz.dll 下面给大家分解下我最近做的关于计

.Net平台开源作业调度框架Quartz.Net

Quartz.NET介绍: Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度.它有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等等. Quartz.Net的cron表达式: 一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素. 字段   允许值   允许的

Quartz.NET开源作业调度架构

Quartz.NET是一个开源的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度.它有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等等. Quick Start 1.项目官网:http://www.quartz-scheduler.net/ 2.建议官网下载压缩包,浏览下源码的大概(里面源码.使

quartz开源作业调度框架的使用

1.官网学习 地址:http://www.quartz-scheduler.org/ 官网上有说明文档及示例,帮助你更好的使用API 2.学习总结 2.1 任务管理(启动,关闭)等操作是依靠调度器Scheduler来管理的,而Scheduler实例是通过工厂Factory获取的,调度器调用了.start()方法,才会让其管理的任务执行 // Grab the Scheduler instance from the Factory Scheduler scheduler = StdSchedule