针对Properties中实时性要求不高的配置参数,用Java缓存起来
Properties常用于项目中参数的配置,当项目中某段程序需要获取动态参数时,就从Properties中读取该参数,使程序是可配置的、灵活的。
有些配置参数要求立即生效,有些则未必:
一、实时性要求非常高。项目中,有些参数要求实时性非常高,即在系统运行中,IT人员修改了该参数值,该新参数值要求立即在程序中生效;
二、实时性要求不高。其实,并不是每个配置参数都要求实时性那么高,有些配置参数基本不会在项目运行当中修改,或即使在运行当中修改,也只要求其在下一次项目启动时生效。
针对第二种情况,鉴于程序读取Properties文件,IO损耗大、效率较低的现状,我们可以在项目启动时,预先将Properties的信息缓存起来,以备程序运行当中方便、快捷地使用。
初步想法:在项目启动加载Listener时,将需要缓存的Properties以键值对形式缓存起来。
kick off:
首先,需要一个类存储Properties,并提供接口实现“缓存Properties”和“读取Properties”的动作
package com.nicchagil.propertiescache; import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties; import org.apache.log4j.Logger; public class PropertiesCacher { private static final Logger logger = Logger.getLogger(PropertiesCacher.class); // Properties Cache
public static Map<String, Properties> pMap = new Hashtable<String, Properties> (); /**
* Set properties to properties cache
* @param pName
* @throws IOException
*/
public static void setProperties(String pName) throws IOException { Properties properties = new Properties();
InputStream is = null; try {
is = PropertiesCacher.class.getResourceAsStream(pName);
properties.load(is); } finally { if (is != null) {
is.close();
} } logger.info("Load to properties cache : " + pName);
pMap.put(pName, properties);
} /**
* Get properties by properties path
* @param pName
* @return
*/
public static Properties getProperties(String pName) {
return pMap.get(pName);
} /**
* Get properties value by properties path & key
* @param pName
* @param key
* @return
*/
public static String getPropertiesValue(String pName, String key) {
if (pMap.get(pName) == null) {
return "";
} return pMap.get(pName).getProperty(key);
} }
PropertiesCacher
然后,我们需要在项目启动时,触发“缓存Properties”这个动作,这里使用Listener
package com.nicchagil.propertiescache; import java.io.IOException; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.apache.log4j.Logger; public class PropertiesListener implements ServletContextListener { private final Logger logger = Logger.getLogger(PropertiesListener.class); @Override
public void contextDestroyed(ServletContextEvent event) { } @Override
public void contextInitialized(ServletContextEvent event) {
try {
// Load config.properties
PropertiesCacher.setProperties("/resource/config.properties"); // Load log4j.properties
PropertiesCacher.setProperties("/log4j.properties"); } catch (IOException e) {
// TODO Auto-generated catch block logger.error(e.getMessage(), e);
e.printStackTrace();
}
} }
PropertiesListener
作为web项目,配置了Listener,当然需要在web.xml注册一下了
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>XlsExporterDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.nicchagil.propertiescache.PropertiesListener</listener-class>
</listener>
<servlet>
<description></description>
<display-name>DebugPropertiesCacheServlet</display-name>
<servlet-name>DebugPropertiesCacheServlet</servlet-name>
<servlet-class>com.nicchagil.propertiescache.DebugPropertiesCacheServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DebugPropertiesCacheServlet</servlet-name>
<url-pattern>/DebugPropertiesCacheServlet</url-pattern>
</servlet-mapping>
</web-app>
web.xml
最后,自己新建俩properties用于测试,一个是log4j.properties,放在编译根路径下;一个是config.properties,放在编译根路径的resource文件夹下。key值和value值自定义。
这两个properties是本人测试用的,当然你也可以用自己滴,但需要相应地修改PropertiesListener的加载动作哦~
dà gōng gào chéng
现在写一个简单滴Servlet来测试一下是否能成功读取,其中这个Servlet在上述滴web.xml一并注册了,可见“DebugPropertiesCacheServlet”
package com.nicchagil.propertiescache; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DebugPropertiesCacheServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public DebugPropertiesCacheServlet() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pfile = request.getParameter("pfile");
String key = request.getParameter("key"); if (pfile == null || key == null) {
System.out.println(PropertiesCacher.pMap);
} if (pfile != null && key == null) {
System.out.println(PropertiesCacher.getProperties(pfile));
} if (pfile != null && key != null) {
System.out.println(PropertiesCacher.getPropertiesValue(pfile, key));
} } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
} }
DebugPropertiesCacheServlet
最后,做个简单的测试,使用浏览器分别访问以下url(其中参数可能需要自行改一下),并查看console是否打印正确滴信息
http://localhost:8080/XlsExporterDemo/DebugPropertiesCacheServlet http://localhost:8080/XlsExporterDemo/DebugPropertiesCacheServlet?pfile=/resource/config.properties http://localhost:8080/XlsExporterDemo/DebugPropertiesCacheServlet?pfile=/resource/config.properties&key=url
URL
Oh year,成功了!!!
针对Properties中实时性要求不高的配置参数,用Java缓存起来的更多相关文章
- 针对系统中磁盘IO负载过高的指导性操作
针对系统中磁盘IO负载过高的指导性操作 主要命令:echo deadline > /sys/block/sda/queue/scheduler 注:以下的内容仅是提供参考,如果磁盘IO确实比较大 ...
- 磁盘IO过高时的处理办法 针对系统中磁盘IO负载过高的指导性操作
磁盘IO过高时的处理办法 针对系统中磁盘IO负载过高的指导性操作 主要命令:echo deadline > /sys/block/sda/queue/scheduler 注:以下的内容仅是提供参 ...
- nginx 高并发配置参数(转载)
声明:原文章来自http://blog.csdn.net/oonets334/article/details/7528558.如需知道更详细内容请访问. 一.一般来说nginx 配置文件中对优化比较有 ...
- nginx 高并发配置参数
一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1. worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu ...
- 在CentOS 6.5 中安装JDK 1.7 + Eclipse并配置opencv的java开发环境(二)
一.安装JDK 1.7 1. 卸载OpenJDK rpm -qa | grep java rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.50.1.11.5. ...
- 【原创】有利于提高xenomai 实时性的一些配置建议
版权声明:本文为本文为博主原创文章,转载请注明出处.如有错误,欢迎指正. @ 目录 一.影响因素 1.硬件 2.BISO(X86平台) 3.软件 4. 缓存使用策略与GPU 二.优化措施 1. BIO ...
- (转)AIX 中 Paging Space 使用率过高的分析与解决
AIX 中 Paging Space 使用率过高的分析与解决 原文:https://www.ibm.com/developerworks/cn/aix/library/au-cn-pagingspac ...
- Linux 2.6 内核实时性分析 (完善中...)
经过一个月的学习,目前对linux 下驱动程序的编写有了入门的认识,现在需要着手实践,编写相关的驱动程序. 因为飞控系统对实时性有一定的要求,所以先打算学习linux 2.6 内核的实时性与任务调 ...
- 物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载)
物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载) 2015年11月14日| by: nbboy| Category: 系统设计, 缓存设计, 高性能系统 摘要 ...
随机推荐
- Performance Testing
To test application performance, add rules using FiddlerScript to the OnBeforeResponse function (exc ...
- 文本框只支持数字、小数点、退格符、负号、Del键
Public Function OnlyNumberAndDot(inKeyAscii As Integer) As Integer '函数说明:文本框只支持数字.小数点.退格符.负号.Del键 '入 ...
- 服务名无效。请键入 NET HELPMSG 2185 以获得更多的帮助。
1:关闭MySQL服务的时候出现“服务名无效.请键入 NET HELPMSG 2185 以获得更多的帮助.”错误. 2:查看服务名称 3:重新关闭服务 4:管理员运行控制台后重新执行 成功停掉了.
- ios的坑 无痕模式
我们的一个小应用,用localStorage做了下缓存,测试上线之后有反馈页面数据拉取不到, 最后定位到是localStorage有问题. 是Private Browsing Mode引起的.然后查看 ...
- Java 提示“找不到或无法加载主类” 解决方法
1.检查环境变量配置,我的安装路径是F:\java\jdk1.8.0_91,因为是6以上版本,故不需配置Classpath 否则 CLASSPATH=.;%JAVA_HOME%\lib\dt.jar; ...
- 解决Eclipse里项目名有红叉,但是底下的每一个文件都没有红叉
有这种错误让人很恼火,因为并不知道是哪个文件引起的整个项目错误.所以我们可以打开下边的错误按钮,看看是什么问题. 1.如果是因为java compiler level does not match t ...
- plsql 常用快捷键(自动替换)
plsql 常用快捷键 CreateTime--2018年4月23日17:33:05 Author:Marydon 说明:这里的快捷键,不同于以往的快捷键,输入指定字符,按快捷键,可以自动替换成你 ...
- Oracle使用——PLSQL的中文乱码显示全是问号
问题 这两天刚将PLSQL与Oracle配置好,但是在PLSQL中插入数据时.出现一个问题,PLSQL中的表里无法显示中文,中文无法保存.无法输出.中文在表中显示问号.如图: 原因 经过一番查证,发现 ...
- PmExceptionController
package main.java.com.zte.controller.system; import java.util.ArrayList; import java.util.List; impo ...
- 解决:Access denied for user ''@'sinochip-79e833' (using password: NO)
uthentication to host '' for user '' using method 'mysql_native_password' failed with message: Acces ...