首页 > 代码库 > .net项目中使用Quartz
.net项目中使用Quartz
(1)在web.config中进行相关配置
<configSections> <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> </configSections>
<common> <logging> <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging"> <arg key="showLogName" value="http://www.mamicode.com/true"/> <arg key="showDataTime" value="http://www.mamicode.com/true"/> <arg key="level" value="http://www.mamicode.com/DEBUG"/> <arg key="dateTimeFormat" value="http://www.mamicode.com/HH:mm:ss:fff"/> </factoryAdapter> </logging> </common> <quartz> <add key="quartz.scheduler.instanceName" value="http://www.mamicode.com/ExampleDefaultQuartzScheduler"/> <add key="quartz.threadPool.type" value="http://www.mamicode.com/Quartz.Simpl.SimpleThreadPool, Quartz"/> <add key="quartz.threadPool.threadCount" value="http://www.mamicode.com/10"/> <add key="quartz.threadPool.threadPriority" value="http://www.mamicode.com/2"/> <add key="quartz.jobStore.misfireThreshold" value="http://www.mamicode.com/60000"/> <add key="quartz.jobStore.type" value="http://www.mamicode.com/Quartz.Simpl.RAMJobStore, Quartz"/> </quartz>
(2)创建一个普通类,实现Quartz.IJob接口
public class MyJog: IJob { public MyJog() { }
public void Execute(JobExecutionContext context) { //throw new Exception("The method or operation is not implemented."); //你的处理逻辑,也就是“工作” } }
接口非常简单,只要在Execute()方法中进行逻辑处理就可以了。比如,读取数据库数据,或者是读取电子邮件。
(3)在Global.asax文件中启动工作调度 这便于我们在web应用启动时,就启动工作调度。
<%@ Import Namespace="Quartz" %>
<script runat="server">
IScheduler sched; void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 ISchedulerFactory sf = new Quartz.Impl.StdSchedulerFactory(); sched = sf.GetScheduler(); JobDetail job = new JobDetail("job1", "group1", typeof(MyJob));
string cronExpr = "0 0 1 * * ?";
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1",cronExpr); sched.AddJob(job, true); DateTime ft = sched.ScheduleJob(trigger); sched.Start(); } void Application_End(object sender, EventArgs e) { // 在应用程序关闭时运行的代码 if (sched != null) { sched.Shutdown(true); } }
</script>
需要注意的是,当Application_End的时候,需要关闭Quartz的工作。
最后 代码部分完毕之后,要重启WWW服务,并且访问站点内任一ASPX页面,任务方可执行!
<script type="text/javascript" language="javascript">// 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 }