Search code examples
javajndi

Loading Property file only when it is changed


Guys i have created a properties file outside the jboss classpath

(I kept outside because i can change during runtime 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.


Solution

  • 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