.net 6 配置QuartZ定时任务的过程
///
/// 添加任务和触发器
///
///
///
///
///
public static void AddJobAndTrigger
{
// Use the name of the IJob as the appsettings.json key
string jobName = typeof(T).Name;
// Try and load the schedule from configuration
var configKey = $"Quartz:{jobName}";
var cronSchedule = config[configKey];
// Some minor validation
if (string.IsNullOrEmpty(cronSchedule))
{
throw new Exception($"No Quartz.NET Cron schedule found for job in configuration at {configKey}");
}
// register the job as before
var jobKey = new JobKey(jobName);
quartz.AddJob
quartz.AddTrigger(opts => opts
.ForJob(jobKey)
.WithIdentity(jobName + "-trigger")
.WithCronSchedule(cronSchedule)); // use the schedule from configuration
}
///
/// 添加任务和触发器(带参数传递)
///
///
///
///
/// 需要传递的参数
/// 默认通过 工作描述时传递参数
///
public static void AddJobAndTriggerWithParameter
IDictionary
{
// Use the name of the IJob as the appsettings.json key
string jobName = typeof(T).Name;
// Try and load the schedule from configuration
var configKey = $"Quartz:{jobName}";
var cronSchedule = config[configKey];
// Some minor validation
if (string.IsNullOrEmpty(cronSchedule))
{
throw new Exception($"No Quartz.NET Cron schedule found for job in configuration at {configKey}");
}
// register the job as before
var jobKey = new JobKey(jobName);
if (keyValuePairs != null && isJobDetailJobDataMap)
{
switch (isJobDetailJobDataMap)
{
case true:
quartz.AddJob
.WithIdentity(jobKey)
.UsingJobData(new JobDataMap(keyValuePairs)));
quartz.AddTrigger(opts => opts
.ForJob(jobKey)
.WithIdentity(jobName + "-trigger")
.WithCronSchedule(cronSchedule)); // use the schedule from configuration
break;
case false:
quartz.AddJob
.WithIdentity(jobKey));
quartz.AddTrigger(opts => opts
.ForJob(jobKey)
.WithIdentity(jobName + "-trigger")
.WithCronSchedule(cronSchedule)
.UsingJobData(new JobDataMap(keyValuePairs))); // use the schedule from configuration
break;
}
}
else
{
quartz.AddJob
.WithIdentity(jobKey));
quartz.AddTrigger(opts => opts
.ForJob(jobKey)
.WithIdentity(jobName + "-trigger")
.WithCronSchedule(cronSchedule)); // use the schedule from configuration
}
}
相关文章
- ASP.NET Maker(代码自动生成工具)v2020.0.4.1 特别安装版(附激活教程)
- .net和c#有什么区别
- 请求添加或删除指定服务器上的功能失败.net Framework 3.5安装报错的解决方法
- microsoft.net framework 4.5是什么
- ASP.NET 中 Button、LinkButton和ImageButton 三种控件的使用详解
- ASP.NET Core MVC基础学习之局部视图(Partial Views)
- Asp.Net上传文件并配置可上传大文件的方法
- .Net Core 下使用ZKWeb.System.Drawing实现验证码功能(图形验证码)
- 详解Asp.net web.config customErrors 如何设置
- ASP.NET MVC DropDownList数据绑定及使用详解