Schedule

  1. 相关属性设置

    • 扩展属性方式

      var host = Host.CreateDefaultBuilder()
      .ConfigureServices(services =>
      {
      services.AddQuartz(opt =>
      {
      // 主键
      opt.SchedulerId = "";
      // 调度名称
      opt.SchedulerName = "";
      // 最大并发(一次运行Job的最大数)
      opt.MaxBatchSize = "";
      // 可中断作业
      opt.InterruptJobsOnShutdown = true;
      // 关机时中断作业可等待
      opt.InterruptJobsOnShutdownWithWait = true;
      // 批量设置触发器的执行的提前时间
      q.BatchTriggerAcquisitionFireAheadTimeWindow = TimeSpan.Zero;
      });
      })
      .Build();
    • 手动实例化

      var scheduler = ScheduleBuilder().Create()
      .WithMisfireThreshold(TimeSpan.FromDays(1))
      .WithId("")
      .WithName("")
      .WithMaxBatchSize(2)
      .WithInterruptJobsOnShutdown(true)
      .WithInterruptJobsOnShutdownWithWait(true)
      // 提前1分钟进行批量执行 .WithBatchTriggerAcquisitionFireAheadTimeWindow(TimeSpan.FromMilliseconds(1))
      .Build();
  2. StdSchedulerFactory

    默认使用的调度工厂,相关配置文件存储在本地进行加载。

  3. DirectSchedulerFactory

    不建议使用,除非:对于Quartz.NET很熟悉并且所有配置项都能通过代码进行配置

  4. Loggin

    • 从3.1开始,可以通过配置Microsoft.Extensions.Logging.Abstractions来代替 LibLog library

    • 实例化配置

      // obtain your logger factory, for example from IServiceProvider
      ILoggerFactory loggerFactory = ...; // Quartz 3.1
      Quartz.LogContext.SetCurrentLogProvider(loggerFactory); // Quartz 3.2 onwards
      Quartz.Logging.LogContext.SetCurrentLogProvider(loggerFactory);
    • 使用Microsoft DI 集成进行配置

      services.AddQuartz(q =>
      {
      // this automatically registers the Microsoft Logging
      });

Clustering

  • 集群配置,仅适用于JobStore设置为JobStoreTX时

  • 如果IJobDetail中RequestRecovery设置为true,负载均衡和作业故障转移会被启用

  • 通过配置 quartz.jobStore.clustered=true来启用集群模式,集群中每个实例都有相同的副本

    • 每个节点必须有一个唯一的instanceId(quartz.scheduler.instanceId)

    注:

    1.单独的机器不要运行集群,

    2.同一个数据库不能在集群启动后在链接一个非集群的实例

    3.如果某个节点CPU到达了100%时,请优化它,不然会存在丢失作业的风险

配置资源

​ 默认情况下,StdSchedulerFactory会从当前工作目录中加载quartz.config文件,如果失败,则会加载quartz.dll中的配置文件。非默认情况下可通过修改quartz.properties进行文件设置

​ 可使用以下四种方式进行配置信息

  1. 通过Initialize或StdSchedulerFactory方法修改

    // 创建Collection
    System.Collections.Specialized.NameValueCollection collection = new System.Collections.Specialized.NameValueCollection();
    // 新增key和value
    collection.Add("quartz.threadPool.ThreadCount","20");
    // 实例化调度工厂
    var factory= new StdSchedulerFactory(collection);
    // 也可以通过factory.Initialize(NameValueCollection)来初始化
    // Initialize必须在GetScheduler方法之前
    factory.Initialize(collection);
    // 获得调度实例
    var scheduler= await factory.GetScheduler();
  2. App.config配置

    .net Framework生效

    <?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.threadPool.ThreadCount" value="11"/>
    </quartz>
    <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    </configuration>
  3. quartz.config配置

  4. Enviroment(环境变量配置)

    // 设置环境变量
    Environment.SetEnvironmentVariable("quartz.threadPool.ThreadCount","50");
    System.Collections.Specialized.NameValueCollection collection = new System.Collections.Specialized.NameValueCollection();
    var factory = new StdSchedulerFactory();
    var scheduler= await factory.GetScheduler();
