是否可以从源代码修改或创建配置文件。我正在创建一些带有远程处理的客户端/服务器架构。我想要实现的是启动客户端应用程序的能力,例如:主机/端口,当还没有配置文件时创建一个满足命令行参数的文件。
akka {
actor {
provider = remote
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1" <--- here
port = 2553 <--- here
}
}
}
配置并不复杂。我想从源代码更改端口(最终是主机,现在它是本地主机用于测试)以使其自动化一点,这样我就可以通过将它们传递给主函数来运行多个客户端。
最佳答案
是的,您可以在代码中修改或创建配置。以下摘自 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))
关于java - Akka 从源代码修改/创建配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44163235/