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

时间:2023-02-21 15:53:37

  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

Quartz.NET开源作业调度框架系列(一):快速入门step by step的更多相关文章

  1. Quartz&period;NET开源作业调度框架系列&lpar;一&rpar;&colon;快速入门step by step-转

    Quartz.NET是一个被广泛使用的开源作业调度框架 , 由于是用C#语言创建,可方便的用于winform和asp.net应用程序中.Quartz.NET提供了巨大的灵活性但又兼具简单性.开发人员可 ...

  2. Quartz&period;NET开源作业调度框架系列

    Quartz.NET是一个被广泛使用的开源作业调度框架 , 由于是用C#语言创建,可方便的用于winform和asp.net应用程序中.Quartz.NET提供了巨大的灵活性但又兼具简单性.开发人员可 ...

  3. Quartz&period;NET开源作业调度框架系列&lpar;三&rpar;&colon;IJobExecutionContext 参数传递

    前面写了关于Quartz.NET开源作业调度框架的入门和Cron Trigger , 这次继续这个系列, 这次想讨论一下Quartz.NET中的Job如何通过执行上下文(Execution Conte ...

  4. Quartz&period;NET开源作业调度框架系列&lpar;三&rpar;&colon;IJobExecutionContext 参数传递-转

    前面写了关于Quartz.NET开源作业调度框架的入门和Cron Trigger , 这次继续这个系列, 这次想讨论一下Quartz.NET中的Job如何通过执行上下文(Execution Conte ...

  5. Quartz&period;NET开源作业调度框架系列&lpar;五&rpar;&colon;AdoJobStore保存job到数据库

    Quartz.NET 任务调度的核心元素是 scheduler, trigger 和 job,其中 trigger(用于定义调度时间的元素,即按照什么时间规则去执行任务) 和 job 是任务调度的元数 ...

  6. Quartz&period;NET开源作业调度框架系列&lpar;四&rpar;&colon;Plugin Job

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

  7. Quartz&period;NET开源作业调度框架系列&lpar;四&rpar;&colon;Plugin Job-转

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

  8. Quartz&period;NET开源作业调度框架系列&lpar;二&rpar;&colon;CronTrigger

    CronTriggers比SimpleTrigger更加的灵活和有用,对于比较复杂的任务触发规则,例如"每个星期天的晚上12:00"进行备份任务,SimpleTrigger就不能胜 ...

  9. Quartz&period;NET开源作业调度框架系列&lpar;二&rpar;&colon;CronTrigger-转

    CronTriggers比SimpleTrigger更加的灵活和有用,对于比较复杂的任务触发规则,例如"每个星期天的晚上12:00"进行备份任务,SimpleTrigger就不能胜 ...

随机推荐

  1. SVN&plus;码云 简单使用流程

    1.登录码云网站...

  2. qam 64的设计

    module qam64(x,out,clk,clk1,rst);input x,clk,clk1,rst;output [18:0] out;reg [2:0] count;reg [5:0] re ...

  3. mysql 数据库字符集的指定

    create database mydb default character set utf8 default collate utf8_general_ci;

  4. 初学者学习javascript语言应注意的那几点

    javascript在书写时应注意得那四点: 1)大小写敏感: 2)javascript是弱类型语言,声明变量是应全部使用var(因为javascript是弱类型语言): 3)字符串在定义时使用单引号 ...

  5. JS无缝文字滚动(兼容各大浏览器)

    <style>*{margin:0px;padding:0px;border:0px;}body{font-size:12px}#demo1{height:auto;text-align: ...

  6. Object&period;defineProperty 规则

  7. Python Django开发中XSS内容过滤问题的解决

    from:http://*.com/questions/699468/python-html-sanitizer-scrubber-filter 通过下面这个代码就可以把内容过 ...

  8. activemq读取剩余消息队列中消息的数量

    先上原文链接: http://blog.csdn.net/bodybo/article/details/5647968  ActiveMQ在C#中的应用 ActiveMQ是个好东东,不必多说.Acti ...

  9. KL散度

    摘自: https://www.jianshu.com/p/43318a3dc715?from=timeline&isappinstalled=0 一.解决的问题 量化两种概率分布P和Q可以使 ...

  10. 动态sql and在前 逗号在后