Quartz.Net快速上手
来源:互联网 发布:springmvc注解详解源码 编辑:程序博客网 时间:2024/06/07 20:07
最近在研究微信公众号,里面需要定期更新access_token,于是就想到了Quartz.Net,但无奈网上的文档要么版本太早,要么程序不能运行,让我走了不少弯路,在此给大家分享下我的经验。
可能你已经对它有所了解,但还是老套的现介绍一下。
Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等。 Quartz.NET允许开发人员根据时间间隔(或天)来调度作业。它实现了作业和触发器的多对多关系,还能把多个作业与不同的触发器关联,配置灵活方便。
官方网址:https://www.quartz-scheduler.net/
官方文档:https://www.quartz-scheduler.net/documentation/index.html
下载地址:https://www.quartz-scheduler.net/download.html
本文用的是Quartz.NET2.6.1。
第一步 新建解决方案和相关项目,并安装相关程序包
使用nuget安装Quartz.NET
点击“工具”->"NuGet包管理器"->“程序包管理器控制台”
Install-Package Quartz
第二步 创建两个Job类Job1,实现IJob,在Execute方法里编写要处理的业务逻辑
/// <summary> /// 实现IJob接口 /// </summary> public class Job1 : IJob { public void Execute(IJobExecutionContext context) { try { Console.WriteLine("Job1 任务运行开始"); Console.WriteLine("Job1任务运行结束"); } catch (Exception ex) { Console.WriteLine("Job1 运行异常", ex); } } }
第三步 配置quartz.config、quartz_jobs.xml
quartz.config主要是实例的基础配置。
# You can configure your scheduler in either <quartz> configuration section# or in quartz properties file# Configuration section has precedencequartz.scheduler.instanceName = QuartzTest# configure thread pool infoquartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartzquartz.threadPool.threadCount = 10quartz.threadPool.threadPriority = Normal# job initialization plugin handles our xml reading, without it defaults are usedquartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartzquartz.plugin.xml.fileNames = ~/test/quartz_jobs.xml# export this server to remoting context#quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz#quartz.scheduler.exporter.port = 555#quartz.scheduler.exporter.bindName = QuartzScheduler#quartz.scheduler.exporter.channelType = tcp#quartz.scheduler.exporter.channelName = httpQuartz
quartz.plugin.xml.fileNames是quartz_jobs.xml的完整路径。
quartz_jobs.xml是各个Job 任务的配置。
<?xml version="1.0" encoding="utf-8" ?><job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> <processing-directives> <overwrite-existing-data>true</overwrite-existing-data> </processing-directives> <schedule> <job> <name>Job1</name> <group>JobGroup</group> <description>Quartz Job1</description> <job-type>ConsoleApp1.Job1,ConsoleApp1</job-type> <durable>true</durable> <recover>false</recover> </job> <trigger> <cron> <name>Job1Trigger</name> <group>JobTriggerGroup</group> <job-name>Job1</job-name> <job-group>JobGroup</job-group> <cron-expression>0/10 * * * * ?</cron-expression> </cron> </trigger> </schedule></job-scheduling-data>
配置内容会在以后的文章中说明
第四步 宿主程序,本文才用的是控制台应用。
class Program { private static IScheduler scheduler; static void Main(string[] args) { ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); scheduler = schedulerFactory.GetScheduler(); scheduler.Start(); Thread.Sleep(TimeSpan.FromSeconds(60)); scheduler.Shutdown(); } }
执行结果如下:
- Quartz.Net快速上手
- GENTLE.NET快速上手
- .net core快速上手
- NET 2.0 SqlDependency快速上手指南
- Spring.net 基础教程快速上手指南
- 快速上手
- Quartz.NET学习系列(一)--- 快速入门
- 上手Quartz(转)
- 上手Quartz(转)
- 怎样在两个星期内快速上手.NET
- ado.net快速上手实践篇(一)
- ado.net快速上手实践篇(二)
- 在 ASP.NET 環境下使用 Memcached 快速上手指南
- 在 ASP.NET 環境下使用 Memcached 快速上手指南
- 在 ASP.NET 環境下使用 Memcached 快速上手指南
- ADO.NET 4 快速上手(1)——概述
- ADO.NET 4 快速上手(6)——事务
- ADO.NET 4 快速上手(10)——LinQ
- 保险1-概述
- 朴素贝叶斯——完整代码
- Selenium WebDriver 中鼠标事件
- SpringMVC使用@RequestBody注解报400错误
- Volatile总结
- Quartz.Net快速上手
- leetcode 341. Flatten Nested List Iterator
- 二叉树专题-lintcode路径相关
- 融云即时聊天通讯的简单使用
- http://www.52nlp.cn/tag/tensorflow Andrew Ng (吴恩达) 深度学习课程小结 tensorflow
- 程序员业余时间修炼指南
- 安卓中相机相册调用总结
- libcoap安装中openssl路径问题
- 加载assetbundle坑记录