akka java programmatic override configuration

2019-05-24 23:20发布

问题:

The few topics I can find about this are for Scala, rather than Java, and none address remote actors.

I have a base configuration file (SERVER_CONFIG_FILE):

Include "akka-common"

TheSystem {
  akka {
    actor {
      provider = "akka.remote.RemoteActorRefProvider"
        deployment { 
          /OtherSupervisor {
            remote = "akka://OtherSystem@127.0.0.1:8553"
          }
        }
      }
    remote {
      transport = "akka.remote.netty.NettyRemoteTransport"
      netty {
        hostname = "127.0.0.1"
        port = 8552
      }
    }
  }
}

I want to load it in my program, and then override several settings, but I can't figure out the code. Something like:

private final Config serverConfig = ConfigFactory.load(SERVER_CONFIG_FILE).withValue...?

I need to override the value of "akka://DroneSystem@127.0.0.1:8553", and also the hostname. I think the hostname can be addressed by "ComparisonSystem.akka.remote.netty.hostname", but confirmation would help.

I really do not know how to address the first value, nor what java calls to use to make it come together. I can learn from an example I can see, but not from the Scala I find, and nothing addresses having that Actor Name in the path.

Thanks in advance.

回答1:

You can do that like this, note that you use the Config instance with overrides and let it fallback to your default configuration rather than the other way around:

Config overrides = ConfigFactory.parseString("some.setting=a-value");
Config actualConfig = overrides.withFallback(ConfigFactory.load());

Or if you do not like building strings you could use properties to specify your overrides:

Properties properties = new Properties();
properties.setProperty("some.setting", "a-value");
Config overrides = ConfigFactory.parseProperties(properties);
Config actualConfig = overrides.withFallback(ConfigFactory.load());