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. 每日一句英语:怎样回答美国人的How is it going问候语?

    和中国人"吃了吗"是一个性质,本质上仅仅是个话题的起始点,而不是真的想知道你吃了没有. 美国人打招呼有几种方式: 不太熟的人:How are you? 一 般说 pretty go ...

  2. Oracle的REGEXP_SUBSTR函数简单使用方法

    REGEXP_SUBSTR延伸SUBSTR函数的功能.让你搜索一个正則表達式模式字符串. 这也相似于REGEXP_INSTR.而是返回子字符串的位置,它返回的子字符串本身. 语法 Oracle数据库中 ...

  3. 1z0-052 q209_3

    3: Identify two situations in which you can use Data Recovery Advisor for recovery. (Choose two.) —° ...

  4. MVC3循环添加数据

    foreach (var item in orderdetails) { var billdetails = new BillDetail(){BillCode = billoflading.Bill ...

  5. bzoj1296【SCOI2009】粉刷匠

    1296: [SCOI2009]粉刷匠 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 1479  Solved: 837 [id=1296" ...

  6. Foundations of Machine Learning: Boosting

    Foundations of Machine Learning: Boosting Boosting是属于自适应基函数(Adaptive basis-function Model(ABM))中的一种模 ...

  7. 2017年WorkApplication牛客网线上机试题

    WorkApplication是一家日企,主要办公地在东京.新加坡.上海等地. 第一题:n的全排列中有多少个排列逆序数为k 输入两个数字n,k,两个数字的范围都是[1,1000]. 输出:n的全排列中 ...

  8. 使用JavaStcript对数组元素去重的方法

    在做javascript开发的时候,经常会遇到数组元素重复的问题,而javascript Array又没有直接提供方法解决此问题,还需要自己去实现. 方案一: 思路: 1.构建一个新的数组存放结果: ...

  9. 【LeetCode】24. Swap Nodes in Pairs (3 solutions)

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  10. bootstrap table 插件 搜索

    以前用过easyui datagrid 每次搜索的时候调用datagrid构造方法 重新获取数据, 发现bootstrap-table 插件不行,只需要初始化一次, 以后每次搜索时,直接调用refre ...