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 加载配置文件的更多相关文章

  1. spring加载配置文件

    spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoa ...

  2. spring-自动加载配置文件\使用属性文件注入

    在上一篇jsf环境搭建的基础上 , 加入spring框架 , 先看下目录结构 src/main/resources 这个source folder 放置web项目所需的主要配置,打包时,会自动打包到W ...

  3. powershell创建并加载配置文件

    $pshome :powershell的主目录 $profile :显示 Windows PowerShell 配置文件的路径 test-path $profile :确定是否已经在系统上创建了 Wi ...

  4. java加载配置文件

    有时候一些变量可能会变,但直接写在程序代码中不好,因为有时候需要改的时候要改动源代码,这时候可以使用配置文件来管理.比如数据库中的端口和密码. 1. 把.properties配置文件放在src目录下. ...

  5. 【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法

    小结: (1)在nutch中,一般通过ToolRunner来运行hadoop job,此方法可以方便的通过ToolRunner.run(Configuration conf,Tool tool,Str ...

  6. Java中加载配置文件的集中方式,以及利用ClassLoader加载文件 .

    我们往常进行文件的加载的时候 用到的都是  FileInputStream进行 文件的加载比如下面一个例子 : InputStream in=FileInputStream("1.prope ...

  7. java动态加载配置文件

    最近项目中需要做定时任务,即定时数据库的备份.定时时间用户可以在界面中配置,要求配置修改好立即生效. 想不到什么好办法.下面是一种实现思路 把用户配置的时间存到properties配置文件中,定时任务 ...

  8. java动态加载配置文件(申明:来源于网络)

    java动态加载配置文件 地址:http://blog.csdn.net/longvs/article/details/9361449

  9. nginx-启动|关闭|重新加载配置文件的命令

    1.1 进入操作目录 D: cd D:\NginxTest\nginx-1.10.2 1.2 启动指令 nginx -c conf\nginx.conf 1.3 关闭指令 nginx -s stop ...

随机推荐

  1. bzoj2660最多的方案

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2660 当然可以看出  选了第 i 个斐波那契数<=>选了第 i - 1 和第 i ...

  2. .net下所有DLL(API)查询,转换C#代码

    地址: http://www.pinvoke.net/default.aspx/coredll.SetDevicePower 实例: SetDevicePower (coredll)   coredl ...

  3. TS/ES/PS

    数字信号实际传送的是数据流,一般数据流包括以下三种: ES流(Elementary Stream): 也叫基本码流,包含视频.音频或数据的连续码流. PES流(Packet Elementary St ...

  4. Makefile编写 二

    变量 1. Makefile中变量和函数的展开(除规则命令行中的变量和函数以外),是在make读取makefile文件时进行的,包括“define”定义的变量. 2. 变量可以用来代表一个文件名列表. ...

  5. 学习blus老师js(1)--基础

    1.网页换肤: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <t ...

  6. 访问https请求出现警告,去掉警告的方法

    在顶部引多一个包即可 from requests.packages.urllib3.exceptions import InsecureRequestWarning,InsecurePlatformW ...

  7. 自动手动随便你 Win7驱动程序安装自己设

    Win7系统是非常智能方便的操作系统,可以自动安装硬件驱动程序,为用户提供了很多方便.但是并不是所有的驱动程序和硬件都能完美兼容,如果不合适就需要卸载了重新安装:还有一些朋友就习惯自己安装驱动,那么, ...

  8. 十一.jQuery源码解析之.pushStack()

    pushStack()顾明思意,就是像桟中添加东西呗,现在看看他是如何添加东西的. 创建一个空的jQuery对象,然后把Dom元素集合放入这个jQuery对象中, 并保留对当前jQuery对象的引用. ...

  9. Day15-Django

    all_entries = Entry.objects.all() #查询所有 Entry.objects.filter(pub_date__year=2006) #查询所有pub_date为2006 ...

  10. Gitlab 社区版安装部署和维护指南

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.这篇文章是在 Gitlab 7.4 的环境下配置的,相关内容可能已经过时. 后续做了一次迁移,将 Gitlab 升级到了 ...