ASP.NET使用Quartz.NET实现定时任务调度

来源:互联网 发布:远景能源怎么样 知乎 编辑:程序博客网 时间:2024/05/22 07:56

第一步:下载Quartz.NET类库源码

下载地址:http://www.quartz-scheduler.net/


第二步:程序集成:

1.修改网站根目录下的web.config文件,在configuration节增加:

  <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>
    <!--/End 定时任务调度配置-->
  </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>
  <!--/End 定时任务调度配置-->

   注意:如果web.config已存在configSections节,请将configSections节内的配置复制到web.config的configSections节下。


2.编写代码:

   2.1 取得作业调度单一实例

   2.2 定时作业调度器辅助类

   2.3 一个示例的定时任务


3.在网站Global.asax文件的Application_Start事件里开启定时任务:

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //开启定时任务调度
            JobHelper.StartJob<SampleJob>("0 0/1 * * * ?");//每隔一分钟执行一次任务
        }

至此程序集成完毕,可以根据自己设定的表达式触发器来检验定时任务是否正常工作。


参考文档:

1.Quartz.NET 官网:http://www.quartz-scheduler.net/documentation/index.html

2.简单触发器:http://www.quartz-scheduler.net/documentation/quartz-2.x/quick-start.html

3.表达式触发器:http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/crontrigger.html

原文链接:http://qingshanboke.com/article.aspx?id=113



0 0
原创粉丝点击