当前位置:网站首页>.Net Core 下使用 Quartz —— 【6】作业和触发器之触发器的日历
.Net Core 下使用 Quartz —— 【6】作业和触发器之触发器的日历
2022-04-23 05:55:00 【番茄大侠本尊】
触发器的日历可以让你在某段日期内屏蔽执行作业
完整代码:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
//注册调度器工厂
services.AddSingleton<ISchedulerFactory>(new StdSchedulerFactory());
}
HomeController.cs
using Microsoft.AspNetCore.Mvc;
using Quartz;
using Quartz.Impl.Calendar;
using System;
using System.Threading.Tasks;
namespace QuartzLearn.Controllers
{
public class HomeController : Controller
{
//调度器工厂
private readonly ISchedulerFactory _schedulerFactory;
//构造函数注入
public HomeController(ISchedulerFactory schedulerFactory)
{
//注入调度器工厂
_schedulerFactory = schedulerFactory;
}
public async Task<IActionResult> Index()
{
IScheduler scheduler = await _schedulerFactory.GetScheduler();
await scheduler.Start();
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("job1", "jobGroup1")
.UsingJobData("name", "zhangsan")
.Build();
//日历实例
HolidayCalendar cal = new HolidayCalendar();
//屏蔽的日期
cal.AddExcludedDate(DateTime.Parse("2020-11-12"));
await scheduler.AddCalendar("myHolidays", cal, false,false);
//在每天的9:30分触发一次
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(9, 30))
.ModifiedByCalendar("myHolidays") //不在myHolidays 日期触发
.Build();
await scheduler.ScheduleJob(job, trigger);
return View();
}
}
[DisallowConcurrentExecution]
[PersistJobDataAfterExecution]
public class HelloJob : IJob
{
public string Name { private get; set; }
public async Task Execute(IJobExecutionContext context)
{
await Console.Out.WriteLineAsync($"This is HelloJob {DateTime.Now}");
}
}
}
版权声明
本文为[番茄大侠本尊]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Tomato2313/article/details/109311634
边栏推荐
猜你喜欢
随机推荐
MOS tube characteristics and conduction process
Assembler 32-bit unsigned addition calculator
【无标题】js中的类型判断
The use of volatile in C language
约瑟夫序列 线段树 O(nlogn)
金额输入框,用于充值提现
赛氪-二进制
Understanding of SSH public key and private key
v-for下定时给图片添加动画
20220222回归职场
邮箱字符串判断
JS中的this指向
五个路由守卫的使用
Vs can be compiled, but there will be a red underline to indicate the problem of undefined identifiers
赛氪-zeal
useCenterHook
HDU-Memory Control
Use of C language and
Eigen 学习总结
信息学一本通-小球









