akkaakka-remote-actor

Akka Remote: get autogenerated port


I have a Java client, which obtains an autogenerated port. After starting the actor system, I want to access the port.

Config clientConfig = ConfigFactory.parseString("akka.remote.netty.tcp.port = 0")
        .withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.hostname = " + serverHostName))
        .withFallback(ConfigFactory.load("common"));

actorSystem = ActorSystem.create("clientActorSystem", clientConfig);

// how to access the generated port here..!?

The port must already be set since the log output after ActorSystem.create(...) is like that:

[INFO] [03/31/2016 14:11:32.042] [main] [akka.remote.Remoting] Starting remoting
[INFO] [03/31/2016 14:11:32.233] [main] [akka.remote.Remoting] Remoting started; listening on addresses :[akka.tcp://actorSystem@localhost:58735]
[INFO] [03/31/2016 14:11:32.234] [main] [akka.remote.Remoting] Remoting now listens on addresses: [akka.tcp://actorSystem@localhost:58735]

If I try to get it via the configuration with actorSystem.settings().config().getValue("akka.remote.netty.tcp.port"), I still get 0 as defined before.

Has anyone an idea how this port (58735 in the example) can be accessed?


Solution

  • Using scala you can get Option of port on which Actor system is currently running:

    val port = system.provider.getDefaultAddress.port
    

    Hope you will be able to get the same code in Java.