基于Quartz.Net类库的Cron定时任务(实例)

cron常用表达式

标准格式为六位:从左至右依次为“秒 分 时 日 月 周”

七位格式:从左至右依次为“秒 分 时 日 月 周 年”

  • 六位格式年份默认为1970-2099

  • “周” 从1-7,数字或英文缩写(1或 SUN,MON,TUE,WED,THU,FRI,SAT)

  • * 表示所有可能的值

  • ? 仅被用于天(月)和天(星期)两个子表达式,表示不指定值,当两个子表达式其中之一被指定了确定值以后,为了避免冲突,需要将另一个子表达式的值设为“?”

  • 在确定周几的时候,“日”一定得判断,如【0 15 10 ? * MON-FRI】,“日”用“?”,不能用“ * ”

常用示例

【0 * 14 * * ?】: 每天下午的 2点到2点59分每分触发
【0 0-30 0-23 ? * 1-5】: 周一到周五内每天0-23:59点之间0-30分钟执行
【0 15 10 ? * MON-FRI 】: 从周一到周五每天上午的10点15分触发
【0/3 * * ? * MON-FRI 】: 从周一到周五每天每3秒触发
【0 * 14 * * ? 】: 每天下午的 2点到2点59分每分触发

【0 15 10 * * ? 2005】: 2005年每天10点15分触发

基于Quartz.Net类库的 C# 实现

  1. 引入 Quartz.Net 类库
  2. 配置文件
<configuration>
  <configSections>  <!--此配置节必须紧跟configuration,且只有一个-->
    <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>
  
  <appSettings>
  <!--自定义配置:每五分钟执行一次-->
  <add key="cron" value="* 5 * * * ?"/>
  </appSettings>
</configuration>


3.代码编写,以控制台程序为例,在自定义的触发时间内重复执行指定方法,常用于服务的编写

using Quartz;
using Quartz.Impl;
using System;
namespace QuartzTest
{
    public class Program
    {
        static void Main(string[] args)
        {
            UseJob();
            Console.ReadKey();
        }

        public static void UseJob()
        {
            IJobDetail jobDetail = JobBuilder.Create(typeof(MyJob)).WithIdentity("TestJob1", "Group1").Build();

            ITrigger trigger = TriggerBuilder.Create().WithIdentity("TestJob1", "Group1").StartNow().WithCronSchedule("1 37 9 * * ?").Build();   //"0/3 * 0-23 12 9 ?"  9月12日0-23点中每3秒

            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sched = sf.GetScheduler();
            //var sched = StdSchedulerFactory.GetDefaultScheduler();
            sched.Start();

            sched.ScheduleJob(jobDetail, trigger);
        }
    }

    
    /// <summary>
    /// 方法实体,继承于IJob
    /// </summary>
    public class MyJob:IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("****************************");
            Console.WriteLine("我的任务!!!");
            Console.WriteLine("****************************");

        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq_31176861/article/details/81331815
今日推荐