zoukankan      html  css  js  c++  java
  • 任务调度框架-Quartz.Net

    使用Quartz.Net依赖于以下3个组件:Common.Logging.dll、Common.Logging.Core.dll、Quartz.dll

    简单封装

     1 using Quartz;
     2 using Quartz.Impl;
     3 using System;
     4 using System.Collections.Generic;
     5 using System.Linq;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 
     9 namespace Test
    10 {
    11     public class SchedulerHelper
    12     {
    13         /// <summary>
    14         /// 任务调度器实例
    15         /// </summary>
    16         public static IScheduler scheduler { get; }
    17         static SchedulerHelper()
    18         {
    19             //实例化任务调度器对象
    20             scheduler = StdSchedulerFactory.GetDefaultScheduler();
    21         }
    22 
    23         /// <summary>
    24         /// 添加任务
    25         /// </summary>
    26         /// <typeparam name="T">IJob类型</typeparam>
    27         /// <param name="jobIdentity">job唯一标识</param>
    28         /// <param name="triggerIdentity">trigger唯一标识</param>
    29         /// <param name="cronExpression">cron表达式</param>
    30         /// <param name="jobGroup">任务所属分组[可选]</param>
    31         /// <param name="triggerGroup">触发器所属分组[可选]</param>
    32         public static void AddScheduleJob<T>(string jobIdentity, string triggerIdentity, string cronExpression, string jobGroup = "", string triggerGroup = "")
    33             where T : IJob
    34         {
    35             var jobBuilder = JobBuilder.Create<T>();
    36             if (string.IsNullOrEmpty(jobGroup))
    37                 jobBuilder.WithIdentity(jobIdentity);
    38             else
    39                 jobBuilder.WithIdentity(jobIdentity, jobGroup);
    40 
    41             var currentJob = jobBuilder.Build();
    42 
    43             var triggerBuilder = TriggerBuilder.Create();
    44             if (string.IsNullOrEmpty(triggerGroup))
    45                 triggerBuilder.WithIdentity(triggerIdentity);
    46             else
    47                 triggerBuilder.WithIdentity(triggerIdentity, triggerGroup);
    48 
    49             var currentTrigger = triggerBuilder
    50                                 .StartNow()
    51                                 .WithCronSchedule(cronExpression)
    52                                 .Build();
    53 
    54             //把作业,触发器加入调度器。
    55             scheduler.ScheduleJob(currentJob, currentTrigger);
    56         }
    57 
    58         public static void Start()
    59         {
    60             if (scheduler == null)
    61                 throw new ArgumentNullException("scheduler", "任务调度器实例对象不能为null");
    62             if (!scheduler.IsStarted)
    63                 scheduler.Start();
    64         }
    65 
    66         public static void ShutDown()
    67         {
    68             if (scheduler == null)
    69                 throw new ArgumentNullException("scheduler", "任务调度器实例对象不能为null");
    70             if (!scheduler.IsShutdown)
    71                 scheduler.Shutdown();
    72         }
    73     }
    74 }

    客户端

    using NLog;
    using Quartz;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Test
    {
        public class WriteLogJob : IJob
        {
            ILogger log = LogManager.GetLogger("admin");
            public void Execute(IJobExecutionContext context)
            {
                try
                {
                    log.Error(string.Format("当前时间为:{0}", DateTime.Now));
                }
                catch (Exception x) { }
            }
        }
    }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Configuration;
     4 using System.Linq;
     5 using System.Text;
     6 
     7 namespace Test
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             SchedulerHelper.AddScheduleJob<WriteLogJob>("WriteLogJob", "WriteLogTrigger", "0/5 * * * * ?");
    14 
    15             SchedulerHelper.Start();
    16 
    17             Console.ReadLine();
    18         }
    19     }
    20 }

    Quartz的cron表达式

    官文文档:cron表达式介绍

    由7段构成:秒 分 时 日 月 星期 年(可选)
    "-" :表示范围  MON-WED表示星期一到星期三
    "," :表示列举 MON,WEB表示星期一和星期三
    "*" :表是“每”,每月,每天,每周,每年等
    "/" :表示增量:0/15(处于分钟段里面) 每15分钟,在0分以后开始,3/20 每20分钟,从3分钟以后开始
    "?" :只能出现在日,星期段里面,表示不指定具体的值
    "L" :只能出现在日,星期段里面,是Last的缩写,一个月的最后一天,一个星期的最后一天(星期六)
    "W" :表示工作日,距离给定值最近的工作日
    "#" :表示一个月的第几个星期几,例如:"6#3"表示每个月的第三个星期五(1=SUN...6=FRI,7=SAT)

    常用cron表达式示例

    // 0/5 * * * * ?            每5秒钟执行一次
    // 0 0/5 * * * ?            每5分钟执行一次
    // 10 0/5 * * * ?           每5分钟,在第10秒钟执行一次
    // 0 30 10-13 ? * WED,FRI   每周的周三、周五,时针为10-13,分钟刻度为30的时候执行一次
    // 0 0/30 8-9 5,20 * ?      每个月的第5、第20天,时针为8到9,分钟刻度为从0开始,每过30分钟执行一次

    官方实例

    ExpressionMeaning
    0 0 12 * * ? Fire at 12pm (noon) every day
    0 15 10 ? * * Fire at 10:15am every day
    0 15 10 * * ? Fire at 10:15am every day
    0 15 10 * * ? * Fire at 10:15am every day
    0 15 10 * * ? 2005 Fire at 10:15am every day during the year 2005
    0 * 14 * * ? Fire every minute starting at 2pm and ending at 2:59pm, every day
    0 0/5 14 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
    0 0/5 14,18 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
    0 0-5 14 * * ? Fire every minute starting at 2pm and ending at 2:05pm, every day
    0 10,44 14 ? 3 WED Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
    0 15 10 ? * MON-FRI Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
    0 15 10 15 * ? Fire at 10:15am on the 15th day of every month
    0 15 10 L * ? Fire at 10:15am on the last day of every month
    0 15 10 L-2 * ? Fire at 10:15am on the 2nd-to-last last day of every month
    0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
    0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
    0 15 10 ? * 6L 2002-2005 Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
    0 15 10 ? * 6#3 Fire at 10:15am on the third Friday of every month
    0 0 12 1/5 * ? Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.
    0 11 11 11 11 ? Fire every November 11th at 11:11am.
  • 相关阅读:
    multipart/form-data同时传递文本和多文件参数controller接收
    sonar配置记录一下经常找不到
    神经网络分类知识蒸馏
    jconsole监听JVM
    Cocos2dx在安卓平台下获取到assets目录下文件的绝对路径
    打印100以内的质数及优化
    VBA调用百度翻译API
    VBA调用百度智能云的文字识别获取图片中的数字
    象棋的思考方法讨论
    やさしい日本語2019 学习方法
  • 原文地址:https://www.cnblogs.com/Jabben_Yi/p/5751307.html
Copyright © 2011-2022 走看看