ejb 3.0 - Load external properties files into EJB 3 app running on WebLogic 11 -
am researching best way load external properties files , ejb 3 app ear file deployed weblogic.
was thinking using init servlet read somewhere slow (e.g. message handler might receive message jms queue before init servlet runs).
suppose have multiple property files or 1 file here:
~/opt/conf/
so far, feel best possible solution using web logic application lifecycle event code read properties files during pre-start:
import weblogic.application.applicationlifecyclelistener; import weblogic.application.applicationlifecycleevent; public class mylistener extends applicationlifecyclelistener { public void prestart(applicationlifecycleevent evt) { // load properties files } }
see: http://download.oracle.com/docs/cd/e13222_01/wls/docs90/programming/lifecycle.html
what happen if server running, post start viable solution?
can think of alternative ways better?
it depends on how want properties reloaded. 1 approach have taken have properties file wrapper (singleton) has configurable parameter defines how files should reloaded. read properties through wrapper , reload properties ever 15 minutes (similar log4j's configureandwatch). way, if wanted to, can change properties without changing state of deployed application.
this allows load properties database, instead of file. way can have level of confidence properties consistent across nodes in cluster , reduces complexity associated managing config file each node.
i prefer on tying lifecycle event. if weren't ever going change them, make them static constants somewhere :)
here example implementation give idea:
import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.util.*; /** * user: jeffrey.a.west * date: jul 1, 2011 * time: 8:43:55 */ public class reloadingproperties { private final string lockobject = "lockme"; private long lastloadtime = 0; private long reloadinterval; private string filepath; private properties properties; private static final map<string, reloadingproperties> instancemap; private static final long default_reload_interval = 1000 * 60 * 5; public static void main(string[] args) { reloadingproperties props = reloadingproperties.getinstance("myproperties.properties"); system.out.println(props.getproperty("example")); try { thread.sleep(6000); } catch (interruptedexception e) { e.printstacktrace(); } system.out.println(props.getproperty("example")); } static { instancemap = new hashmap(31); } public static reloadingproperties getinstance(string filepath) { reloadingproperties instance = instancemap.get(filepath); if (instance == null) { instance = new reloadingproperties(filepath, default_reload_interval); synchronized (instancemap) { instancemap.put(filepath, instance); } } return instance; } private reloadingproperties(string filepath, long reloadinterval) { this.reloadinterval = reloadinterval; this.filepath = filepath; } private void checkrefresh() { long currenttime = system.currenttimemillis(); long sincelastload = currenttime - lastloadtime; if (properties == null || sincelastload > reloadinterval) { system.out.println("reloading!"); lastloadtime = system.currenttimemillis(); properties newproperties = new properties(); fileinputstream filein = null; synchronized (lockobject) { try { filein = new fileinputstream(filepath); newproperties.load(filein); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { if (filein != null) { try { filein.close(); } catch (ioexception e) { e.printstacktrace(); } } } properties = newproperties; } } } public string getproperty(string key, string defaultvalue) { checkrefresh(); return properties.getproperty(key, defaultvalue); } public string getproperty(string key) { checkrefresh(); return properties.getproperty(key); } }
Comments
Post a Comment