主要配置项
配置项名称 Required 类型 默认值 说明
quartz.scheduler.instanceName no string 'QuartzScheduler' 客户端/集群:区分调度器
quartz.scheduler.instanceId no string 'NON_CLUSTERED' 唯一键
AUTO:自动生成
SYS_PROP:系统属性生成
quartz.scheduler.instanceIdGenerator.type no string Quartz.Simpl.SimpleInstanceIdGenerator, Quartz instanceId设置为AUTO时生效
quartz.scheduler.threadName no string instanceName + '_QuartzSchedulerThread' 调度线程名称
quartz.scheduler.makeSchedulerThreadDaemon no boolean false 是否为守护线程
quartz.scheduler.idleWaitTime no long 30000 空闲时,重新查询触发器的等待时间,不能小于5000ms
quartz.scheduler.typeLoadHelper.type no string Quartz.Simpl.SimpleTypeLoadHelper Type.GetType()加载
quartz.scheduler.jobFactory.type no string Quartz.Simpl.PropertySettingJobFactory 工厂的类型名称
quartz.context.key.SOME_KEY no string none 设置“quartz.context.key.MyKey=MyValue” 将执行schedule.context.Put(“MyKey”,“MyValue”)
quartz.scheduler.batchTriggerAcquisitionMaxCount no int 1 集群使用,当前节点最大触发数
quartz.scheduler.batchTriggerAcquisitionFireAheadTimeWindow no long 0 在计划触发之前可以获取几个触发器,数量越大越不精确触发
线程池配置项
Property Name Required Type Default Value 说明
quartz.threadPool.type no string Quartz.Simpl.DefaultThreadPool, Quartz 线程池类型
quartz.threadPool.maxConcurrency no int 10 最大并发数

自定义线程池

/*
系统自带
DedicatedThreadPool 自定义了线程池(声明了专用的线程池)
TaskSchedulingThreadPool 使用TaskScheduler调度任务
ZeroSizeThreadPool 零大小线程池
*/
// MyLibrary为dll名称
// MyLibrary.FooThreadPool 命名空间+类名
quartz.threadPool.type = MyLibrary.FooThreadPool, MyLibrary
// 未给出说明
quartz.threadPool.somePropOfFooThreadPool = someValue
监听配置项

可以由StdSchedulerFactory实例化和配置

// MyListenerType必须有无参构造函数
//触发器注册监听(全局)
// 空间+类型, dll名称
quartz.triggerListener.NAME.type = MyLibrary.MyListenerType, MyLibrary
// 属性名称
quartz.triggerListener.NAME.propName = propValue
// 属性名称
quartz.triggerListener.NAME.prop2Name = prop2Value
// 作业注册监听(全局)
quartz.jobListener.NAME.type = MyLibrary.MyListenerType, MyLibrary
quartz.jobListener.NAME.propName = propValue
quartz.jobListener.NAME.prop2Name = prop2Value
插件配置项

与监听类似,配置一个插件,插件类型必须有无参构造函数

