tsc-reload is a tiny library in java that can be used for configuration reloading. In basic usage it wraps TypeSafe config but it is only an optional dependency and can be used with any configuration parsing approach.
When you use plain TypeSafe config, you probably load config on bootstrap phase of your project. When configuration content will change, you need to restart your application. Thanks to tsc-reload, you decide when you want to use current value from configuration.
Sample TypeSafe config usage code:
import com.typesafe.config.*
Config cfg = ConfigFactory.parseFile("config.conf");
int configValue = cfg.getInt("foo.bar");
Then you can pass this value to any place in your application. But this value will change when you change content of config.conf.
When you use tsc-config the same code will look like:
import pl.touk.tscreload.*;
import com.typesafe.config.Config;
import java.time.*;
Reloadable<Config> cfg = TscReloadableConfigFactory.parseFile("config.conf", Duration.ofSeconds(30));
Reloadable<Integer> configValue = cfg.map(c -> c.getInt("foo.bar"));
Then you can also pass value to any place in your application. Value is wrapped in reloadable context. You decide when you want to read current value invoking configValue.currentValue()
. You can add any transformations to Reloadable<T>
using map
method e.g. wrap values with own configuration or use other lib which covert Config
to something else.
You can mix it with other libs like e.g. Ficus. Example code in scala (please make notice that there were used JFunctionConversions available in test sources):
import pl.touk.tscreload._
import java.time._
import net.ceedubs.ficus.Ficus._
import net.ceedubs.ficus.readers.ArbitraryTypeReader._
import JFunctionConversions._
case class Foo(bar: Int)
val reloadable = TscReloadableConfigFactory.parseFile("config.conf", Duration.ofSeconds(30))
val reloadableFoo: Reloadable[Foo] = reloadable.map((cfg: Config) => cfg.as[Foo]("foo"))
val configValue = reloadableFoo.currentValue().bar
With maven:
<dependency>
<groupId>pl.touk</groupId>
<artifactId>tsc-reload</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.3</version>
</dependency>
With sbt:
libraryDependencies += "pl.touk" % "tsc-reload" % "0.6.0"
libraryDependencies += "com.typesafe" % "config" % "1.3.3"
If you want to use it with other config parser than TypeSafe config, just skip the second line.
The tsc-reload is released under version 2.0 of the Apache License.