开发者

Loading Property file only when it is changed

开发者 https://www.devze.com 2023-01-24 12:21 出处:网络
Guysi have created a properties file outside the jboss classpath (I kept outside because i can change during run开发者_StackOverflow社区time and the values will be reflected).

Guys i have created a properties file outside the jboss classpath

(I kept outside because i can change during run开发者_StackOverflow社区time and the values will be reflected).

I am loading this property file (like System.getProperty("jboss.base.dir.home")) everytime to do rmi lookup .I think that everytime loading the same file, eventhough it is not changed is pain.

I want ideas , how to detect the change in properties file and load only when there is a change.i thought of having the timestamp of lastmodified .kindly let me know your suggestions.


You could reuse Apache commons to read the property file : http://commons.apache.org/configuration/userguide/howto_filebased.html#Automatic_Reloading

OR

You could do it programatillcay yourself. This would reimplementing the apache commons code - probably with far less testing and more error prone. If you insist on this approach here is that I think you can do :

  1. Use a wrapper class on a Properties instance.
  2. This wrapper class should (when initialized) load the properties file from the predefined location (probably configurable)
  3. The wrapper, post init, should start a thread that would run forever and do this :
    • record the modified timestamp of the properties file recently read (see methods in File class)
    • sleep for a configurable time period
    • wake up and check the modified timestamp of the properties file
    • if there is a difference in the timestamps - reload the properties file into the Properties instance. Remember this must be done in isolation (i.e change the object when no ones accessing it).

HTH


Perhaps you could do this with a naming convention. For example, if there are no changes the file would be named (whatever you name it now) for example:

foo.properties

But, on the other hand, if you wish to change it, you could name it something like:

foo.properties-changed

You could then programmatically look for the '-changed' file, load it, and then strip off the suffix.


This solution was inspired by javamonkey79's answer and by the way hot deployment is handled in JBoss EAP 6.1.0. Instead of renaming the foo.properties file whenever I change it, I created an additional foo.updated file in the same directory. Whenever I change the settings in the foo.properties file, I rename foo.updated to foo.doUpdate.

In the singleton bean that loads the settings from the foo.properties file on startup, before I try to get a certain value from the bean, I first check if there is a foo.doUpdate file in the configuration directory. If so, I read the settings from the foo.properties file, and then delete foo.doUpdate and create a new file named foo.updated.

0

精彩评论

暂无评论...
验证码 换一张
取 消