Saturday 21 May 2016

Owner: Reload property file automatically


By using the annotation @HotReload, you can instruct Owner API, to reload the properties from file system, when it sees there is change in property file. Hot reloading works only if the property file is in same system, where your application deployed. If the property file is in some other system it don't work.

Following is the definition of HotReload interface.
@interface HotReload {
    long value() default 5;

    TimeUnit unit() default SECONDS;

    HotReloadType type() default SYNC;
}

enum HotReloadType {
    SYNC,
    ASYNC
}

As you observe the definition, it checks property file for changes in the interval of 5 seconds. If Owner API sees any changes in the file, then it reloads the file, else nothing.

Of course you can change these default values like below.
@HotReload(value=500, unit = TimeUnit.MILLISECONDS)

What about HotReloadType?
HotReloadType specifies, how Owner API should apply reload. You can use any one SYNC, ASYNC.
HotReloadType
Description
SYNC
The hot reload will happen when one of the methods is invoked on the Config interface.
ASYNC
The hot reload will happen in background at the specified interval.
  

ProjectConfig.properties
#production.properties
server.production.endPoint = "http://abc.prod.com/getMe"
server.production.host = devHost
server.production.user = devUser

#beta.properties
server.beta.endPoint = "http://abc.beta.com/getMe"
server.beta.host = betaHost
server.beta.user = betaUser

#test.properties
server.test.endPoint = "http://abc.test.com/getMe"
server.test.host = testHost
server.test.user = testUser

#Setting up Environment
environment = production

import org.aeonbits.owner.Config;
import org.aeonbits.owner.Config.HotReload;
import org.aeonbits.owner.Config.Sources;
import org.aeonbits.owner.Config.HotReloadType;

@Sources({ "classpath:ProjectConfig.properties" })
@HotReload(type = HotReloadType.ASYNC)
public interface ProjectConfig extends Config {

    @DefaultValue("test")
    String environment();

    @Key("server.${environment}.endPoint")
    String endPoint();

    @Key("server.${environment}.host")
    String host();

    @Key("server.${environment}.user")
    String user();
}

import org.aeonbits.owner.ConfigFactory;

public class PropertyUtil {

    public static void main(String args[]) throws Exception {
        ProjectConfig cfg = ConfigFactory.create(ProjectConfig.class);

        System.out.println(cfg.environment());
        System.out.println(cfg.endPoint());
        System.out.println(cfg.host());
        System.out.println(cfg.user());

        System.out
                .println("I am going to sleep for 10 seconds, mean time update ProjectConfig.properties environment variable to test, to see the changes");

        Thread.sleep(10000);

        System.out.println(cfg.environment());
        System.out.println(cfg.endPoint());
        System.out.println(cfg.host());
        System.out.println(cfg.user());

    }
}


Output
production
"http://abc.prod.com/getMe"
devHost
devUser
I am going to sleep for 10 seconds, mean time update ProjectConfig.properties environment variable to test, to see the changes 
test
"http://abc.test.com/getMe"
testHost
testUser



Previous                                                 Next                                                 Home

No comments:

Post a Comment