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

时间:2023-03-08 16:11:33
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调度器作为框架,为我们提供基础服务。 Quartz.NET允许开发人员根据时间间隔(或天)来调度作业 , 它实现了作业和触发器的多对多关系,还能把多个作业与不同的触发器关联。整合了 Quartz.NET的应用程序可以重用来自不同事件的作业,还可以为一个事件组合多个作业.

2 如何创建一个简单的Quartz.NET

2.1 创建桌面应用程序并添加类库

  用VS2012创建一个QuartzDemo的桌面应用程序 , 并用NuGet程序包管理添加Quartz.NET , 添加好类库后 , 项目文件如下图所示:

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

  Quartz依赖库为Common.Logging和Common.Logging.Core  ,  二者需要一并导入 . 然后修改Form1的标题为QuartzDemo.

2.2 Form1定制

  在Form1设计视图上 , 为该窗体添加一个TextBox和Panel , 并设置相关属性(背景色,字体等) , 如下图所示:

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

  为了直观的执行定期任务 , 我们添加一个时序图(用Oxyplot控件) , 这里下载并引用Oxyplot.在Form1.cs文件中  ,首先引入需要的命名空间:

Quartz.NET开源作业调度框架系列(一):快速入门step by step-转
1     using Quartz;
2 using Quartz.Impl;
3 using Quartz.Job;
4 using System.Threading;
5
6 using OxyPlot;
7 using OxyPlot.Axes;
8 using OxyPlot.Series;
Quartz.NET开源作业调度框架系列(一):快速入门step by step-转

  重载一个带参数的Form1构造方法:

Quartz.NET开源作业调度框架系列(一):快速入门step by step-转
 1         private bool isFirst = true;
2 public Form1(string msg)
3 {
4 InitializeComponent();
5 _msg = msg;
6 try
7 {
8 //启动 scheduler
9 scheduler.Start();
10 // 定义一个job并和自定义的HelloJob进行绑定
11 IJobDetail job = JobBuilder.Create<HelloJob>()
12 .WithIdentity("HelloJob", "SimpleGroup")
13 .Build();
14 #region Cron Expressions
15 // ITrigger trigger2 = TriggerBuilder.Create()
16 //.WithIdentity("trigger3", "group1")
17 //.WithCronSchedule(" 0 0/5 * * * ?", x => x
18 // .WithMisfireHandlingInstructionFireAndProceed())
19 //.ForJob("job1", "group1")
20 //.Build();
21 #endregion
22
23 //定义一个即时触发的触发器,(每隔1秒进行重复执行)
24 ITrigger trigger= TriggerBuilder.Create()
25 .WithIdentity("trigger1", "SimpleGroup")
26 .StartNow()
27 .WithSimpleSchedule(x => x
28 .WithIntervalInSeconds(1)
29 .RepeatForever())
30 .Build();
31 // 将job和trigger进行绑定,并告知 quartz 调度器用trigger去执行job
32 scheduler.ScheduleJob(job, trigger);
33 }
34 catch (SchedulerException se)
35 {
36 Console.WriteLine(se);
37 }
38
39 }
Quartz.NET开源作业调度框架系列(一):快速入门step by step-转

  在Form1的FormClosed事件,即窗体关闭后,将scheduler关闭:

1         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
2 {
3 //关闭 scheduler
4 scheduler.Shutdown();
5 }

  在窗体加载时,创建一个时序图:

Quartz.NET开源作业调度框架系列(一):快速入门step by step-转
 1         public OxyPlot.WindowsForms.PlotView  Plot;