quartz.plugin.NAME.type = MyLibrary.MyPluginType, MyLibrary
quartz.plugin.NAME.propName = propValue
quartz.plugin.NAME.prop2Name = prop2Value
  • 日志记录触发器历史插件

    // 引用插件,需要有Quartz.Plugins.dll文件
    quartz.plugin.triggHistory.type = Quartz.Plugin.History.LoggingTriggerHistoryPlugin, Quartz.Plugins
    // 启动信息
    quartz.plugin.triggHistory.triggerFiredMessage = Trigger {1}.{0} fired job {6}.{5} at: {4:HH:mm:ss MM/dd/yyyy}
    // 完成信息
    quartz.plugin.triggHistory.triggerCompleteMessage = Trigger {1}.{0} completed firing job {6}.{5} at {4:HH:mm:ss MM/dd/yyyy} with resulting trigger instruction code: {9}
  • XML调度数据处理器插件

    作业初始化插件从XML文件中读取一组作业和触发器,并在初始化期间将它们添加到调度程序中;它还可以删除现有数据。

    quartz.plugin.jobInitializer.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins
    // 读取的文件路径
    quartz.plugin.jobInitializer.fileNames = data/my_job_data.xml
    // 未找到报错
    quartz.plugin.jobInitializer.failOnFileNotFound = true
  • 关机钩子插件

    捕获CLR终止的事件,并在调度程序上调用shutdown

    quartz.plugin.shutdownhook.type = Quartz.Plugin.Management.ShutdownHookPlugin, Quartz.Plugins
    quartz.plugin.shutdownhook.cleanShutdown = true
  • 作业中断监视器插件

    捕获作业长时间运行(超过配置的最大时间)的事件,并告诉调度程序在启用时“尝试”中断它。插件默认为5分钟后发出信号中断,但默认的defaultMaxRunTime配置为不同的值,配置值以毫秒为单位。

    quartz.plugin.jobAutoInterrupt.type = Quartz.Plugin.Interrupt.JobInterruptMonitorPlugin, Quartz.Plugins
    quartz.plugin.jobAutoInterrupt.defaultMaxRunTime = 3000000
远程服务端客户端
Property Name Required Type Default Value 说明
quartz.scheduler.exporter.type yes string Quartz.Simpl.RemotingSchedulerExport,Quartz 只有这一种
quartz.scheduler.exporter.port yes int 端口号
quartz.scheduler.exporter.bindName no string QuartzScheduler QuartzScheduler为对外暴露的名称
quartz.scheduler.exporter.channelType no string 'tcp' tpc/http tcp性能高
quartz.scheduler.exporter.channelName no string 'http' 对外暴露的通道名称
quartz.scheduler.exporter.typeFilterLevel no string 'Full' Low:低反序列化级别
Full:完整的反序列化类型
quartz.scheduler.exporter.rejectRemoteRequests no boolean false 是否接受远程请求
RAMJobStore
  • 必须配置项
quartz.jobStore.type = Quartz.Simpl.RAMJobStore, Quartz
Property Name Required Type Default Value 说明
quartz.jobStore.misfireThreshold no int 60000 触发器执行触发的最小时间间隔
JobStoreTx(ADO.NET)
  • 必须配置项
quartz.jobStore.type = Quartz.Impl.AdoJobStore.JobStoreTX, Quartz
Property Name Required Type Default Value 说明
quartz.jobStore.dbRetryInterval no long 15000 (15 seconds) 间隔15秒重新连接
quartz.jobStore.driverDelegateType yes string null 参见下文
quartz.jobStore.dataSource yes string null 数据库名称
quartz.jobStore.tablePrefix no string "QRTZ_" 表前缀
quartz.jobStore.useProperties no boolean false true:使用BLOB类型存储
quartz.jobStore.misfireThreshold no int 60000 触发器执行触发的最小时间间隔-ms
quartz.jobStore.clustered no boolean false 是否开启集群
quartz.jobStore.clusterCheckinInterval no long 15000 检查集群周期 -ms
quartz.jobStore.maxMisfiresToHandleAtATime no int 20 最大处理错误数
quartz.jobStore.selectWithLockSQL no string "SELECT * FROM {0}LOCKS WHERE SCHED_NAME = {1} AND LOCK_NAME = ? FOR UPDATE" “{0}”将替换为上面配置的tablePrefix;“{1}”替换为调度程序的名称。
quartz.jobStore.txIsolationLevelSerializable no boolean false true:设置事务级别
quartz.jobStore.acquireTriggersWithinLock no boolean false (or true - see doc below) 用于锁定作业存储数据的控制
quartz.jobStore.lockHandler.type no string null 默认即可,自定义可参见下文StdRowLockSemaphore
quartz.jobStore.driverDelegateInitString no string null
  • quartz.jobStore.driverDelegateType

    可选择类型如下:

    • Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz - default when no specific implementation available
    • Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz - for Microsoft SQL Server
    • Quartz.Impl.AdoJobStore.PostgreSQLDelegate, Quartz
    • Quartz.Impl.AdoJobStore.OracleDelegate, Quartz
    • Quartz.Impl.AdoJobStore.SQLiteDelegate, Quartz
    • Quartz.Impl.AdoJobStore.MySQLDelegate, Quartz
  • quartz.jobStore.lockHandler.type

    自定义 StdRowLockSemaphore

    // 自定义设置
    quartz.jobStore.lockHandler.type = Quartz.Impl.AdoJobStore.StdRowLockSemaphore
    quartz.jobStore.lockHandler.maxRetry = 7 # Default is 3
    quartz.jobStore.lockHandler.retryPeriod = 3000 # Default is 1000 millis
