Quartz.Net快速上手

 最近在研究微信公众号,里面需要定期更新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 precedence

quartz.scheduler.instanceName = QuartzTest

# configure thread pool info

quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz

quartz.threadPool.threadCount = 10

quartz.threadPool.threadPriority = Normal

# job initialization plugin handles our xml reading, without it defaults are used

quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz

quartz.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

上神,你怎么看
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值