您的位置:首页 > 其它

Quartz.net使用总结

2009-08-17 15:18 417 查看
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 }

最后
代码部分完毕之后,要重启WWW服务,并且访问站点内任一ASPX页面,任务方可执行!
InsertToEditor("Code
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 }")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: