案例:修改默认线程个数

1.NameValueCollection

            System.Collections.Specialized.NameValueCollection collection = new System.Collections.Specialized.NameValueCollection();

            collection.Add("quartz.threadPool.ThreadCount","");          

            var factory= new StdSchedulerFactory(collection);

            var scheduler= await factory.GetScheduler();

            await scheduler.Start();

            var metData=await scheduler.GetMetaData();

            Console.WriteLine(metData.ThreadPoolSize);

原理

通过反射实例化对象DefaultThreadPool

            Type tpType = loadHelper.LoadType(threadPoolTypeString) ?? typeof(DefaultThreadPool);

            try
{
tp = ObjectUtils.InstantiateType<IThreadPool>(tpType);
}
catch (Exception e)
{
initException = new SchedulerException("ThreadPool type '{0}' could not be instantiated.".FormatInvariant(tpType), e);
throw initException;
}
tProps = cfg.GetPropertyGroup(PropertyThreadPoolPrefix, true);
try
{
ObjectUtils.SetObjectProperties(tp, tProps);
}
catch (Exception e)
{
initException = new SchedulerException("ThreadPool type '{0}' props could not be configured.".FormatInvariant(tpType), e);
throw initException;
}

设置对象的属性

 public static void SetObjectProperties(object obj, NameValueCollection props)
{
// remove the type
props.Remove("type"); foreach (string name in props.Keys)
{
string propertyName = CultureInfo.InvariantCulture.TextInfo.ToUpper(name.Substring(, )) +
name.Substring(); try
{
object value = props[name];
SetPropertyValue(obj, propertyName, value);
}
catch (Exception nfe)
{
throw new SchedulerConfigException(
$"Could not parse property '{name}' into correct data type: {nfe.Message}", nfe);
}
}
}

通过反射设置属性的值

 public static void SetPropertyValue(object target, string propertyName, object value)
{
Type t = target.GetType(); PropertyInfo pi = t.GetProperty(propertyName); if (pi == null || !pi.CanWrite)
{
// try to find from interfaces
foreach (var interfaceType in target.GetType().GetInterfaces())
{
pi = interfaceType.GetProperty(propertyName);
if (pi != null && pi.CanWrite)
{
// found suitable
break;
}
}
} if (pi == null)
{
// not match from anywhere
throw new MemberAccessException($"No writable property '{propertyName}' found");
} MethodInfo mi = pi.GetSetMethod(); if (mi == null)
{
throw new MemberAccessException($"Property '{propertyName}' has no setter");
} if (mi.GetParameters()[].ParameterType == typeof(TimeSpan))
{
// special handling
value = GetTimeSpanValueForProperty(pi, value);
}
else
{
value = ConvertValueIfNecessary(mi.GetParameters()[].ParameterType, value);
} mi.Invoke(target, new[] {value});
}

结果图

2.App.config(只在.NETFramework生效)

<?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>
        static void Main(string[] args)
{
var scheduler =new StdSchedulerFactory().GetScheduler().Result; scheduler.Start(); var metaData=scheduler.GetMetaData().Result; Console.WriteLine(metaData.ThreadPoolSize); Console.Read();
}

结果图

 原理

通过ConfigurationManager.GetSection()来获取App.config里面的值,如果有就转换成NameValueCollection

         public virtual void Initialize()
{
// short-circuit if already initialized
if (cfg != null)
{
return;
}
if (initException != null)
{
throw initException;
} var props = Util.Configuration.GetSection(ConfigurationSectionName);
}
internal static NameValueCollection GetSection(string sectionName)
{
try
{
return (NameValueCollection) ConfigurationManager.GetSection(sectionName);
}
catch (Exception e)
{
log.Warn("could not read configuration using ConfigurationManager.GetSection: " + e.Message);
return null;
}
}

3.quartz.config

quartz.threadPool.ThreadCount=20
            System.Collections.Specialized.NameValueCollection collection = new System.Collections.Specialized.NameValueCollection();

            //collection.Add("quartz.threadPool.ThreadCount","20");          

            //var factory= new StdSchedulerFactory(collection);

            var factory = new StdSchedulerFactory();

            var scheduler= await factory.GetScheduler();

            await scheduler.Start();

            var metData=await scheduler.GetMetaData();

            Console.WriteLine(metData.ThreadPoolSize);

