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

标签 java akka typesafe-config

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

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/

相关文章:

java - @override 错误显示在 .xml 文件中

java - 需要帮助在 2 个彼此重叠的 Jpanels 上绘画

scala - 流会被终止吗?

Scala Akka TCP 角色

scala - 如何在 akka http 中向 POST 请求添加重试?

scala - 从已部署的Scala应用中的文件夹加载application.conf

java - 二元矩阵查找距离为 k 的所有单元格

java - 通过java服务将一个文档添加到另一个Document

typesafe-config - 来自 envs 的 HOCON 数组替换

scala - 使用 uberjar 部署时覆盖 Typesafe 配置中的多个配置值