You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Component Overview

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="57b71e74-28d6-436b-9a5c-c5c5773a9225"><ac:plain-text-body><![CDATA[

http://jakarta.apache.org/commons/component/images/component-logo-white.png

[http://jakarta.apache.org/commons/component/ Commons-Configuration] Tools to assist in the reading of configuration/preferences files in various formats.[BR] A lot of information is available on the [http://jakarta.apache.org/commons/configuration/ Commons-Configuration website]. If you don't find the information you need you can always contact us using one of the [http://jakarta.apache.org/site/mail2.html#Commons mailing lists].

]]></ac:plain-text-body></ac:structured-macro>

External Resources

Do you have a good example, add it here!

Auto-Configuration-Reloading-Class

AndresValdez:
I have made an Auto-Configuration-Reloading Class.

  1. This class implements the Runnable interface and inherits from PropertiesConfiguration. 2. This class also has a Singleton behavior, so there is only one Configuration class at time, provided every X milliseconds, read from your config.txt file.

Advantages:

1.Only a big one: You can edit your configuration file at any time you want, and your changes will take effect immediately!!!
So your application will change it's behavior at runtime!! (big grin)

You can see the implementation here:

public class Configuration extends PropertiesConfiguration implements Runnable {
//Logger
static Log log = LogFactory.getLog(Configuration.class);

//Cargar el archivo de queries al inicializar la clase
private static String configFile;
private static boolean goOn = true;

static {
configFile = "config.txt";
}

/**

  • Holds singleton instance */
    private static Configuration instance;
    /**
  • prevents instantiation */
    private Configuration() {
    // prevent creation
    }
    private Configuration(String file) throws ConfigurationException {
    super(file);
    }
    /**
  • Returns the "singleton" instance. @return the singleton instance */
    static public Configuration getInstance() {
    if (instance == null) {
    try {
    instance = new Configuration(configFile);
    log.debug("Loaded:" + ((PropertiesConfiguration) instance).toString());
    } catch (ConfigurationException e) {
    log.error("Error reading configuration file: " + configFile);
    }
    }
    return instance;
    }
    public static void main(String[] args) {
    Thread t = new Thread(Configuration.getInstance());
    t.start();
    }
    /* Runner

  • @see java.lang.Runnable#run() */
    public void run() {
    while (goOn) {
    try {
    instance = new Configuration(configFile);
    log.debug("Loaded:" + ((PropertiesConfiguration) instance).toString());
    } catch (ConfigurationException e) {
    log.error("Error reading configuration file: " + configFile);
    } catch (Exception e) {
    log.error("Error reading configuration file: " + configFile);
    }
    try {
    Thread.sleep(instance.getLong("reload.config.after"));
    } catch (InterruptedException e) {
    log.error("Error reading configuration file: " + configFile);
    log.error(": " + e.getLocalizedMessage());
    }
    }
    }
    }



FAQ

Add your questions/answers here.

  • No labels