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缓存起来的更多相关文章

  1. 针对系统中磁盘IO负载过高的指导性操作

    针对系统中磁盘IO负载过高的指导性操作 主要命令:echo deadline > /sys/block/sda/queue/scheduler 注:以下的内容仅是提供参考,如果磁盘IO确实比较大 ...

  2. 磁盘IO过高时的处理办法 针对系统中磁盘IO负载过高的指导性操作

    磁盘IO过高时的处理办法 针对系统中磁盘IO负载过高的指导性操作 主要命令:echo deadline > /sys/block/sda/queue/scheduler 注:以下的内容仅是提供参 ...

  3. nginx 高并发配置参数(转载)

    声明:原文章来自http://blog.csdn.net/oonets334/article/details/7528558.如需知道更详细内容请访问. 一.一般来说nginx 配置文件中对优化比较有 ...

  4. nginx 高并发配置参数

    一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1.  worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu ...

  5. 在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. ...

  6. 【原创】有利于提高xenomai 实时性的一些配置建议

    版权声明:本文为本文为博主原创文章,转载请注明出处.如有错误,欢迎指正. @ 目录 一.影响因素 1.硬件 2.BISO(X86平台) 3.软件 4. 缓存使用策略与GPU 二.优化措施 1. BIO ...

  7. (转)AIX 中 Paging Space 使用率过高的分析与解决

    AIX 中 Paging Space 使用率过高的分析与解决 原文:https://www.ibm.com/developerworks/cn/aix/library/au-cn-pagingspac ...

  8. Linux 2.6 内核实时性分析 (完善中...)

      经过一个月的学习,目前对linux 下驱动程序的编写有了入门的认识,现在需要着手实践,编写相关的驱动程序. 因为飞控系统对实时性有一定的要求,所以先打算学习linux 2.6 内核的实时性与任务调 ...

  9. 物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载)

    物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载)   2015年11月14日|    by: nbboy|    Category: 系统设计, 缓存设计, 高性能系统 摘要 ...

随机推荐

  1. redis 安装 命令

    安装: http://redis.io/download 在线操作命令:http://try.redis.io/ 命令查询:https://redis.readthedocs.org/en/lates ...

  2. Field.setAccessible()方法

    http://blog.csdn.net/kjfcpua/article/details/8496911 java代码中,常常将一个类的成员变量置为private 在类的外面获取此类的私有成员变量的v ...

  3. UDP和TCP的比較

    当client须要请求数据库server上的某些数据时,它至少须要三个数据报来建立TCP连接.三个数据报礼发送和确认少量数据,三个用来关闭连接. 然而,假设使用UDP的话,只须要发出两个数据报就能达到 ...

  4. VB LISTBOX属性

    additem 添加属性 listcount总记录数 listindex索引值 Private Sub Form_Load()List1.AddItem "广东省广州市"List1 ...

  5. win10开启IE11企业模式

    .右击任务栏开始按钮,选择“运行”,打开运行框(或使用组合键Win+R打开运行) .输入gpedit.msc,进入“本地组策略编辑器”(注:该功能不支持Win8/Win8.1核心版.需要Win8/Wi ...

  6. Openerp 7.0 附件存储位置

    我们知道对OpenERP中的每个内部对象(比如:业务伙伴,采购订单,销售订单,发货单,等等)我们都可以添加任意的附件,如图片,文档,视频等.那么这些附件在OpenERP内部是如何管理的呢? 默认情况下 ...

  7. CUDA 7.0 速查手册

    Create by Jane/Santaizi 03:57:00 3/14/2016 All right reserved. 速查手册基于 CUDA 7.0 toolkit documentation ...

  8. Redis学习(3)-redis启动

    前端启动 tomcat,redis,mysql的端口号: mysql 3306 tomcat 8088 redis 6379 一,启动redis服务: 例如当前位置在redis安装目录下面: 启动re ...

  9. 11、final详解

    1.final修饰成员变量 即该成员被修饰为常量,意味着不可修改. 对于值类型表示值不可变:对于引用类型表示地址不可变 其初始化可以在三个地方 ①:定义时直接赋值 ②:构造函数 ③:代码块{}或者静态 ...

  10. 同域名不同端口应用共享sessionid问题解决办法

    相同域名共享sessionid 如 www.abc.com www.abc.com/test www.abc.com:8080 www.abc.com:8989 这些地址由于是在相同的域名下,所以共享 ...