Akka从源代码修改/创建配置文件

时间:2017-05-24 15:54:02

标签: java akka typesafe-config

是否可以从源代码修改或创建配置文件。我正在创建一些带有远程处理的客户端/服务器架构。我想要实现的是能够启动客户端应用程序,例如:主机/端口,当没有配置文件创建一个满足命令行args。

akka {
  actor {
    provider = remote
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1" <--- here 
      port = 2553 <--- here
    }
  }
} 

配置并不复杂。我想从源端口改变端口(最终主机,现在它无论如何都是localhost用于测试),使其自动化一点,这样我就可以通过将它们传递给main函数来运行多个客户端。

1 个答案:

答案 0 :(得分:5)

是的,您可以在代码中修改或创建配置。以下摘录来自Akka documentation

以编程方式修改配置的示例:

// make a Config with just your special setting
Config myConfig = ConfigFactory.parseString("something=somethingElse");

// load the normal config stack (system props, then application.conf, then reference.conf)
Config regularConfig = ConfigFactory.load();

// override regular stack with myConfig
Config combined = myConfig.withFallback(regularConfig);

// put the result in between the overrides (system props) and defaults again
Config complete = ConfigFactory.load(combined);

// create ActorSystem
ActorSystem system = ActorSystem.create("myname", complete);

以编程方式创建配置的示例(这是在Scala中,但您可以将其调整为Java):

import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory

val customConf = ConfigFactory.parseString("""
  akka.actor.deployment {
    /my-service {
      router = round-robin-pool
      nr-of-instances = 3
    }
  }
""")

// ConfigFactory.load sandwiches customConfig between default reference
// config and default overrides, and then resolves it.
val system = ActorSystem("MySystem", ConfigFactory.load(customConf))