首页 技术 正文
技术 2022年11月13日
0 收藏 450 点赞 3,067 浏览 3954 个字

  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文件中  ,首先引入需要的命名空间:

     using Quartz;
using Quartz.Impl;
using Quartz.Job;
using System.Threading; using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;

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

         private bool isFirst = true;
public Form1(string msg)
{
InitializeComponent();
_msg = msg;
try
{
//启动 scheduler
scheduler.Start();
// 定义一个job并和自定义的HelloJob进行绑定
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("HelloJob", "SimpleGroup")
.Build();
#region Cron Expressions
// ITrigger trigger2 = TriggerBuilder.Create()
//.WithIdentity("trigger3", "group1")
//.WithCronSchedule(" 0 0/5 * * * ?", x => x
// .WithMisfireHandlingInstructionFireAndProceed())
//.ForJob("job1", "group1")
//.Build();
#endregion //定义一个即时触发的触发器,(每隔1秒进行重复执行)
ITrigger trigger= TriggerBuilder.Create()
.WithIdentity("trigger1", "SimpleGroup")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds()
.RepeatForever())
.Build();
// 将job和trigger进行绑定,并告知 quartz 调度器用trigger去执行job
scheduler.ScheduleJob(job, trigger);
}
catch (SchedulerException se)
{
Console.WriteLine(se);
} }

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

         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//关闭 scheduler
scheduler.Shutdown();
}

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

         public OxyPlot.WindowsForms.PlotView  Plot;
private LineSeries lineSeries = new LineSeries { Title = "即时监控(1秒)", StrokeThickness = };
private void Form1_Load(object sender, EventArgs e)
{
Plot = new OxyPlot.WindowsForms.PlotView();
Plot.Model = new PlotModel();
Plot.Dock = DockStyle.Fill;
this.panel1.Controls.Add(Plot); Plot.Model.PlotType = PlotType.XY;
Plot.Model.Background = OxyColor.FromRgb(, , );
Plot.Model.TextColor = OxyColor.FromRgb(, , ); // add Series and Axis to plot model
Plot.Model.Series.Add(lineSeries);
Plot.Model.Axes.Add(new LinearAxis());
}

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

         private double xInit = ;
public bool SetMsg(string msg)
{
_msg = msg;
//号称NET4最简单的跨进程更新UI的方法
this.Invoke((MethodInvoker)delegate
{
// runs on UI thread
if (isFirst)
{
this.txtLog.AppendText("Hello to Quartz NET ! Created by JackWang 2015");
isFirst = false;
}
this.txtLog.AppendText(string.Format("\r\n$JackWang>> You get {0} message from Quartz MyJob...", _msg));
xInit = xInit + ;
lineSeries.Points.Add(new DataPoint(xInit,double.Parse(_msg)));
if (lineSeries.Points.Count > )
{
//保留最近50个点
lineSeries.Points.RemoveAt();
}
//更新图表数据
this.Plot.Model.InvalidatePlot(true); });
return true; }

2.3 定义一个HelloJob

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace QuartzDemo
{
using Quartz;
using Quartz.Impl;
using Quartz.Job;
public class HelloJob : IJob
{
private static Form1 __instance = null;
public void Execute(IJobExecutionContext context)
{ if (isOpen("Form1"))
{
//获取当前Form1实例
__instance = (Form1)Application.OpenForms["Form1"];
//随机生成小于100的数
string num = new Random().Next().ToString();
//通过方法更新消息
__instance.SetMsg(num);
}
else
{
//__instance = new Form1("0");
//__instance.Show(); } }
/// <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;
}
}
}

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

         [STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(""));
}

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

3 最终效果

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

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

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896