2017-05-24 5 views
1

Ist es möglich, Konfigurationsdatei aus Quellcode zu ändern oder zu erstellen. Ich erstelle eine Client/Server-Architektur mit Remoting. Was ich erfüllen möchte, ist die Fähigkeit, die Client-App zu starten, zum Beispiel: Host/Port und wenn noch keine Konfigurationsdatei existiert, um eine zu erstellen, die die Befehlszeilenargumente erfüllt.Akka Ändern/Erstellen der Konfigurationsdatei aus dem Quellcode

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

Konfigurationen sind nicht wirklich kompliziert. Ich möchte von Quelle nur Port (schließlich Host, für jetzt ist es localhost sowieso für Tests) für die Automatisierung ein bisschen ändern, so kann ich mehrere Clients nur durch die Weitergabe an die Hauptfunktion ausführen.

Antwort

3

Ja, Sie können die Konfiguration im Code ändern oder erstellen. Die folgenden Auszüge stammen aus dem Akka documentation:

Ein Beispiel für eine Konfiguration programmatisch ändern:

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

Ein Beispiel für eine Konfiguration programmatisch zu schaffen (dies ist in Scala, aber man kann es für Java anpassen):

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)) 
Verwandte Themen