找回密码
 立即注册
首页 业界区 安全 C# 定时任务 Quartz.NET 的使用

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

滤冽 前天 20:33
一、定时任务的介绍

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

  二、C# 的Quartz.NET的使用

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

2、创建一个 TestJob ,并对 IJob 的接口的实现
  1. /// <summary>
  2. /// 创建一个测试的Job类
  3. /// </summary>
  4. public class TestJob : IJob
  5. {
  6.     public async Task Execute(IJobExecutionContext context)
  7.     {
  8.         Console.WriteLine($"{DateTime.Now.ToString("yy-MM-dd HH:mm:ss fff")},执行了TestJob");
  9.         await Task.CompletedTask;
  10.     }
  11. }
复制代码
3、实例化调度器的参数:任务明细,注意:“myGroup” 是任务的一个标识,每一个任务都有独立的一个标识状态
  1. IJobDetail job = JobBuilder.Create<TestJob>()
  2.     .WithIdentity("TestJob", "myGroup")
  3.     .Build();
复制代码
4、实例化调度器的参数:触发器,如下代码:创建一个一秒循环的触发器
  1. ITrigger trigger = TriggerBuilder.Create().WithIdentity("TestJobTrigger", "myGroup")
  2.     .WithSimpleSchedule(x =>
  3.     {
  4.         x.WithIntervalInSeconds(1).RepeatForever();
  5.     })
  6.     .Build();
复制代码
5、创建任务调度器,并执行任务
  1. StdSchedulerFactory factory = new StdSchedulerFactory();
  2. //创建任务调度器
  3. IScheduler scheduler = await factory.GetScheduler();
  4. await scheduler.Start();
  5. await scheduler.ScheduleJob(job, trigger);
  6. Console.WriteLine("任务调度器已启动,按任意键退出...");
复制代码
6、执行的效果如下:
3.png

 三、时间表达式 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 CharactersSecondsYES0-59, - * /MinutesYES0-59, - * /HoursYES0-23, - * /Day of monthYES1-31, - * ? / L WMonthYES1-12 or JAN-DEC, - * /Day of weekYES1-7 or SUN-SAT, - * ? / L #YearNOempty, 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:
ExpressionMeaning0 0 12 * * ?Fire at 12pm (noon) every day0 15 10 ? * *Fire at 10:15am every day0 15 10 * * ?Fire at 10:15am every day0 15 10 * * ? *Fire at 10:15am every day0 15 10 * * ? 2005Fire at 10:15am every day during the year 20050 * 14 * * ?Fire every minute starting at 2pm and ending at 2:59pm, every day0 0/5 14 * * ?Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day0 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 day0 0-5 14 * * ?Fire every minute starting at 2pm and ending at 2:05pm, every day0 10,44 14 ? 3 WEDFire at 2:10pm and at 2:44pm every Wednesday in the month of March.0 15 10 ? * MON-FRIFire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday0 15 10 15 * ?Fire at 10:15am on the 15th day of every month0 15 10 L * ?Fire at 10:15am on the last day of every month0 15 10 L-2 * ?Fire at 10:15am on the 2nd-to-last last day of every month0 15 10 ? * 6LFire at 10:15am on the last Friday of every month0 15 10 ? * 6LFire at 10:15am on the last Friday of every month0 15 10 ? * 6L 2002-2005Fire at 10:15am on every last Friday of every month during the years 2002, 2003, 2004 and 20050 15 10 ? * 6#3Fire at 10:15am on the third Friday of every month0 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 * * ?
  1. IJobDetail job = JobBuilder.Create<TestJob>()
  2.     .WithIdentity("TestJob", "myGroup")
  3.     .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();
复制代码
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册