Reputation: 588
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
}
}
}
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.
Upvotes: 2
Views: 1811
Reputation: 19517
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))
Upvotes: 6