Example usage for org.apache.commons.jci.listeners FileChangeListener FileChangeListener

List of usage examples for org.apache.commons.jci.listeners FileChangeListener FileChangeListener

Introduction

In this page you can find the example usage for org.apache.commons.jci.listeners FileChangeListener FileChangeListener.

Prototype

FileChangeListener

Source Link

Usage

From source file:org.apache.commons.jci.examples.configuration.ConfigurationReloading.java

private void run(String[] args) {

    final File configFile = new File("some.properties");

    System.out.println("Watching " + configFile.getAbsolutePath());

    final Collection<Configurable> configurables = new ArrayList<Configurable>();

    final FilesystemAlterationListener listener = new FileChangeListener() {
        public void onStop(FilesystemAlterationObserver pObserver) {
            super.onStop(pObserver);

            if (hasChanged()) {
                System.out.println("Configuration change detected " + configFile);

                final Properties props = new Properties();
                InputStream is = null;
                try {
                    is = new FileInputStream(configFile);
                    props.load(is);//ww  w .  j  a  va2s  . c o  m

                    System.out.println("Notifying about configuration change " + configFile);

                    for (Configurable configurable : configurables) {
                        configurable.configure(props);
                    }

                } catch (Exception e) {
                    System.err.println("Failed to load configuration " + configFile);
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                    }
                }

            }
        }
    };

    fam.addListener(configFile, listener);
    fam.start();

    configurables.add(new Something());

    while (true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
}