5. Reloading configuration changes

Config is loaded once only, when it is constructed. You can use Reloader to load changes on the fly if you want to. Reloader is constructed using a File or other source, and a max age in seconds

  import uk.co.bigbeeconsultants.bconfig.Reloader
  val reloader = Reloader(new File("src/test/resources/sample2.txt"), 10)

Reloader wraps Config loading so that changes to the source can be reloaded dynamically. If the config file is edited whilst the application is running, the new values are loaded in. This usually happens after a short delay; the 10 parameter, above, specifies that changes will be ignored for up to ten seconds, which greatly reduces the contention for the lock needed by the reloader (which is very good for performance). You can use any value from 0 (always reload the file if it has changed) up to max-int (which would never reload so would be amusingly pointless!).

Reloader contains a Config instance, which is reached by Reloader.config. Accessing the data in it is almost the same as described above, except you must use the reloader’s config method. Don’t keep any direct references to the contained Config, else it won’t get updated when the file changes!!!

  val a: String = reloader.config("a")
  val aOption: Option[String] = reloader.config.get("a")

Although their values may change over time, Reloaders are thread-safe.

Reloaders can be daisy-chained with fallbacks, just like Config. When this is done, any of the configs will update if its source is altered. But a fallback reloader only tries its update check when the containing reloader does so. Therefore it is recommended that the max age of each reloader in a chain is the same because it’s easiest to understand what’s going to happen in this simplest case.