DataSources(ADO.NET JobStores)
Property Name Required Type 说明
quartz.dataSource.NAME.provider yes string
quartz.dataSource.NAME.connectionString no string 连接字符串
quartz.dataSource.NAME.connectionStringName no string app.config or appsettings.json定义的连接名称
quartz.dataSource.NAME.connectionProvider.type no string 自定义连接提供程序
  • quartz.dataSource.NAME.provider

    可设置列表如下:

    • SqlServer - Microsoft SQL Server

    • OracleODP - Oracle's Oracle Driver

    • OracleODPManaged - Oracle's managed driver for Oracle 11

    • MySql - MySQL Connector/.NET

    • SQLite - SQLite ADO.NET Provider

    • SQLite-Microsoft - Microsoft SQLite ADO.NET Provider

    • Firebird - Firebird ADO.NET Provider

    • Npgsql - PostgreSQL Npgsql

      该列表与JobStore不同,以此为准。

Clustering 集群

​ 官网提供的全量集群配置信息,具体配置项可参见Clustering

#============================================================================
# Configure Main Scheduler Properties
#============================================================================ quartz.scheduler.instanceName = MyClusteredScheduler
quartz.scheduler.instanceId = AUTO #============================================================================
# Configure ThreadPool
#============================================================================ quartz.threadPool.type = Quartz.Simpl.DefaultThreadPool, Quartz
quartz.threadPool.threadCount = 25
quartz.threadPool.threadPriority = 5 #============================================================================
# Configure JobStore
#============================================================================ quartz.jobStore.misfireThreshold = 60000 quartz.jobStore.type = Quartz.Impl.AdoJobStore.JobStoreTX
quartz.jobStore.driverDelegateType = Quartz.Impl.AdoJobStore.SqlServerDelegate
quartz.jobStore.useProperties = true
quartz.jobStore.dataSource = myDS
quartz.jobStore.tablePrefix = QRTZ_ quartz.jobStore.clustered = true
quartz.jobStore.clusterCheckinInterval = 20000 #============================================================================
# Configure Datasources
#============================================================================ quartz.dataSource.myDS.provider = SqlServer
quartz.dataSource.myDS.connectionString = Server=localhost;Database=quartznet;User Id=quartznet;Password=quartznet;