结果图

原理

判断当前程序中是否有quartz.config这个文件

如果有则读取

            if (props == null && File.Exists(propFileName))
{
// file system
try
{
PropertiesParser pp = PropertiesParser.ReadFromFileResource(propFileName);
props = pp.UnderlyingProperties;
Log.Info($"Quartz.NET properties loaded from configuration file '{propFileName}'");
}
catch (Exception ex)
{
Log.ErrorException("Could not load properties for Quartz from file {0}: {1}".FormatInvariant(propFileName, ex.Message), ex);
}
}

#表示注释

!END代表结束,如果没有就读取全部

        public static PropertiesParser ReadFromFileResource(string fileName)
{
return ReadFromStream(File.OpenRead(fileName));
} private static PropertiesParser ReadFromStream(Stream stream)
{
NameValueCollection props = new NameValueCollection();
using (StreamReader sr = new StreamReader(stream))
{
string line;
while ((line = sr.ReadLine()) != null)
{
line = line.TrimStart(); if (line.StartsWith("#"))
{
// comment line
continue;
}
if (line.StartsWith("!END"))
{
// special end condition
break;
}
string[] lineItems = line.Split(new[] { '=' }, );
if (lineItems.Length == )
{
props[lineItems[].Trim()] = lineItems[].Trim();
}
}
}
return new PropertiesParser(props);
}

4.Environment(环境变量)

            Environment.SetEnvironmentVariable("quartz.threadPool.ThreadCount","");

            System.Collections.Specialized.NameValueCollection collection = new System.Collections.Specialized.NameValueCollection();

            //collection.Add("quartz.threadPool.ThreadCount","20");          

            //var factory= new StdSchedulerFactory(collection);

            var factory = new StdSchedulerFactory();

            var scheduler= await factory.GetScheduler();

            await scheduler.Start();

            var metData=await scheduler.GetMetaData();

            Console.WriteLine(metData.ThreadPoolSize);

结果图

 原理

public virtual void Initialize()
{
。。。。。。
Initialize(OverrideWithSysProps(props));
}

获取所有的环境变量,然后赋值给NameValueCollection

        private static NameValueCollection OverrideWithSysProps(NameValueCollection props)
{
NameValueCollection retValue = new NameValueCollection(props);
IDictionary<string, string> vars = QuartzEnvironment.GetEnvironmentVariables(); foreach (string key in vars.Keys)
{
retValue.Set(key, vars[key]);
} return retValue;
}

 quartz.config<app.config<环境变量<NameValueCollection

