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 }")
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 }")
相关文章推荐
- quartz.net 使用总结
- Quartz.net使用总结
- Quartz.net使用总结
- Quartz.net使用总结
- Quartz.Net使用总结
- quartz.net 3.x 使用总结
- Quartz.Net在C#中的使用
- 在asp.net中KindEditor编辑器的使用总结
- EXT.NET 使用总结(1)
- ASP.NET中 ValidationSummary(验证总结)的使用
- asp.net的尖括号使用总结
- 什么是连接池:Ado.net连接池使用总结:什么情况下需要禁用连接池?
- ASP.NET MVC 使用总结(一)
- asp.net 水晶报表使用总结
- C#使用Quartz.NET详解
- ASP.NET中相对路径的使用总结
- 总结:ADO.NET在开发中的部分使用方法和技巧
- Quartz使用总结
- [转]web使用Quartz.NET实现作业调度
- Quartz使用总结