Akka从源代码修改/创建配置文件 [英] Akka modifying/creating configuration file from source code

查看:607
本文介绍了Akka从源代码修改/创建配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to modify or create configuration file from source code. I am creating some client/server architecture with remoting. What I want to fulfill is ability to start client app with for example: host/port and when there is no configuration file yet to create one fulfilling the command line 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函数来运行多个客户端。

Configs aren't really complicated. I want to change from source just port (eventually host, for now it is localhost anyway for tests) for automating it a bit so I can run multiple clients just by passing them to main function.

推荐答案

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

Yes, you can modify or create the configuration in the code. The following excerpts are from the Akka documentation:

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

An example of modifying a configuration programmatically:

// 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) :

An example of creating a configuration programmatically (this is in Scala, but you can adapt it for 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))

这篇关于Akka从源代码修改/创建配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