Quartz.Net系列(十五):Quartz.Net四种修改配置的方式的更多相关文章

  1. mysql四种修改密码的方式

    方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...

  2. java正则表达式四种常用的处理方式是怎么样呢《匹配、分割、代替、获取》

    java 正则表达式高级篇,介绍四种常用的处理方式:匹配.分割.替代.获取,具体内容如下package test; import java.util.regex.Matcher; import jav ...

  3. java内部类及四种内部类的实现方式

     java内部类及四种内部类的实现方式 一.内部类定义:内部类分为: 成员内部类.静态嵌套类.方法内部类.匿名内部类. 二.为何要内部类?a.内部类提供了某种进入外围类的窗户.b.也是最吸引人的原因, ...

  4. Spring中四种实例化bean的方式

    本文主要介绍四种实例化bean的方式(注入方式) 或者叫依赖对象实例化的四种方式.上面的程序,创建bean 对象,用的是什么方法 ,用的是构造函数的方式 (Spring 可以在构造函数私有化的情况下把 ...

  5. 面渣逆袭:Spring三十五问,四万字+五十图详解

    大家好,我是老三啊,面渣逆袭 继续,这节我们来搞定另一个面试必问知识点--Spring. 有人说,"Java程序员都是Spring程序员",老三不太赞成这个观点,但是这也可以看出S ...

  6. 学习ASP.NET Core Razor 编程系列十五——文件上传功能(三)

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  7. 聊聊MySQL的加锁规则《死磕MySQL系列 十五》

    大家好,我是咔咔 不期速成,日拱一卒 本期来聊聊MySQL的加锁规则,知道这些规则后可以判断SQL语句的加锁范围,同时也可以写出更好的SQL语句,防止幻读问题的产生,在能力范围内最大程度的提升MySQ ...

  8. Android之Activity系列总结(三)--Activity的四种启动模式

    一.返回栈简介 任务是指在执行特定作业时与用户交互的一系列 Activity. 这些 Activity 按照各自的打开顺序排列在堆栈(即返回栈,也叫任务栈)中. 首先介绍一下任务栈: (1)程序打开时 ...

  9. 单点登录(十五)-----实战-----cas4.2.x登录mongodb验证方式实现自定义加密

    我们在前一篇文章中实现了cas4.2.x登录使用mongodb验证方式. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程 也学习参考了cas5.0.x版 ...

随机推荐

  1. flex弹性模型

    flex模型是w3c最新提出的一种盒子模型,很好的解决了普通模型的一些弊端. 一.比较两种盒子模型: demo: 给div添加边框,观察他们的区别 <body> <div class ...

  2. JavaScript 定时器 取消定时器

    感谢:链接(视频讲解很清晰) 定时器:作用主要是一定时间间隔后,做出相关的变化,例如图片轮播. 目录 两种定时器的使用 两种定时器区别 取消定时器的方法 两种定时器的使用: 方法一:setTimeou ...

  3. 分析ThreadLocal的弱引用与内存泄漏问题

    目录 一.介绍 二.问题提出 2.1内存原理图 2.2几个问题 三.回答问题 3.1为什么会出现内存泄漏 3.2若Entry使用弱引用 3.3弱引用配合自动回收 四.总结 一.介绍 之前使用Threa ...

  4. CODING DevOps 系列第一课:基于开源工具链打造持续交付平台

    当下软件发展趋势 当今 IT 行业发展中比较流行的几个技术,首先是微服务化,将原有的一个系统拆分成多个,意味着有多个系统需要构建.测试.部署和运维. 第二个是敏捷开发模式,需求粒度更细化,要求一个可独 ...

  5. Rust 数据类型

    Rust中的每个值都具有特定的数据类型. 基础类型: 整数,浮点数,布尔值和字符 i8,i16,i32,i64,i64,i128,isize, u8,u16,u32,u64,u64,u128,usiz ...

  6. SpringBoot——项目启动时读取配置及初始化资源

    介绍   在开发过程中,我们有时候会遇到非接口调用而出发程序执行任务的一些场景,比如我们使用quartz定时框架通过配置文件来启动定时任务时,或者一些初始化资源场景等触发的任务执行场景. 方法一:注解 ...

  7. 关于idea的一些快捷键

    最近在用idea写代码,熟悉一些快捷键的使用能够让写代码的速度提高,以下快捷键是默认idea的快捷键,当然我们可以自己修改的: 自动补全代码快捷键:CTRL+alt+V 自动格式化代码:CTRL+al ...

  8. 且谈 Apache Spark 的 API 三剑客:RDD、DataFrame 和 Dataset

    作者:Jules S. Damji 译者:足下 本文翻译自 A Tale of Three Apache Spark APIs: RDDs, DataFrames, and Datasets ,翻译已 ...

  9. html+css快速入门教程(3)

    练习: 1.画盒子 2.相框 5 基础选择器 5.1 id选择器 ID选择器与类选择器的定义与引用方式类似,只是定义的符号不一样.ID通常表示唯一值,因此,ID选择器在CSS 中通常只出现一次.如果出 ...

  10. mpvue实战-手势滑动导航栏

    写点东西记录一下美好时光,上周学习了一下通过mpuve开发微信小程序,看完文档,就准备撸起袖子加油干的时候,一开始就被支持手势滑动的导航栏给搞懵逼了.求助一波百度和谷歌未果后,只能自己动脑动手!为了给 ...