Quartz.Net 学习之一

          Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中。它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。它有很多特征,如:数据库支持,集群,插件,支持cron-like表达式等等。

你曾经需要应用执行一个任务吗?这个任务每天或每周星期二晚上11:30,或许仅仅每个月的最后一天执行。一个自动执行而无须干预的任务在执行过程中如果发生一个严重错误,应用能够知到其执行失败并尝试重新执行吗?你和你的团队是用.NET编程吗?如果这些问题中任何一个你回答是,那么你应该使用Quartz.NET调度器。 Quartz.NET允许开发人员根据时间间隔(或天)来调度作业。它实现了作业和触发器的多对多关系,还能把多个作业与不同的触发器关联。整合了 Quartz.NET的应用程序可以重用来自不同事件的作业,还可以为一个事件组合多个作业.

 

  

CONFIG中的设置:
 1 <!-- 关于Quartz.NET的配置 -->
2     < configSections >
3         < section name = " quartz " type = " System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089 " />
4         < sectionGroup name = " common " >
5             < section name = " logging " type = " Common.Logging.ConfigurationSectionHandler, Common.Logging " />
6         </ sectionGroup >
7     </ configSections >
8     < common >
9         < logging >
10             < factoryAdapter type = " Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging " >
11                 < arg key = " showLogName " value = " true " />
12                 < arg key = " showDataTime " value = " true " />
13                 < arg key = " level " value = " DEBUG " />
14                 < arg key = " dateTimeFormat " value = " HH:mm:ss:fff " />
15             </ factoryAdapter >
16         </ logging >
17     </ common >
18     < quartz >
19         < add key = " quartz.scheduler.instanceName " value = " ExampleDefaultQuartzScheduler " />
20         < add key = " quartz.threadPool.type " value = " Quartz.Simpl.SimpleThreadPool, Quartz " />
21         < add key = " quartz.threadPool.threadCount " value = " 10 " />
22         < add key = " quartz.threadPool.threadPriority " value = " 2 " />
23         < add key = " quartz.jobStore.misfireThreshold " value = " 60000 " />
24         < add key = " quartz.jobStore.type " value = " Quartz.Simpl.RAMJobStore, Quartz " />
25     </ quartz >
26     <!-- 关于Quartz.NET的配置结束 -->
另外,还可以增加一个配置项,这里的意思是没10秒钟执行一次
<add key="cronExpr" value="0/10 * * * * ?"/>
当然也可以在调用任务时再行设置,设置说明:
1. Seconds 秒
2. Minutes 分钟
3. Hours 小时
4. Day-of-Month 月中的天
5. Month 月
6. Day-of-Week 周中的天
7. Year (optional field) 年(可选的域)

执行任务的类:
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Common.Logging;
5 using Quartz;
6
7 namespace Common
8 {
9     public class DBJob : IJob
10     {
11         #region IJob 成员
12         public DBJob()
13         {
14             //
15             // TODO: 在此处添加构造函数逻辑
16             //
17         }
18         public void Execute(JobExecutionContext context)
19         {
20             try
21             {
22                 string sql = " INSERT INTO test(nowDate) VALUES (' " + DateTime.Now.ToLocalTime() + " ') " ;
23                 Common.SqlHelper.ExecuteSql(sql);
24             }
25             catch (Exception e)
26             {
27                 JobExecutionException e2 = new JobExecutionException(e);
28                 e2.RefireImmediately = true ;
29                 throw e2;
30             }
31         }
32
33         #endregion
34     }
35 }

Global.asax中调用

1 void Application_Start(object sender, EventArgs e)
2     {
3         // 在应用程序启动时运行的代码
4         //Common.CronTriggerRunner lt = new Common.CronTriggerRunner();
5         //lt.Run();
6         Quartz.ISchedulerFactory sf = new Quartz.Impl.StdSchedulerFactory();
7         sched = sf.GetScheduler();
8         Quartz.JobDetail job = new Quartz.JobDetail("job1", "group1", typeof(Common.DBJob));
9
10         string cronExpr = ConfigurationManager.AppSettings["cronExpr"];
11         Quartz.CronTrigger trigger = new Quartz.CronTrigger("trigger1", "group1", "job1", "group1", cronExpr);
12         sched.AddJob(job, true);
13         DateTime ft = sched.ScheduleJob(trigger);
14         sched.Start();
15     }
16    
17     void Application_End(object sender, EventArgs e)
18     {
19         //  在应用程序关闭时运行的代码
20         sched.Shutdown(true);
21     }

   

你可能感兴趣的:(quartz)