CONFIG中的设置:
<!--关于Quartz.NET的配置-->
<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="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的配置结束-->
另外,还可以增加一个配置项,这里的意思是没10秒钟执行一次
<appSettings>
<add key="cronExpr" value="0/10 * * * * ?"/>
<add key="cronExpr" value="0/10 * * * * ?"/>
</appSettings>
当然也可以在调用任务时再行设置,设置说明:
当然也可以在调用任务时再行设置,设置说明:
1. Seconds 秒
2. Minutes 分钟
3. Hours 小时
4. Day-of-Month 月中的天
5. Month 月
6. Day-of-Week 周中的天
7. Year (optional field) 年(可选的域)
执行任务的类:
using System;
using System.Collections.Generic;
using System.Text;
using Common.Logging;
using Quartz;
namespace Common
{
public class DBJob : IJob
{
#region IJob 成员
public DBJob()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Execute(JobExecutionContext context)
{
try
{
string sql= "INSERT INTO test(nowDate) VALUES ('"+ DateTime.Now.ToLocalTime() +"') ";
Common.SqlHelper.ExecuteSql(sql);
}
catch (Exception e)
{
JobExecutionException e2 =new JobExecutionException(e);
e2.RefireImmediately =true;
throw e2;
}
}
#endregion
}
}
Global.asax中调用
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
//Common.CronTriggerRunner lt = new Common.CronTriggerRunner();
//lt.Run();
Quartz.ISchedulerFactory sf =new Quartz.Impl.StdSchedulerFactory();
sched = sf.GetScheduler();
Quartz.JobDetail job =new Quartz.JobDetail("job1","group1", typeof(Common.DBJob));
string cronExpr = ConfigurationManager.AppSettings["cronExpr"];
Quartz.CronTrigger trigger =new Quartz.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);
}
}
最后
代码部分完毕之后,要重启WWW服务,并且访问站点内任一ASPX页面,任务方可执行!