首页 > 代码库 > 针对Properties中实时性要求不高的配置参数,用Java缓存起来
针对Properties中实时性要求不高的配置参数,用Java缓存起来
Properties常用于项目中参数的配置,当项目中某段程序需要获取动态参数时,就从Properties中读取该参数,使程序是可配置的、灵活的。
有些配置参数要求立即生效,有些则未必:
一、实时性要求非常高。项目中,有些参数要求实时性非常高,即在系统运行中,IT人员修改了该参数值,该新参数值要求立即在程序中生效;
二、实时性要求不高。其实,并不是每个配置参数都要求实时性那么高,有些配置参数基本不会在项目运行当中修改,或即使在运行当中修改,也只要求其在下一次项目启动时生效。
针对第二种情况,鉴于程序读取Properties文件,IO损耗大、效率较低的现状,我们可以在项目启动时,预先将Properties的信息缓存起来,以备程序运行当中方便、快捷地使用。
初步想法:在项目启动加载Listener时,将需要缓存的Properties以键值对形式缓存起来。
kick off:
首先,需要一个类存储Properties,并提供接口实现“缓存Properties”和“读取Properties”的动作
1 package com.nicchagil.propertiescache; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Hashtable; 6 import java.util.Map; 7 import java.util.Properties; 8 9 import org.apache.log4j.Logger;10 11 public class PropertiesCacher {12 13 private static final Logger logger = Logger.getLogger(PropertiesCacher.class);14 15 // Properties Cache16 public static Map<String, Properties> pMap = new Hashtable<String, Properties> ();17 18 /**19 * Set properties to properties cache20 * @param pName21 * @throws IOException22 */23 public static void setProperties(String pName) throws IOException {24 25 Properties properties = new Properties();26 InputStream is = null;27 28 try {29 is = PropertiesCacher.class.getResourceAsStream(pName);30 properties.load(is);31 32 } finally {33 34 if (is != null) {35 is.close();36 }37 38 }39 40 logger.info("Load to properties cache : " + pName);41 pMap.put(pName, properties);42 }43 44 /**45 * Get properties by properties path46 * @param pName47 * @return48 */49 public static Properties getProperties(String pName) {50 return pMap.get(pName);51 }52 53 /**54 * Get properties value by properties path & key55 * @param pName56 * @param key57 * @return58 */59 public static String getPropertiesValue(String pName, String key) {60 if (pMap.get(pName) == null) {61 return "";62 }63 64 return pMap.get(pName).getProperty(key);65 }66 67 }
然后,我们需要在项目启动时,触发“缓存Properties”这个动作,这里使用Listener
1 package com.nicchagil.propertiescache; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletContextEvent; 6 import javax.servlet.ServletContextListener; 7 8 import org.apache.log4j.Logger; 9 10 public class PropertiesListener implements ServletContextListener {11 12 private final Logger logger = Logger.getLogger(PropertiesListener.class);13 14 @Override15 public void contextDestroyed(ServletContextEvent event) {16 17 }18 19 @Override20 public void contextInitialized(ServletContextEvent event) {21 try {22 // Load config.properties23 PropertiesCacher.setProperties("/resource/config.properties");24 25 // Load log4j.properties26 PropertiesCacher.setProperties("/log4j.properties");27 28 } catch (IOException e) {29 // TODO Auto-generated catch block30 31 logger.error(e.getMessage(), e);32 e.printStackTrace();33 }34 }35 36 }
作为web项目,配置了Listener,当然需要在web.xml注册一下了
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>XlsExporterDemo</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file>10 <welcome-file>default.jsp</welcome-file>11 </welcome-file-list>12 <listener>13 <listener-class>com.nicchagil.propertiescache.PropertiesListener</listener-class>14 </listener>15 <servlet>16 <description></description>17 <display-name>DebugPropertiesCacheServlet</display-name>18 <servlet-name>DebugPropertiesCacheServlet</servlet-name>19 <servlet-class>com.nicchagil.propertiescache.DebugPropertiesCacheServlet</servlet-class>20 </servlet>21 <servlet-mapping>22 <servlet-name>DebugPropertiesCacheServlet</servlet-name>23 <url-pattern>/DebugPropertiesCacheServlet</url-pattern>24 </servlet-mapping>25 </web-app>
最后,自己新建俩properties用于测试,一个是log4j.properties,放在编译根路径下;一个是config.properties,放在编译根路径的resource文件夹下。key值和value值自定义。
这两个properties是本人测试用的,当然你也可以用自己滴,但需要相应地修改PropertiesListener的加载动作哦~
dà gōng gào chéng
现在写一个简单滴Servlet来测试一下是否能成功读取,其中这个Servlet在上述滴web.xml一并注册了,可见“DebugPropertiesCacheServlet”
1 package com.nicchagil.propertiescache; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class DebugPropertiesCacheServlet extends HttpServlet {10 private static final long serialVersionUID = 1L;11 12 public DebugPropertiesCacheServlet() {13 super();14 }15 16 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {17 String pfile = request.getParameter("pfile");18 String key = request.getParameter("key");19 20 if (pfile == null || key == null) {21 System.out.println(PropertiesCacher.pMap);22 } 23 24 if (pfile != null && key == null) {25 System.out.println(PropertiesCacher.getProperties(pfile));26 }27 28 if (pfile != null && key != null) {29 System.out.println(PropertiesCacher.getPropertiesValue(pfile, key));30 }31 32 }33 34 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {35 this.doPost(request, response);36 }37 38 }
最后,做个简单的测试,使用浏览器分别访问以下url(其中参数可能需要自行改一下),并查看console是否打印正确滴信息
1 http://localhost:8080/XlsExporterDemo/DebugPropertiesCacheServlet2 3 http://localhost:8080/XlsExporterDemo/DebugPropertiesCacheServlet?pfile=/resource/config.properties4 5 http://localhost:8080/XlsExporterDemo/DebugPropertiesCacheServlet?pfile=/resource/config.properties&key=url
Oh year,成功了!!!