当前位置: 首页 > news >正文

C# 定时任务 Quartz.NET 的使用

一、定时任务的介绍

相信我们在生活中,大部分都会使用到定时任务去执行自定义的业务逻辑,如:每天早上8点钟发送一份汇总好的财经报告到指定人的邮箱;或者每周一5点30分钟自动执行下载器下载电影,下载完并通过QQ等机器人的方式通知管理员(如下图)。

image

  二、C# 的Quartz.NET的使用

1、NuGet页面搜索Quartz.NET,并安装

image

2、创建一个 TestJob ,并对 IJob 的接口的实现

/// <summary>
/// 创建一个测试的Job类
/// </summary>
public class TestJob : IJob
{public async Task Execute(IJobExecutionContext context){Console.WriteLine($"{DateTime.Now.ToString("yy-MM-dd HH:mm:ss fff")},执行了TestJob");await Task.CompletedTask;}
}

3、实例化调度器的参数:任务明细,注意:“myGroup” 是任务的一个标识,每一个任务都有独立的一个标识状态

IJobDetail job = JobBuilder.Create<TestJob>().WithIdentity("TestJob", "myGroup").Build();

4、实例化调度器的参数:触发器,如下代码:创建一个一秒循环的触发器

ITrigger trigger = TriggerBuilder.Create().WithIdentity("TestJobTrigger", "myGroup").WithSimpleSchedule(x =>{x.WithIntervalInSeconds(1).RepeatForever();}).Build();

5、创建任务调度器,并执行任务

StdSchedulerFactory factory = new StdSchedulerFactory();
//创建任务调度器
IScheduler scheduler = await factory.GetScheduler();
await scheduler.Start();
await scheduler.ScheduleJob(job, trigger);
Console.WriteLine("任务调度器已启动,按任意键退出...");

6、执行的效果如下:

image

 三、时间表达式 Cron 的使用

官网说明:CronTrigger Tutorial | Quartz.NET

A cron expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of the allowed special characters for that field. The fields are as follows:

Field NameMandatoryAllowed ValuesAllowed Special Characters
Seconds YES 0-59 , - * /
Minutes YES 0-59 , - * /
Hours YES 0-23 , - * /
Day of month YES 1-31 , - * ? / L W
Month YES 1-12 or JAN-DEC , - * /
Day of week YES 1-7 or SUN-SAT , - * ? / L #
Year NO empty, 1970-2099 , - * /

So cron expressions can be as simple as this: * * * * ? *

or more complex, like this: 0/5 14,18,3-39,52 * ? JAN,MAR,SEP MON-FRI 2002-2010

 

 

Here are some full examples:

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.

 

如:我想要每天上午10:15分执行一次的cron表达式:0 15 10 * * ?

IJobDetail job = JobBuilder.Create<TestJob>().WithIdentity("TestJob", "myGroup").Build();ITrigger trigger = TriggerBuilder.Create().WithIdentity("TestJobTrigger", "myGroup").WithCronSchedule("0 15 10 * * ?").Build();StdSchedulerFactory factory = new StdSchedulerFactory();
//创建任务调度器
IScheduler scheduler = await factory.GetScheduler();
//启动任务调度器
await scheduler.Start();//将创建的任务和触发器条件添加到创建的任务调度器当中
await scheduler.ScheduleJob(job, trigger);Console.WriteLine("任务调度器已启动,按任意键退出...");
Console.ReadKey();

 

http://www.sczhlp.com/news/34895/

相关文章:

  • 昆山做网站找哪家好市场营销策划案例经典大全
  • 乡镇网站个人做可以不怎样做网络推广挣钱
  • 深圳做网站优化报价seo优化师培训
  • 子域名查询工具南京百度seo
  • 广州网站设计实力乐云seoseo在线优化工具
  • 网站代码怎么写seo门户网站优化
  • 专门做反季的网站网络推广外包要多少钱
  • soho的网站怎么做网页开发培训网
  • 02020204 .NET Core重难点知识04-async和await原理揭秘、async背后的线程切换
  • 测试实践
  • 集合--介绍、框架体系、单列集合(Collection、List接口介绍,常用方法,遍历方式)
  • 广州网站开发水平广州亦客网络专业做app软件开发公司
  • 织梦素材网站模板免费制作链接
  • 南京建设教育网站河南纯手工seo
  • 最专业的佛山网站建设价格百度推广费用怎么算
  • 关于并查集 - Ghost
  • 变量作用域详细解释
  • 网站维护的主要内容包括2022年app拉新推广项目
  • 做网站推广常识题库及答案青岛百度网站排名优化
  • wordpress绕绕国外seo比较好的博客网站
  • wordpress 显示缩略图优化培训内容
  • wangz网站建设宁波seo优化项目
  • 网站开发要学什么河南新闻头条最新消息
  • 太原网站开发工程师seo快速排名利器
  • AT_abc277_c [ABC277C] Ladder Takahashi 题解
  • wordpress悬浮菜单厦门网站优化
  • 怎么才能搜索到自己做的网站seo推广外包报价表
  • 个人可以做商城网站吗青岛关键词网站排名
  • 男女做暖暖其他网站百度广告一天多少钱
  • 珠海网站建设网络公司怎么样交友网站有哪些