.net core中Quartz的使用方法

我们在日常开发中,总会遇到这样的需求:每隔一段时间,执行一次某个任务。固定某个时间执行任务,例如凌晨12点对当天的数据进行统计。每个月的第几天,执行某个任务。

Quartz.Net是根据Java的Quartz用C#改写而来,Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等。Quartz.NET允许开发人员根据时间间隔来调度作业。

fcfc0ea208d3bbad261f49d0f1b575ce.png

Quartz版本:3.3.2

1.NuGet中下载安装Quartz

2.新建Quartz帮助类

(1)新建JobSchedule类

public class JobSchedule{public JobSchedule(Type jobType, string cronExpression){JobType = jobType;CronExpression = cronExpression;}public Type JobType { get; }public string CronExpression { get; }

新建QuartzFactory类

public class QuartzFactory : IJobFactory{private readonly IServiceProvider _serviceProvider;public QuartzFactory(IServiceProvider serviceProvider){_serviceProvider = serviceProvider;}public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler){return _serviceProvider.GetRequiredService(bundle.JobDetail.JobType) as IJob;}public void ReturnJob(IJob job){ }

新建QuartzService类

public class QuartzHostedService : IHostedService{private readonly ISchedulerFactory _schedulerFactory;private readonly IJobFactory _jobFactory;private readonly IEnumerable<JobSchedule> _jobSchedules;public QuartzHostedService(ISchedulerFactory schedulerFactory,IJobFactory jobFactory,IEnumerable<JobSchedule> jobSchedules){_schedulerFactory = schedulerFactory;_jobSchedules = jobSchedules;_jobFactory = jobFactory;}public IScheduler Scheduler { get; set; }public async Task StartAsync(CancellationToken cancellationToken){Scheduler = await _schedulerFactory.GetScheduler(cancellationToken);Scheduler.JobFactory = _jobFactory;foreach (var jobSchedule in _jobSchedules){var job = CreateJob(jobSchedule);var trigger = CreateTrigger(jobSchedule);await Scheduler.ScheduleJob(job, trigger, cancellationToken);}await Scheduler.Start(cancellationToken);}public async Task StopAsync(CancellationToken cancellationToken){await Scheduler?.Shutdown(cancellationToken);}private static IJobDetail CreateJob(JobSchedule schedule){var jobType = schedule.JobType;return JobBuilder.Create(jobType).WithIdentity(jobType.FullName).WithDescription(jobType.Name).Build();}private static ITrigger CreateTrigger(JobSchedule schedule){return TriggerBuilder.Create().WithIdentity($"{schedule.JobType.FullName}.trigger").WithCronSchedule(schedule.CronExpression).WithDescription(schedule.CronExpression).Build();}}

在Startup.cs中注册代码

services.AddControllers();#region Quartz Service// Add Quartz servicesservices.AddSingleton<IJobFactory, QuartzFactory>();services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();// Add our jobservices.AddTransient<DeleteFileJob>();services.AddJob();services.AddHostedService<QuartzHostedService>();

Quartz.Net源码:

https://github.com/quartznet/quartznet

官方学习文档:

http://www.quartz-scheduler.net/documentation/index.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279790.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

windows10访客_如何在Windows 10中创建访客帐户

windows10访客If you find that your guests are asking fairly often to use your computer temporarily to check their email or look something up on the web, you don’t have to let them use your personal account or create a special account for each guest. 如果发…

几个有趣的算法题目

本文首发 http://svtter.cn最接近的数字 题目 一个K位的数N $$ (K\leq2000&#xff0c;N\leq10^{20}) $$ 找出一个比N大且最接近的数&#xff0c;这个数的每位之和与N相同&#xff0c;用代码实现之。 例如&#xff1a;0050 所求书数字为0104&#xff1b;112 所求数为121&#x…

获取一篇新闻的全部信息

给定一篇新闻的链接newsUrl&#xff0c;获取该新闻的全部信息 标题、作者、发布单位、审核、来源 发布时间:转换成datetime类型 点击&#xff1a; newsUrlnewsId(使用正则表达式re)clickUrl(str.format(newsId))requests.get(clickUrl)newClick(用字符串处理&#xff0c;或正则…

上twitter_如何在Twitter上更改您的显示名称

上twitterUnlike Facebook, Twitter has never insisted people user their real names. In fact, there’s a long tradition of people changing their names to a joke or pun because it’s Christmas or Halloween, or just for no reason at all. 与Facebook不同&#xf…

网桥

配置实现网桥 网桥&#xff1a;即桥接 把一套机器上的若干个网络接口 “连接” 起来&#xff0c;其结果是&#xff0c;其中一个网口收到的报文会被复制给其他网口并发送出去。以使得网口之间的报文能够互相转发。网桥就是这样一个设备&#xff0c;它有若干个网口&#xff0c;并…

raspberry pi_在月光下将Raspberry Pi变成蒸汽机

raspberry piValve’s Steam Machines aim to bring your Steam game library right into your living room (but at a rather steep premium). Today we’ll show you how to bring your Steam library (plus all your other computer games) to your living room for a fract…

MySql数据库出现 1396错误

1、安装MySql数据库后。创建新的用户。有可能会出现 1396这个错误&#xff0c; 2、解决的办法如下&#xff1a;假装有你需要创建的这个用户、先删了。再创建。 3、这样就可以解决用户创建不成功的问题了。 转载于:https://www.cnblogs.com/chifa/p/9362882.html

如何使用wink框架_如何解决Wink Hub的Z-Wave连接问题

如何使用wink框架Overall, the Wink hub works extremely well…but sometimes the devices you have connected to it can act a little wonky. Here are some things you can do in order to fix any connection issues with all of those Z-Wave sensors and devices connec…

谷歌相册_Google相册中的新存档功能是什么?

谷歌相册If you’re a Google Photos user, you’ve may have seen a new feature called “Archive” show up in the app’s sidebar. if not, don’t stress—it’s just now rolling out and not everyone has it yet. Since it’s new, here’s a quick look at what it i…

CenterOS 7安装Nginx

1.wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm下载对应当前系统版本的nginx包(package) 2.rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm建立nginx的yum仓库 3.yum install nginx 下载并安装nginx systemctl s…

阿里云一键建站产品,阿里云自营建站-中小企业建站首选

阿里云推出的自营建站服务&#xff0c;这对于中小企业来说简直是福利了&#xff0c;现在一般的公司都开始有了自己的官网&#xff0c;有可能就是因为你的官网设计的标准&#xff0c;大气&#xff0c;客户就会对你的信任度增加&#xff0c;从而促进一笔不小的订单&#xff0c;这…

航拍拉近拉远镜头_什么是远摄镜头?

航拍拉近拉远镜头Telephoto lenses can be incredibly useful, but how is it different from other lenses, and when should you use it? 远摄镜头可能非常有用&#xff0c;但是它与其他镜头有什么不同&#xff1f;何时使用&#xff1f; 什么是远摄镜头&#xff1f; (What I…

阿里云对象存储OSS支持版本管理特性

2019独角兽企业重金招聘Python工程师标准>>> 阿里云对象存储OSS现已经全面支持“对象版本管理”特性。该功能适用于所有的存储类型以及区域。当Bucket启用该特性后&#xff0c;“对象版本管理”功能可以保护和恢复误删除、误覆盖的数据。 对象存储OSS“版本管理”具…

Python第一天学习---基础语法

1.字符串的用法(String) Python 中的字符串有两种索引方式&#xff0c;从左往右以 0 开始&#xff0c;从右往左以 -1 开始。Python中的字符串不能改变。Python 没有单独的字符类型&#xff0c;一个字符就是长度为 1 的字符串这三点是我觉得Python字符处理特别的一点 我们来看第…

apple tv设置_如何设置Apple TV播放个人iTunes库

apple tv设置If you already have a lot of music and home videos in your iTunes library, you can easily stream it all to your Apple TV, and thus whatever output sources to which it is connected. 如果iTunes库中已经有很多音乐和家庭视频&#xff0c;则可以轻松地将…

亚马逊echo中国使用_如何使用亚马逊的主要照片备份所有照片

亚马逊echo中国使用Millions of people are Amazon Prime subscribers, but many of them don’t realize that in addition to free shipping and Prime Instant Video, they also get unlimited photo storage for all their computers and mobile devices. 数以百万计的人是…

抽象SQL查询:SQL-MAP技术的使用

什么是参数化查询&#xff1f;我们来看百度百科对此的定义和示例&#xff1a; 一&#xff0c;定义 ------------------------------------------------------------------ 参数化查询&#xff08;Parameterized Query 或 Parameterized Statement&#xff09;是指在设计与数据库…

如何使用echo.js实现图片的懒加载(整理)

如何使用echo.js实现图片的懒加载&#xff08;整理&#xff09; 一、总结 一句话总结&#xff1a;a、在img标签中添加data-echo属性加载真实图片&#xff1a;<img class"loading" src"blank.gif" data-echo"图片真实路径" />&#xff0c;在…

还原出厂设置 擦除frp_如何备份,擦除和还原Apple Watch

还原出厂设置 擦除frpThe Apple Watch is, in its own right, a little tiny computer with data backup and security needs. Read on as we show you how to ensure your Apple Watch is backed up, wiped, and restored just like you’d do with your smartphone. Apple Wa…

exchange 2010 search mailbox 的幕后强大功能

铃……….半夜中被一阵急促的手机铃声吵醒&#xff0c;年度服务客户打来电话需要进行邮件的排查和删除工作。问其原因&#xff0c;原来是组织中有人发了一封关于领导的不健康的邮件&#xff0c;并在企业内部进行了转发&#xff0c;领导要求立即找出此类邮件并进行删除。管理员深…