简体   繁体   中英

How to use typesafe conf in akka project java

I have an application written in Java, using the AKKA framework. I want to run the app in local mode. The configuration file application.conf :

    akka{
 actor {
   provider = "akka.cluster.ClusterActorRefProvider"
 }
 remote {
  transport = "akka.remote.netty.NettyRemoteTransport"

   log-remote-lifecycle-events = off
   netty.tcp {

      hostname = "127.0.0.1"
      port =2552
      maximum-frame-size = 1048576000b
      send-buffer-size = 1048576000b
      receive-buffer-size = 1048576000b

   }
 }

 cluster {
  failure-detector {
      threshold = 12
      acceptable-heartbeat-pause = 240s
      heartbeat-interval = 200s
      heartbeat-request {
        expected-response-after = 100s
      }
    }
    seed-nodes = [
   "akka.tcp://kCoreDescompositionSystem@127.0.0.1:2552",
   "akka.tcp://kCoreDescompositionSystem@127.0.0.1:2553",
   "akka.tcp://kCoreDescompositionSystem@127.0.0.1:2554",
    ]
   auto-down-unreachable-after = 10s
 }
}

I want to configure one Master and 3 workers. So the MasterMain Class which reads the conf file :

public class MasterMain {

  public static void main(String[] args) throws IOException {

        final int nbWorkers = 3 ;
        final int nbPartitions = 3 ;
        final String graphFile = "graph/facebook.txt" ;
        final int algo = 1 ;
        final int method = 1;               
        int port = 2552;   
        final Config configLocal = ConfigFactory.parseString("akka.cluster.roles = [masterRole] ").
        //  withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.port=0" )).
            withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.hostname = \"127.0.0.1\"")).
                //withFallback(ConfigFactory.parseString("akka.cluster.role.workerRole.min-nr-of-members = " +nbWorkers)).
            withFallback(ConfigFactory.parseString("akka.cluster.seed-nodes = [\"akka.tcp://MasterMain@127.0.0.1:2552\"]")).
                withFallback(ConfigFactory.load("kcore"));

        System.out.println(configLocal.toString());

        final ActorSystem system = ActorSystem.create("MasterMain", configLocal);

        system.log().info("System will start when at least"+nbWorkers+" workers node join the cluster.");
        Cluster.get(system).registerOnMemberUp(new Runnable() {
          @Override
          public void run() {
                      system.actorOf(Props.create(Master.class, nbWorkers, nbPartitions,graphFile,algo,method  ),"master");
          }
        });

I'm confused how to properly create a Config configLocal = ConfigFactory .....

When creating the configLocal object, you could use the ConfigFactory.load() to load the configuration from the file.

final Config configLocal = ConfigFactory.parseString("akka.cluster.roles = [masterRole] ").
    //  withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.port=0" )).
        withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.hostname = \"127.0.0.1\"")).
            //withFallback(ConfigFactory.parseString("akka.cluster.role.workerRole.min-nr-of-members = " +nbWorkers)).
        withFallback(ConfigFactory.parseString("akka.cluster.seed-nodes = [\"akka.tcp://MasterMain@127.0.0.1:2552\"]")).
            withFallback(ConfigFactory.load());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM