dsx-tech / rhea

Reactive configuration framework for Kotlin and Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rhea

Rhea is a type-safe dynamic configuration library for JVM applications. This library uses asynchronous data flows that gives a natural auto-reloading feature in runtime, so it allows to change configuration properties and get the freshest values in your application without the need to restart.

Features

  • Open source project under the MIT License
  • Extendable with user-defined property types
  • Extendable with user-defined configuration sources
  • Supports reading configuration from files (.yaml, .properties, .json), Vault, JDBC, MongoDB

Getting Started

Setting up dependency

To get started, add uk.dsxt:rhea-core:<version> as a dependency:

Gradle

dependencies {
  compile group: "uk.dsxt", name:"rhea-core", version: "0.0.1-SNAPSHOT"
}

Maven

<dependencies>
  <dependency>
    <groupId>uk.dsxt</groupId>
    <artifactId>rhea-core</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>
</dependencies>

Configuration Sources

To read configuration from source other than .properties, add its rhea module as a dependency. For example, to add .json as configuration source:

Gradle

dependencies {
  ...
  compile group: "uk.dsxt", name:"rhea-json", version: "0.0.1-SNAPSHOT"
}

Maven

<dependencies>
    ...
  <dependency>
    <groupId>uk.dsxt</groupId>
    <artifactId>rhea-json</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>
</dependencies>

Note: If configuration source is not supported, you can add it by implementing ConfigSource interface.

Usage

Java

  1. Build a configuration object that holds properties, add configuration sources

    ConfigSource jsonSource =
        new JsonConfigSource(Paths.get("src" + File.separator + "test" + File.separator + "resources"), "jsonSource.json");
        
    ReactiveConfig config = new ReactiveConfig.Builder()
        .addSource("jsonConfig", jsonSource)
        .build();
  2. Create reloadable properties

    Reloadable<Boolean> isSomethingOn = config.get("flag", PropertyTypesKt.booleanType);
    Reloadable<Integer> port = config.get("port", PropertyTypesKt.intType);
    
    // access the freshest typed values with get()
    Server server = new Server(port.get(), "host");
    if (isSomethingOn.get()) {
        server.start();
    }
  3. Also, you can add some complex logic that will execute every time the property is changed

    port.onChange((Integer newValue) -> {
        // for example, restart server
    });

Kotlin

  1. Build a configuration object that holds properties, add configuration sources

    val jsonSource: ConfigSource =
        JsonConfigSource(Paths.get("src" + File.separator + "test" + File.separator + "resources"), "jsonSource.json")
        
    val config: ReactiveConfig = ReactiveConfig.Builder()
        .addSource("jsonConfig", jsonSource)
        .build()
  2. Create reloadable properties or declare objects that extend PropertyGroup to define hierarchies of properties

    val isSomethingOn: Realoadable<Boolean> = config["flag", booleanType]
    object server : PropertyGroup() {
        val name by stringType
        val port by intType
    }
    
    // access the freshest typed values with get()
    val server = Server(config[server.port].get(), config[server.name].get())
    if (isSomethingOn.get()) {
        server.start()
    }
  3. Also, you can add some complex logic that will execute every time the property is changed

    val port: Reloadable<Int> = config[server.port]
    
    port.onChange() {
        // for example, restart server
    }

Extending

Property Types

To add a custom property type, you should create an instance of PropertyType class and provide it with default property value and parse function.

Configuration Sources

To support a new configuration source, you should implement ConfigSource interface.

Contributors

License

This project is licensed under the MIT License. The full text could be found in LICENSE.md.

Acknowledgments

  • Inspired by Tinkoff ReactiveConfig and Konfig

Notes

  • Rhea is one of the Titans in Greek mythology, the mother of the first generation of the Olympian gods
  • Rhea's name is believed to be derived from the word ῥέω (rheo), meaning “flow” or “ease”

About

Reactive configuration framework for Kotlin and Java

License:MIT License


Languages

Language:Kotlin 87.1%Language:Java 12.9%