针对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: 系统设计, 缓存设计, 高性能系统 摘要 ...
随机推荐
- 机器学习Python实现 SVD 分解
这篇文章主要是结合机器学习实战将推荐算法和SVD进行对应的结合 不论什么一个矩阵都能够分解为SVD的形式 事实上SVD意义就是利用特征空间的转换进行数据的映射,后面将专门介绍SVD的基础概念.先给出p ...
- eclipse/STS 切换目录视图
- vue 钩子函数 使用async await
示例: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...
- 基于jquery的锚点滚动插件(百度百科效果) anchorScroll.js
1.插进使用场景 请打开https://baike.baidu.com/item/%E6%97%A5%E6%9C%AC%E5%8A%A8%E7%94%BB#hotspotmining,查看百度百科页面 ...
- 解决Linux SSH登录慢
出现ssh登录慢一般有两个原因:DNS反向解析的问题和ssh的gssapi认证 :ssh的gssapi认证问题 GSSAPI ( Generic Security Services Applicati ...
- ecipse theme
市场里搜“jeeeyul's Eclipse Themes”
- oracle 多字段去重查询
oracle 多字段去重查询 CreationTime--2018年6月29日15点11分 Author:Marydon 1.情景展示 需要对表BASE_MRI_DEVICE的COMPNAME.F ...
- Redis学习(6)-常用命令
List命令 value值为LinkedList类型. 使用环境: 1,做大数据集合的增删. 2,任务队列.用户任务队列 链表查看 lrange key start end:获取链表从start到en ...
- 执行存储过程报错——ora-01031:权限不足
1. 执行DDL报错 在oracle存储过程中,默认是可以直接执行DML和DQL的,但是执行CREATE这种的DDL则需要借助EXECUTE IMMEDIATE ···了,如下备份表语句 --抄表表备 ...
- HighCharts: 设置时间图x轴的宽度
这个x轴宽度的设置整了好久,被老板催的要死 highcharts的api文档很难找,找了半天也没找到,网上资料少,说的试了下,也没有,我用的图里api文档里没有介绍,这个属性不知道的话,根本不好找.为 ...