2017-05-24 8 views
1

ソースコードから設定ファイルを変更または作成することは可能ですか?私は、リモートでいくつかのクライアント/サーバーアーキテクチャを作成しています。私が果たそうとしているのは、たとえば、ホスト/ポート、およびコマンドライン引数をまだ作成していない設定ファイルがない場合など、クライアントアプリケーションを起動する機能です。Akkaソースコードから設定ファイルを変更/作成する

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

構成は実際に複雑ではありません。私はちょうどそれを自動化するために私はちょうど主な機能にそれらを渡すことによって複数のクライアントを実行することができます(ちょうどポート(最終的にホスト、今のところそれはテストのためにとにかくローカルホストです)から変更します。

答えて

3

はい、コード内の設定を変更または作成できます。以下の抜粋は、アッカ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)) 
関連する問題