任务调度框架-Quartz.Net

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

简单封装

 using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Test
{
public class SchedulerHelper
{
/// <summary>
/// 任务调度器实例
/// </summary>
public static IScheduler scheduler { get; }
static SchedulerHelper()
{
//实例化任务调度器对象
scheduler = StdSchedulerFactory.GetDefaultScheduler();
} /// <summary>
/// 添加任务
/// </summary>
/// <typeparam name="T">IJob类型</typeparam>
/// <param name="jobIdentity">job唯一标识</param>
/// <param name="triggerIdentity">trigger唯一标识</param>
/// <param name="cronExpression">cron表达式</param>
/// <param name="jobGroup">任务所属分组[可选]</param>
/// <param name="triggerGroup">触发器所属分组[可选]</param>
public static void AddScheduleJob<T>(string jobIdentity, string triggerIdentity, string cronExpression, string jobGroup = "", string triggerGroup = "")
where T : IJob
{
var jobBuilder = JobBuilder.Create<T>();
if (string.IsNullOrEmpty(jobGroup))
jobBuilder.WithIdentity(jobIdentity);
else
jobBuilder.WithIdentity(jobIdentity, jobGroup); var currentJob = jobBuilder.Build(); var triggerBuilder = TriggerBuilder.Create();
if (string.IsNullOrEmpty(triggerGroup))
triggerBuilder.WithIdentity(triggerIdentity);
else
triggerBuilder.WithIdentity(triggerIdentity, triggerGroup); var currentTrigger = triggerBuilder
.StartNow()
.WithCronSchedule(cronExpression)
.Build(); //把作业,触发器加入调度器。
scheduler.ScheduleJob(currentJob, currentTrigger);
} public static void Start()
{
if (scheduler == null)
throw new ArgumentNullException("scheduler", "任务调度器实例对象不能为null");
if (!scheduler.IsStarted)
scheduler.Start();
} public static void ShutDown()
{
if (scheduler == null)
throw new ArgumentNullException("scheduler", "任务调度器实例对象不能为null");
if (!scheduler.IsShutdown)
scheduler.Shutdown();
}
}
}

客户端

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) { }
}
}
}
 using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text; namespace Test
{
class Program
{
static void Main(string[] args)
{
SchedulerHelper.AddScheduleJob<WriteLogJob>("WriteLogJob", "WriteLogTrigger", "0/5 * * * * ?"); SchedulerHelper.Start(); Console.ReadLine();
}
}
}

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分钟执行一次

官方实例

Expression Meaning
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.
上一篇:[Android Pro] Android签名与认证详细分析之一(CERT.RSA剖析)


下一篇:highchart导出功能的介绍更改exporting源码