2 private LineSeries lineSeries = new LineSeries { Title = "即时监控(1秒)", StrokeThickness =2 };
3 private void Form1_Load(object sender, EventArgs e)
4 {
5 Plot = new OxyPlot.WindowsForms.PlotView();
6 Plot.Model = new PlotModel();
7 Plot.Dock = DockStyle.Fill;
8 this.panel1.Controls.Add(Plot);
9
10 Plot.Model.PlotType = PlotType.XY;
11 Plot.Model.Background = OxyColor.FromRgb(255, 255, 255);
12 Plot.Model.TextColor = OxyColor.FromRgb(0, 0, 0);
13
14 // add Series and Axis to plot model
15 Plot.Model.Series.Add(lineSeries);
16 Plot.Model.Axes.Add(new LinearAxis());
17 }
Quartz.NET开源作业调度框架系列(一):快速入门step by step-转

  定义一个SetMsg方法来更新消息:

Quartz.NET开源作业调度框架系列(一):快速入门step by step-转
 1         private double xInit = 0;
2 public bool SetMsg(string msg)
3 {
4 _msg = msg;
5 //号称NET4最简单的跨进程更新UI的方法
6 this.Invoke((MethodInvoker)delegate
7 {
8 // runs on UI thread
9 if (isFirst)
10 {
11 this.txtLog.AppendText("Hello to Quartz NET ! Created by JackWang 2015");
12 isFirst = false;
13 }
14 this.txtLog.AppendText(string.Format("\r\n$JackWang>> You get {0} message from Quartz MyJob...", _msg));
15 xInit = xInit + 1;
16 lineSeries.Points.Add(new DataPoint(xInit,double.Parse(_msg)));
17 if (lineSeries.Points.Count > 50)
18 {
19 //保留最近50个点
20 lineSeries.Points.RemoveAt(0);
21 }
22 //更新图表数据
23 this.Plot.Model.InvalidatePlot(true);
24
25 });
26 return true;
27
28 }
Quartz.NET开源作业调度框架系列(一):快速入门step by step-转

2.3 定义一个HelloJob

  在带参数的Form1构造方法中 , 创建的HelloJob的定义为:

Quartz.NET开源作业调度框架系列(一):快速入门step by step-转
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 namespace QuartzDemo
7 {
8 using Quartz;
9 using Quartz.Impl;
10 using Quartz.Job;
11 public class HelloJob : IJob
12 {
13 private static Form1 __instance = null;
14 public void Execute(IJobExecutionContext context)
15 {
16
17 if (isOpen("Form1"))
18 {
19 //获取当前Form1实例
20 __instance = (Form1)Application.OpenForms["Form1"];
21 //随机生成小于100的数
22 string num = new Random().Next(100).ToString();
23 //通过方法更新消息
24 __instance.SetMsg(num);
25 }
26 else
27 {
28 //__instance = new Form1("0");
29 //__instance.Show();
30
31 }
32
33 }
34 /// <summary>
35 /// 判断窗体是否打开
36 /// </summary>
37 /// <param name="appName"></param>
38 /// <returns></returns>
39 private bool isOpen(string appName)
40 {
41 FormCollection collection = Application.OpenForms;
42 foreach (Form form in collection)
43 {
44 if (form.Name == appName)
45 {
46 return true;
47 }
48 }
49 return false;
50 }
51 }
52 }
Quartz.NET开源作业调度框架系列(一):快速入门step by step-转

  注意修改Program.cs中用带参数的Form1构建方法进行实例创建:

Quartz.NET开源作业调度框架系列(一):快速入门step by step-转
1         [STAThread]
2 static void Main()
3 {
4 Application.EnableVisualStyles();
5 Application.SetCompatibleTextRenderingDefault(false);
6 Application.Run(new Form1("0"));
7 }
Quartz.NET开源作业调度框架系列(一):快速入门step by step-转

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

3 最终效果

  最终效果如下 , 每隔1秒,就是用随机的数来更新文本框和图表中的内容:

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

  值得注意的是,如果用下面的代码格式,必须保证二者的名称完全一致,否则无法实现任务和触发器的绑定.

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

水平有限,望各位园友不吝赐教!如果觉得不错,请点击推荐和关注! 
出处:http://www.cnblogs.com/isaboy/ 
声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类: C#