In a Scala application using Configes Config, I want to add the ability to reload Config at runtime. The Config instance is immutable. Here is what I still have:
package config trait Settings { private[config] var config: Config = ConfigFactory.empty() def engine: EngineSettings } trait EngineSettings { def weight: Int def offset: Int } class AppSettings { override def engine = new EngineSettings { override def weight = config.getInt("engine.weight") override def offset = config.getInt("engine.offset") } } object Settings { private val namedSettings = new TrieMap[String, AppSettings] def load(configName: String = "local"): Settings = {
Initially, an instance of settings is created using the Settings.load parameter. This instance reference is passed to other classes. Then the second thread can reload the base Config by calling Settings.load again. Here's how you access it:
class Engine(settings: Settings) { def calculate() = { val weight = settings.engine.weight
There are two problems:
- someone can reload the basic configuration while calculate () is in the line: // do some things (consistency)
- I do not like to use var in setting
How can I improve this design :)
java scala concurrentmodification typesafe-config
reikje
source share