Quartz.Net 简单使用
0.介绍
Open-source job scheduling system for .NET
Quartz.net 是调度任务框架,我们可以用来定时发送邮件、定时处理邮件、定时统计分析数据、定时监控...
本文介绍Quartz.net的简单使用
1. 参考资料
官方Doc https://www.quartz-scheduler.net/documentation/quartz-3.x/quick-start.html
远程调度GitHub https://github.com/guryanovev/CrystalQuartz
2.核心内容
使用总结
Quartz.net 启用方式大致分为三种
-
程序配置
-
XML方式配置
XML方式配置job可以参考 https://www.cnblogs.com/z-huan/p/7412181.html
- 远程调度
远程调度job可以参考GitHub https://github.com/guryanovev/CrystalQuartz
程序配置简单使用
- 简单启用一个job
// 实例化调度 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);
- 简单启用多个job
// 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);
- 触发器使用Cron语法
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
- IJob实现任务
[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.
3.样例源码地址
作者:Iannnnnnnnnnnnn
出处:https://www.cnblogs.com/Iannnnnnnnnnnnn
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· .NET 8.0 + Linux 香橙派,实现高效的 IoT 数据采集与控制解决方案
· DeepSeek处理自有业务的案例:让AI给你写一份小众编辑器(EverEdit)的语法着色文件