RocketMQ 加载配置文件
BrokerStartup.java
// 指定配置文件
if (commandLine.hasOption('c')) {
String file = commandLine.getOptionValue('c');
if (file != null) {
configFile = file;
InputStream in = new BufferedInputStream(new FileInputStream(file));
properties = new Properties();
properties.load(in);
MixAll.properties2Object(properties, brokerConfig);
MixAll.properties2Object(properties, nettyServerConfig);
MixAll.properties2Object(properties, nettyClientConfig);
MixAll.properties2Object(properties, messageStoreConfig); BrokerPathConfigHelper.setBrokerConfigPath(file); System.out.println("load config properties file OK, " + file);
}
}
MixAll.java
/**
* 将Properties中的值写入Object
*/
public static void properties2Object(final Properties p, final Object object) {
Method[] methods = object.getClass().getMethods();
for (Method method : methods) {
String mn = method.getName();
if (mn.startsWith("set")) {
try {
String tmp = mn.substring(4);
String first = mn.substring(3, 4); String key = first.toLowerCase() + tmp;
String property = p.getProperty(key);
if (property != null) {
Class<?>[] pt = method.getParameterTypes();
if (pt != null && pt.length > 0) {
String cn = pt[0].getSimpleName();
Object arg = null;
if (cn.equals("int")) {
arg = Integer.parseInt(property);
}
else if (cn.equals("long")) {
arg = Long.parseLong(property);
}
else if (cn.equals("double")) {
arg = Double.parseDouble(property);
}
else if (cn.equals("boolean")) {
arg = Boolean.parseBoolean(property);
}
else if (cn.equals("String")) {
arg = property;
}
else {
continue;
}
method.invoke(object, new Object[] { arg });
}
}
}
catch (Throwable e) {
}
}
}
}
由资源文件生成对象:
首先获取资源文件
获取对象的所有set开头的方法
截取方法,如:setMethod1 则为 method1
通过key获取properties文件value
获取该方法参数类型
调用该方法。
从而完成set 。
RocketMQ 加载配置文件的更多相关文章
- spring加载配置文件
spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoa ...
- spring-自动加载配置文件\使用属性文件注入
在上一篇jsf环境搭建的基础上 , 加入spring框架 , 先看下目录结构 src/main/resources 这个source folder 放置web项目所需的主要配置,打包时,会自动打包到W ...
- powershell创建并加载配置文件
$pshome :powershell的主目录 $profile :显示 Windows PowerShell 配置文件的路径 test-path $profile :确定是否已经在系统上创建了 Wi ...
- java加载配置文件
有时候一些变量可能会变,但直接写在程序代码中不好,因为有时候需要改的时候要改动源代码,这时候可以使用配置文件来管理.比如数据库中的端口和密码. 1. 把.properties配置文件放在src目录下. ...
- 【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法
小结: (1)在nutch中,一般通过ToolRunner来运行hadoop job,此方法可以方便的通过ToolRunner.run(Configuration conf,Tool tool,Str ...
- Java中加载配置文件的集中方式,以及利用ClassLoader加载文件 .
我们往常进行文件的加载的时候 用到的都是 FileInputStream进行 文件的加载比如下面一个例子 : InputStream in=FileInputStream("1.prope ...
- java动态加载配置文件
最近项目中需要做定时任务,即定时数据库的备份.定时时间用户可以在界面中配置,要求配置修改好立即生效. 想不到什么好办法.下面是一种实现思路 把用户配置的时间存到properties配置文件中,定时任务 ...
- java动态加载配置文件(申明:来源于网络)
java动态加载配置文件 地址:http://blog.csdn.net/longvs/article/details/9361449
- nginx-启动|关闭|重新加载配置文件的命令
1.1 进入操作目录 D: cd D:\NginxTest\nginx-1.10.2 1.2 启动指令 nginx -c conf\nginx.conf 1.3 关闭指令 nginx -s stop ...
随机推荐
- Java 的Integer、int与new Integer到底怎么回事?
先做一些总结,询问了些经验比较多的师傅,在这里表示感谢,然后自己总结下,今天的收获分享给大家: 1. int 和Integer在进行比较的时候,Integer会进行拆箱,转为int值与int进行比较. ...
- 2.运行成功的Demo(Python+Appium)
1.打开Appium运行 2.在Pycharm输入代码如下所示: from appium import webdriver desired_caps = {} #初始化 desired_caps['p ...
- emacs之配置php
php-setting.el (require 'php-mode) 以后丰富
- Ninject Lazy Load问题
参考: http://stackoverflow.com/questions/2538132/lazy-loading-with-ninject 方案一: public class Module : ...
- hackerrank Week of Code 31
https://www.hackerrank.com/contests/w31/challenges Beautiful Word 模拟 Accurate Sorting 检查每个数字距离原位是否都不 ...
- 【UVALive】4094 WonderTeam(神结论)
题目 传送门:QWQ 分析 好神的结论啊 看代码吧(Length只有85) 代码 顺手压了压代码 目前代码长度rk1 vjudge #include <iostream> ?:n ...
- js 取元素相对页面的高度和宽度
function pos(elem) { var x = elem.offsetLeft, y = elem.offsetTop; while (elem = elem.offsetPa ...
- SpringBoot入门篇--对于JSON数据的返回以及处理一
在后台的开发过程中不可避免的就是一系列对JSON数据的返回,需要我们进行的就是提供各种各样的数据.一般情况下数据类型最常用的就是JSON以及XML,在这里我们就讲讲在SpringBoot里面我们怎样进 ...
- SQL Server实时同步更新远程数据库遇到的问题
工作中遇到这样的情况,需要在更新表TableA(位于服务器ServerA 172.16.8.100中的库DatabaseA)同时更新TableB(位于服务器ServerB 172.16.8.101中的 ...
- spring data jpa 的各种查询总结
参考哦:https://blog.csdn.net/weixin_36667844/article/details/79945156