• Quartz.net 定时计划使用


      新建解决方案和工程Quartz.net

      使用Power Shell 命令 Install-Package Quartz 导入Quartz.net程序集

      新建一个计划TestJob

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Quartz.net
    {
        public class TestJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {
                try
                {
                    Console.WriteLine("执行计划...");
                }
                catch (Exception)
                {
    
                    throw;
                }
            }
        }
    }

        从程序中控制计划时间的用法

    using Quartz.Impl;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Quartz.net
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 如果配置了Log4Net 可取消注释
                //Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter { Level = Common.Logging.LogLevel.Info };
    
                // 从工厂里获取调度实例
                IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
                scheduler.Start();
    
                // 创建一个任务
                IJobDetail job = JobBuilder.Create<TestJob>()
                    .WithIdentity("MyJob", "group1")
                    .Build();
    
                // 创建一个触发器
                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("trigger1", "group1")
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInSeconds(10) // 每10秒执行一次
                        .RepeatForever()) // 无限次执行
                    .Build();
    
                // 每天执行的触发器
                ITrigger t = TriggerBuilder.Create()
                   .WithIdentity("myTrigger", "group2")
                   .ForJob(job)
                   .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(9, 30)) // 每天9:30执行一次
                    //.ModifiedByCalendar("myHolidays") // but not on holidays 设置那一天不知道
                   .Build();
    
                // 每天执行的将触发器换成天的就可以了
                scheduler.ScheduleJob(job, trigger); 
            }
        }
    }

      从配置文件设置定时计划的用法,先看App.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </configSections>
    
      <quartz>
        <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler" />
        <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
        <add key="quartz.threadPool.threadCount" value="10" />
        <add key="quartz.threadPool.threadPriority" value="2" />
    
        <add key="quartz.jobStore.misfireThreshold" value="60000" />
        <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />
    
        <!--******************************Plugin配置********************************************* -->
        <add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" />
        <add key="quartz.plugin.xml.fileNames" value="~/quartz_jobs.xml"/>
      </quartz>
    
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
    </configuration>

      再看quartz_jobs.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <!-- This file contains job definitions in schema version 2.0 format -->
    <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>
          <!--任务名称,同一个group中多个job的name不能相同-->
          <name>TestJob</name>
          <!--任务分组-->
          <group>sampleGroup</group>
          <!--任务描述-->
          <description>Sample job for Quartz Server</description>
          <!--任务类型,任务的具体类型及所属程序集-->
          <job-type>Quartz.NETConfig.TestJob, Quartz.NETConfig</job-type>
          <durable>true</durable>
          <recover>false</recover>
        </job>
    
        <!--任务触发器-->
        <trigger>
          <!--简单任务的触发器,可以调度用于重复执行的任务-->
          <simple>
            <!--触发器名称,同一个分组中的名称必须不同-->
            <name>sampleSimpleTrigger</name>
            <!--触发器组-->
            <group>sampleSimpleGroup</group>
            <!--描述-->
            <description>Simple trigger to simply fire sample job</description>
            <!--要调度的任务名称,该job-name必须和对应job节点中的name完全相同-->
            <job-name>TestJob</job-name>
            <!--调度任务(job)所属分组,该值必须和job中的group完全相同-->
            <job-group>sampleGroup</job-group>
            <!--任务开始时间-->
            <start-time>2012-04-01T08:00:00+08:00</start-time>
            <misfire-instruction>SmartPolicy</misfire-instruction>
            <!--任务执行次数  -1 为无限次执行-->
            <repeat-count>-1</repeat-count>
            <!--任务触发间隔(毫秒)-->
            <repeat-interval>10000</repeat-interval>
            <!--每3秒中执行一次-->
          </simple>
        </trigger>
    
        <job>
          <!--任务名称,同一个group中多个job的name不能相同-->
          <name>StatisticsJob</name>
          <!--任务分组-->
          <group>sampleGroup1</group>
          <!--任务描述-->
          <description>Sample job for Quartz Server</description>
          <!--任务类型,任务的具体类型及所属程序集-->
          <job-type>Quartz.NETConfig.StatisticsJob, Quartz.NETConfig</job-type>
          <durable>true</durable>
          <recover>false</recover>
        </job>
        <trigger>
          <cron>
            <name>TriggerJob001</name>
            <group>ProjectJobs</group>
            <job-name>StatisticsJob</job-name>
            <job-group>sampleGroup1</job-group>
            <!--秒 分 时 日(星期) 月 年-->
            <cron-expression>0/3 * * * * ?</cron-expression>
          </cron>
        </trigger>
    
        <!--每天0点定时执行 0 0 0/1 * * ? -->
        <job>
          <name>DefaultJob</name>
          <group>sampleGroup2</group>
          <description>Sample job for Quartz Server</description>
          <job-type>Quartz.NETConfig.DefaultJob, Quartz.NETConfig</job-type>
          <durable>true</durable>
          <recover>false</recover>
        </job>
        <trigger>
          <cron>
            <name>TriggerJob002</name>
            <group>ProjectJobs</group>
            <job-name>DefaultJob</job-name>
            <job-group>sampleGroup2</job-group>
            <cron-expression>0 0 0/1 * * ?</cron-expression>
          </cron>
        </trigger>
        
      </schedule>
    </job-scheduling-data>

       用配置调比较简单

    // 从工厂里获取调度实例
                IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
                scheduler.Start();

      配置的任务也没什么

    public class DefaultJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {
                try
                {
                    // 开始统计数据
                    Console.WriteLine("开始统计数据...");
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
        }
    public class StatisticsJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {
                Console.WriteLine("表达式执行调度任务...");
            }
        }
    public class TestJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {
                Console.WriteLine("执行调度任务");
            }
        }

      以上就是定时计划的用法,可参考文档 http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/more-about-triggers.html

  • 相关阅读:
    R
    P
    O
    M
    二分算法的一些思考
    I
    H
    G
    5-46 新浪微博热门话题 (30分)——unfinished HASH
    BZOJ 1179: [Apio2009]Atm
  • 原文地址:https://www.cnblogs.com/zhouxiaoyun/p/5781399.html
Copyright © 2020-2023  润新知