7

I have a lot of configuration files that modify how my application behaves. I want to be able to make a change and it gets reflected in the application right away when saving the file. Is there a Java library to help with this?

I could simply keep a list of files with their timestamps and continuously check in a background thread when a timestamp changes. Doesn't seem too difficult, but maybe there's a more efficient way to do this? Custom triggers when certain properties have changed would be nice.

I'm using Spring 3.1, is there a built-in mechanism or solution which works nicely with Spring?

UPDATE: Apparently JDK7 now includes this functionality through its Watch Service API: "Most file system implementations have native support for file change notification. The Watch Service API takes advantage of this support where available. However, when a file system does not support this mechanism, the Watch Service will poll the file system, waiting for events." So this'll be my motivation to migrate to JDK7.

at.
  • 50,922
  • 104
  • 292
  • 461
  • 1
    possible duplicate of [File changed listener in Java](http://stackoverflow.com/questions/494869/file-changed-listener-in-java) – ratchet freak Oct 14 '11 at 22:15
  • 1
    I have no idea about the Spring part, but Apache Commons Configuration wraps `java.util.Properties` and enhances it with among others automatic reloading support by `setReloadingStrategy()`. See also http://commons.apache.org/configuration/userguide-1.2/howto_properties.html It should be useable on Spring apps as well. – BalusC Oct 14 '11 at 22:17

1 Answers1

1

Edited:

http://commons.apache.org/configuration/userguide/howto_filebased.html

lpinto.eu
  • 2,077
  • 4
  • 21
  • 45
  • sometimes the configuration values are needed many times a second... sometimes they're needed once for the lifetime of the application. I just want to reload the configuration files whenever they change. – at. Oct 14 '11 at 22:10
  • I planned on migrating to JDK7 for its native support, but looks like you've supplied exactly what I'm looking for and I'd hope it'll eventually be updated to take advantage of JDK7 as well. – at. Oct 15 '11 at 09:57
  • @LuisP: Sorry, Apache Configuration does not reload the file when it is changed (e.g. it has no such event as "FILE_CHANGED"), but it makes sure that the file is realoaded (if necessary) each time you e.g. access the configuration properties. The solution will work, but only for configuration (text files). What if I want to be notified if image file was changed? – dma_k Oct 15 '11 at 11:45
  • The question isn't about images.. it's about configuration files. I assumed text. – lpinto.eu Oct 15 '11 at 12:16