RaceBase
RaceBase

Reputation: 18868

Reloading Properties files when changed

I am loading Properties files to One class and then using the class throughout the application to get them.

public class PropertiesUtil extends PropertyPlaceholderConfigurer {

    private static Map<String, String> properties = new HashMap<String, String>();

    @Override
    protected void loadProperties(final Properties props) throws IOException {
        super.loadProperties(props);
        for (final Object key : props.keySet()) {
            properties.put((String) key, props.getProperty((String) key));
        }
    }

    public String getProperty(final String name) {
        return properties.get(name);
    }

}

and in ApplicationContext.xml

    <bean id="propertiesUtil"
        class="com.test.PropertiesUtil">
        <property name="locations">
            <list>
                <value>classpath:test/test.properties</value>
            </list>
        </property>
    </bean>

Now I want to make sure that Properties file is reloaded whenever it's changed.

I have one listener class which initializes along with tomcat server. and I have written below logic for file watcher

TimerTask task = new FileWatcher(new File("c:\\temp-reb\\config\\config.properties")) {
    /*
     * (non-Javadoc)
     * @see com.belgacom.rosy.rebecca.utils.FileWatcher#onChange(java.io.File)
     */
    @Override
    protected void onChange(File file) {
        loadServiceProperties(file);
        loadMetadata();
    }
};

Timer timer = new Timer();
timer.schedule(task, new Date(), Long.valueOf(properties.getProperty("properties.file.timer.schedule"))); // repeat the check every second

problem is

  1. FileWatcher needs path to run, which I don't want to hardcode
  2. How do I tell spring to call properties to reload explicitly!

Upvotes: 3

Views: 10016

Answers (2)

mtk
mtk

Reputation: 13717

  1. FileWatcher needs path to run, which I don't want to hardcode

Just give the relative path like a resource directory in same project folder, which you can get using the getResource() method. You can also use the system property to access like user.dir which is the use working directory.

File f = new File(System.getProperty("user.dir")+ "/test.properties");
System.out.println(f.getAbsolutePath());

2. How do I tell spring to call properties to reload explicitly!

The way you are doing it currently seems ok to me. There might be other ways as well, but I don't see any flaw in the above process.

Upvotes: 3

Stefan
Stefan

Reputation: 1000

This is a winding road leading to really dark places. Spring per definition instantiates and maintains references to singletons. So if a client becomes injected with a dependency, keeps it, and the application context starts delivering new beans, you get in a real ugly state. If you do not use your properties as bean properties you should be OK, but mixing this is not really the way to go.

If anything I would try to limit the number of beans affected by reloading the properties and put them in special scopes. This way everytime your scope changes, you get a new bean with the newest configuration. At least you will have a defined lifecycle of properties and know exactly what you are up against.

Upvotes: 1

Related Questions