配置
使用Quartz.NET,我们需要在配置文件里面添加点东西,首先是configSections节点中,我们要添加已下内容:
使用Quartz.NET,我们需要在配置文件里面添加点东西,首先是configSections节点中,我们要添加已下内容:
xml代码
|
1
2
3
4
|
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
</sectionGroup>
|
从上面添加的节点可以知道,肯定还有一些节点要添加的,在configuration节点中添加如下:
注:以上的配置我还没有去仔细研究,我是在网上找中文资料抄的 - -!
xml代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
<arg key="showLogName" value="true"/>
<arg key="showDataTime" value="true"/>
<arg key="level" value="DEBUG"/>
<arg key="dateTimeFormat" value="HH:mm:ss:fff"/>
</factoryAdapter>
</logging>
</common>
<quartz>
<add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/>
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
<add key="quartz.threadPool.threadCount" value="10"/>
<add key="quartz.threadPool.threadPriority" value="2"/>
<add key="quartz.jobStore.misfireThreshold" value="60000"/>
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
</quartz>
|
Quartz.NET中几个重要的东西
1、作业调度器接口 IScheduler:这个接口负责启动一个作业和关闭一个作业,在启动和关闭作业之前,它还需要接受两个东西,一个是触发器(Trigger),一个是工作任务(JobDetail),要得到一个这样的接口的实例,我们还需要一个作业调度器工厂(ISchedulerFactory)。
2、作业调度器工厂接口 ISchedulerFactory:从命名来看,很明显是个工厂,专门产出IScheduler接口。
3、工作任务 JobDetail:它定义了你要定时去做什么工作,初始化一个该类的对象,我们要指定工作名(name),工作的组名(group),还有就是一个实现了IJob接口的类的类型(Type),就是说JobD定义的是做什么。
4、触发器 Trigger:据说Quartz.NET的触发器有很多,但是一般我们使用的是两种(都是继承了Trigger类的子类),一个是CronTrigger、另外一个是SimpleTrigger,我们要指定触发器名(name),触发器的的职责是定义工作任务的触发规则,也就是说Trigger定义的是啥时候做。
5、工作接口 IJob:我们需要把定时要做的事情(业务逻辑,例如去写一下数据库,写一下文件之类的)包装在一个类里面,这个类有一个特点,就是继承IJob这个接口,实现该接口的唯一方法Execute(JobExecutionContext context),在这个方法中将尽情地用代码实现我们的业务,所以可以说这个接口定义的是如何做。
网上找的资料基本一上来就是代码,没有对几个重要的对象就行讲解,这点比较遗憾!通过上面我们基本可以猜想出写出来的代码会是怎么样:我们先定义个类去实现IJob接口,把要做的“工作”写好,然后从作业调度器工厂(ISchedulerFactory)中拿个作业调度器(IScheduler)出来,定义好工作任务(JobDetail)指定这个工作任务的工作就是我们定义的那个类,定义好触发器(Trigger,也许是用CronTrigger、也许是用SimpleTrigger),把工作任务和触发器交给作业调度器,这样一来,就确定了在什么时候要去做什么,也知道要怎么做了,最后调用作业调度器的一个方法(Statr())来开始任务!
代码示例
下面用代码来说明问题,我们用代码来完成这样一个需求:在web项目中有一个txt文件(文件名为JobNotePad.txt),每隔十秒钟就把当前时间记录在里面。这里我们使用CronTrigger这种触发器来完成任务。首先我们要实现IJob接口,定义一个类如下:
c#代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using Quartz;
namespace WebApplication.Common
{ public class MyJob : IJob
{
public void Execute(JobExecutionContext context)
{
try
{
var path = @"H:\代码\测试代码\Csharp测试\QuartzTest\WebApplication\WebApplication\JobNotePad.txt";
var curText = System.IO.File.ReadAllText(path);
System.IO.File.WriteAllText(path, curText + "\r\n" + DateTime.Now.ToString());
}
catch (Exception ex)
{
throw;
}
}
}
} |
接着我们要在Global.asax文件里写点代码,在网站开启的时候就开始作业,于是在Application_Start中加入如下代码:
c#代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/// <summary>/// 作业调度器接口/// </summary>IScheduler scheduler;protected void Application_Start(object sender, EventArgs e)
{ try
{
// 创建一个工作调度器工场
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
// 获取一个任务调度器
scheduler = schedulerFactory.GetScheduler();
// 创建一个工作
JobDetail job = new JobDetail("jobname1", "jobgroup1", typeof(Common.MyJob));
// 创建一个触发器
CronTrigger trigger = new CronTrigger();
trigger.Name = "trigger1";
trigger.JobName = "jobname1";
trigger.JobGroup = "jobgroup1";
trigger.Group = "triggergroup1";
trigger.CronExpression = new CronExpression("0/10 * * * * ?");
scheduler.AddJob(job, true);
DateTime ft = scheduler.ScheduleJob(trigger);
scheduler.Start();
}
catch (Exception ex)
{
throw ex;
}
} |
c#代码
|
1
2
3
4
|
protected void Application_End(object sender, EventArgs e)
{ scheduler.Shutdown(true);
} |
c#代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
protected void Application_Start(object sender, EventArgs e)
{ try
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
scheduler = schedulerFactory.GetScheduler();
JobDetail job = new JobDetail("jobname1", "jobgroup1", typeof(Common.MyJob));
// 创建一个触发器
SimpleTrigger trigger = new SimpleTrigger();
trigger.Name = "trigger1";
trigger.JobName = "jobname1";
trigger.JobGroup = "jobgroup1";
trigger.Group = "triggergroup1";
trigger.RepeatInterval = new TimeSpan(0, 0, 1);
trigger.RepeatCount = 10;
scheduler.AddJob(job, true);
DateTime ft = scheduler.ScheduleJob(trigger);
scheduler.Start();
}
catch (Exception ex)
{
throw ex;
}
} |
测试代码,我们只需要运行网站,然后查看根目录下的JobNotePad.txt文件就知道鸟!