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

    QQ图片20171019104445.png

    

    第二步 创建两个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();        }    }

    执行结果如下:

QQ图片20171019105634.png

上神,你怎么看
原创粉丝点击