Search code examples
javaakkaakka-remote-actor

How to reference .properties value on application.conf


I have an J2EE app which uses a custom actor system and I need to externalize some custom configurations.

Is there a way to do this? Since the application.conf is always on the classpath is there anyway I can load an external custom.properties file and use it like below

ActorSystem.akka.remote.netty.hostname = "${custom.ip}"
ActorSystem.akka.remote.netty.port = "${custom.port}"

Solution

  • I am not entirely sure what your constraints are, but in principle you have several options:

    1. You can provide hardcoded configuration to your actor system, when you create it, like this:

      Map configMap = new HashMap();
      configMap.put("akka.remote.netty.hostname", custom.ip);
      configMap.put("akka.remote.netty.port", custom.port);
      
      Config config = ConfigFactory.parseMap(configMap).withFallback(ConfigFactory.load());
      ActorSystem system = ActorSystem.create("ActorSystem", config);
      
    2. you can load you custom config file instead of application.conf either through code: ConfigFactory.load("custom.conf") or by setting a system property -Dconfig.resource=custom.conf and include application.conf in your custom.conf, like this:

      include "application"
      akka.remote.netty.hostname = "custom-ip"
      akka.remote.netty.port = "custom-port"
      
    3. You could also provide the custom-port and ip through system properties and use defaults, if they are not defined. In that case the application.conf would look like this:

      akka.remote.netty.hostname = "default-ip"
      akka.remote.netty.port = "default-port"
      akka.remote.netty.hostname = "${?custom.ip}"
      akka.remote.netty.port = "${?custom.port}"
      
    4. Or you can include custom.properties in your application.conf file. If the custom.properties does not exist if will silently be ignored. application.conf:

      akka.remote.netty.hostname = "default-ip"
      akka.remote.netty.port = "default-port"
      include "custom"
      

      custom.properties:

      akka.remote.netty.hostname = "custom-ip"
      akka.remote.netty.port = "custom-port"