Quartz.Net 官方教程 Tutorial 3/3的更多相关文章

  1. Quartz.net官方开发指南系列篇

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...

  2. Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译

    本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

  3. Unity性能优化(4)-官方教程Optimizing graphics rendering in Unity games翻译

    本文是Unity官方教程,性能优化系列的第四篇<Optimizing graphics rendering in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

  4. Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译

    本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 相关文章: ...

  5. [爬虫] 学Scrapy,顺便把它的官方教程给爬下来

    想学爬虫主要是因为算法和数据是密切相关的,有数据之后可以玩更多有意思的事情,数据量大可以挖掘挖掘到更多的信息. 之前只会通过python中的request库来下载网页内容,再用BeautifulSou ...

  6. Note | PyTorch官方教程学习笔记

    目录 1. 快速入门PYTORCH 1.1. 什么是PyTorch 1.1.1. 基础概念 1.1.2. 与NumPy之间的桥梁 1.2. Autograd: Automatic Differenti ...

  7. Unity性能优化(1)-官方教程The Profiler window翻译

    本文是Unity官方教程,性能优化系列的第一篇<The Profiler window>的简单翻译. 相关文章: Unity性能优化(1)-官方教程The Profiler window翻 ...

  8. jeecg表单页面控件权限设置(请先看官方教程,如果能看懂就不用看这里了)

    只是把看了官方教程后,觉得不清楚地方补充说明一下: 1. 2. 3. 4.用"jeecgDemoController.do?addorupdate"这个路径测试,不出意外现在应该可 ...

  9. [转]Google Guava官方教程(中文版)

    Google Guava官方教程(中文版) http://ifeve.com/google-guava/

  10. Google Guava官方教程(中文版)

    Google Guava官方教程(中文版) 原文链接  译文链接 译者: 沈义扬,罗立树,何一昕,武祖  校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库, ...

随机推荐

  1. Android10 dex2oat实践

    最近看到一篇博客:Android性能优化之Android 10+ dex2oat实践,对这个优化很感兴趣,打算研究研究能否接入到项目中.不过该博客只讲述了思路,没有给完整源码.本项目参考该博客的思路, ...

  2. 开箱即用 yyg-cli(脚手架工具):快速创建 vue3 组件库和vue3 全家桶项目

    1 yyg-cli 是什么 yyg-cli 是优雅哥开发的快速创建 vue3 项目的脚手架.在 npm 上发布了两个月,11月1日进行了大升级,发布 1.1.0 版本:支持创建 vue3 全家桶项目和 ...

  3. js高级之函数高级部分

    基于尚硅谷的尚硅谷JavaScript高级教程提供笔记撰写,加入一些个人理解 github源码 博客下载 原型与原型链 prototype : 显式原型属性,它默认指向一个Object空对象(即称为: ...

  4. springboot集成支付宝的支付(通用版)

    [1.引依赖] <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sd ...

  5. C#使用Task在Winform建立控件上的提示等待窗口,实现局部等待加载,不影响主线程(二)

    效果图: 源码:(处理了亿点点细节) 链接:https://pan.baidu.com/s/18S1IgQBOyXgeGvhnU3nrKQ?pwd=jpq9提取码:jpq9 作者:兮去博客出处:htt ...

  6. java集合类 collection接口,List集合

    java集合类:collection接口,List集合 在java.util包中提供了一些集合类,集合类又被称为容器,常用的有List集合,Set集合,Map集合.下面将介绍collection接口和 ...

  7. Easy-Classification-验证码识别

    1.背景 Easy-Classification是一个应用于分类任务的深度学习框架,它集成了众多成熟的分类神经网络模型,可帮助使用者简单快速的构建分类训练任务.   案例源代码 Easy-Classi ...

  8. MathNet用到的一些功能

    1.计算一元线性函数 Tuple<double, double> myLineTuple = MathNet.Numerics.Fit.Line(myXArray, myYArray); ...

  9. feDisplacementMap滤镜实现水波纹效果,计算动态值。

    参考资料 https://www.zhangxinxu.com/wordpress/2017/12/understand-svg-fedisplacementmap-filter/ 该文章已经讲的特别 ...

  10. 用Python来写个小型购物车程序

    0x1 前言 Python语言能做很多东西的,像数据分析啊.自动化.开发. 爬虫(真的很棒哟,初学者玩很有成就感的啊哈哈)等等还有挺多. 0x2 用Python编写的一个小型购物车程序 import ...