Akka modifying/creating configuration file from source code

后端 未结 1 1643
鱼传尺愫
鱼传尺愫 2021-01-15 16:59

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 sta

相关标签:
1条回答
  • 2021-01-15 17:57

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

    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))
    
    0 讨论(0)
提交回复
热议问题