前面写了关于Quartz.NET开源作业调度框架的入门和Cron Trigger , 这次继续这个系列, 这次想讨论一下Quartz.NET中的Job如何通过执行上下文(Execution Contex)进行参数传递 , 有些参数想保存状态该如何处理 . 在Quartz.NET中可以用JobDataMap进行参数传递.本例用Quartz.NET的任务来定期轮询数据库表,当数据库的条目达到一定的数目后,进行预警.(其实可以将读取的表和预警条件配置到数据库中的预警条件表中,这样就可以简单实现一个自动预警提醒的小平台).
1 JobWithParametersExample
using System;
using System.Threading; using Common.Logging;
using Quartz;
using Quartz.Impl;
using Quartz.Job;
using Quartz.Impl.Calendar;
using Quartz.Impl.Matchers;
namespace QuartzDemo
{ public class JobWithParametersExample
{
public string Name
{
get { return GetType().Name; }
}
private IScheduler sched = null;
public JobWithParametersExample(IScheduler _sched)
{
sched = _sched;
}
public virtual void Run()
{ //2S后执行
DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(null, );
IJobDetail job1 = JobBuilder.Create<JobWithParameters>()
.WithIdentity("job1", "group1")
.Build(); ISimpleTrigger trigger1 = (ISimpleTrigger)TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartAt(startTime)
.WithSimpleSchedule(x => x.WithIntervalInSeconds().WithRepeatCount())
.Build(); // 设置初始参数
job1.JobDataMap.Put(JobWithParameters.tSQL, "SELECT * FROM [ACT_ID_USER]");
job1.JobDataMap.Put(JobWithParameters.ExecutionCount, ); // 设置监听器
JobListener listener = new JobListener();
IMatcher<JobKey> matcher = KeyMatcher<JobKey>.KeyEquals(job1.Key);
sched.ListenerManager.AddJobListener(listener, matcher); // 绑定trigger和job
sched.ScheduleJob(job1, trigger1);
//启动
sched.Start(); }
}
}
JobWithParametersExample用来配置job和trigger,同时定义了一个监听器,来监听定义的job.
2 JobWithParameters
using System;
using Common.Logging;
using Quartz;
using Quartz.Impl;
using Quartz.Job;
using System.Windows.Forms;
namespace QuartzDemo
{ [PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class JobWithParameters : IJob
{ // 定义参数常量
public const string tSQL = "tSQL";
public const string ExecutionCount = "count";
public const string RowCount = "rowCount";
public const string tableAlert = "tAlert";
// Quartz 每次执行时都会重新实例化一个类, 因此Job类中的非静态变量不能存储状态信息
private int counter = ;//都为1
//private static int counter = 1;//可以保存状态
public virtual void Execute(IJobExecutionContext context)
{ JobKey jobKey = context.JobDetail.Key;
// 获取传递过来的参数
JobDataMap data = context.JobDetail.JobDataMap;
string SQL = data.GetString(tSQL);
int count = data.GetInt(ExecutionCount); if (isOpen("FrmConsole"))
{
try
{
//获取当前Form1实例
__instance = (FrmConsole)Application.OpenForms["FrmConsole"];
//获取当前执行的线程ID
__instance.SetInfo(jobKey + "Thread ID " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
//数据库操作
System.Data.DataTable tAlert = SqlHelper.getDateTable(SQL, null);
//回写条数
data.Put(RowCount, tAlert.Rows.Count);
//通过方法更新消息
__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}",
jobKey,
tSQL,
SQL,
tAlert.Rows.Count,
count, counter));
//怎么取出Datatable ? json to datatable
//data.Put(tableAlert, tAlert);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// 修改执行计数并回写到job data map中
count++;
data.Put(ExecutionCount, count);
// 修改本地变量,如果是非静态变量,不能存储状态
counter++;
} private static FrmConsole __instance = null; /// <summary>
/// 判断窗体是否打开
/// </summary>
/// <param name="appName"></param>
/// <returns></returns>
private bool isOpen(string appName)
{
FormCollection collection = Application.OpenForms;
foreach (Form form in collection)
{
if (form.Name == appName)
{
return true;
}
}
return false;
} }
}
Quartz 每次执行时都会重新实例化一个类, 因此Job类中的非静态变量不能存储状态信息.如何要保存状态信息可以用静态变量进行处理,也可以用参数值进行传入传出来实现.
3 JobListener
using System;
using Common.Logging;
using Quartz;
using Quartz.Impl;
using Quartz.Job;
namespace QuartzDemo
{
public class JobListener : IJobListener
{ public virtual string Name
{
get { return "JobListener"; }
} public virtual void JobToBeExecuted(IJobExecutionContext inContext)
{
//执行前执行
Console.WriteLine("JobToBeExecuted");
} public virtual void JobExecutionVetoed(IJobExecutionContext inContext)
{
//否决时执行
Console.WriteLine("JobExecutionVetoed");
} public virtual void JobWasExecuted(IJobExecutionContext inContext, JobExecutionException inException)
{
JobKey jobKey = inContext.JobDetail.Key;
// 获取传递过来的参数
JobDataMap data = inContext.JobDetail.JobDataMap;
//获取回传的数据库表条目数
int rowCount = data.GetInt(JobWithParameters.RowCount); try
{
if (rowCount > )
{
inContext.Scheduler.PauseAll();
System.Windows.Forms.MessageBox.Show("预警已超9条");
inContext.Scheduler.ResumeAll(); }
Console.WriteLine(rowCount.ToString());
}
catch (SchedulerException e)
{ Console.Error.WriteLine(e.StackTrace);
}
} }
}