Saturday, 21 May 2016

Owner: Reload property file programmatically

By extending Reloadable interface, we can programmatically reload the configurations.


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.ConfigFactory;

public class PropertyUtil {

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

    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 file to see the changes");
    
    
    Thread.sleep(10000);
    cfg.reload();
    
    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 file to see the changes
test
"http://abc.test.com/getMe"
testHost
testUser




Previous                                                 Next                                                 Home

No comments:

Post a Comment