标签:init 邮件 document obb add hub cto 任务 syn
Open-source job scheduling system for .NET
Quartz.net 是调度任务框架,我们可以用来定时发送邮件、定时处理邮件、定时统计分析数据、定时监控...
本文介绍Quartz.net的简单使用
官方Doc https://www.quartz-scheduler.net/documentation/quartz-3.x/quick-start.html
远程调度GitHub https://github.com/guryanovev/CrystalQuartz
Quartz.net 启用方式大致分为三种
程序配置
XML方式配置
// 实例化调度 Scheduler
StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
await scheduler.Start();
// 定义任务
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("job1", "group1")
.Build();
// 定义触发器
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(10)
.RepeatForever())
.Build();
// 传入job和trigger开始调度任务
await scheduler.ScheduleJob(job, trigger);
// Grab the Scheduler instance from the Factory
StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
// and start it off
await scheduler.Start();
// define the job and tie it to our HelloJob class
IJobDetail helloJob = JobBuilder.Create<HelloJob>()
.WithIdentity("HelloJob", "HelloJobGroup")
.Build();
// define the job and tie it to our HelloJob class
IJobDetail goodBayJob = JobBuilder.Create<GoodBayJob>()
.WithIdentity("GoodBayJob", "GoodBayJobGroup")
.Build();
// Trigger the job to run now, and then repeat every 10 seconds
ITrigger helloJobTrigger = TriggerBuilder.Create()
.WithIdentity("HelloJobTrigger", "HelloJobGroup")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(10)
.RepeatForever())
.Build();
ITrigger goodBayTrigger = TriggerBuilder.Create()
.WithIdentity("GoodBayTrigger", "GoodBayJobGroup")
.StartNow()
.WithCronSchedule("0/10 * * * * ?")
.Build();
var jobs = new Dictionary<IJobDetail, IReadOnlyCollection<ITrigger>> {
{ helloJob,new HashSet<ITrigger>() { helloJobTrigger }},
{ goodBayJob,new HashSet<ITrigger>() { goodBayTrigger }},
};
await scheduler.ScheduleJobs(jobs, false);
ITrigger goodBayTrigger = TriggerBuilder.Create()
.WithIdentity("GoodBayTrigger", "GoodBayJobGroup")
.StartNow()
.WithCronSchedule("0/10 * * * * ?")
.Build();
更多Cron语法可参考
https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/crontrigger.html#introduction
[DisallowConcurrentExecution]
public class HelloJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
await Console.Out.WriteLineAsync("Greetings from HelloJob!");
}
}
为job增加了特性[DisallowConcurrentExecution],可以保证当前job还未执行结束前,就算触发时间已经到了,也不能再次开启job。保证job的工作是串行进行,而非并行。在实践过程中要根据具体情况考虑是否需要增加此特性(如果还无法确定是否需要添加,建议都添加上此特性~)
[DisallowConcurrentExecution] is an attribute that can be added to the Job class that tells Quartz not to execute multiple instances of a given job definition (that refers to the given job class) concurrently.
标签:init 邮件 document obb add hub cto 任务 syn
原文地址:https://www.cnblogs.com/Iannnnnnnnnnnnn/p/14435004.html