在我的单元测试中,我正在使用端口0在localhost上启动远程actor系统。我不希望测试尝试绑定到正在使用的端口。相关的Akka配置是:
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "localhost"
port = 0
}
}
如何向Akka或Netty询问哪个端口已绑定,以便我可以正确创建客户端?
答案 0 :(得分:1)
我只是一个阿卡初学者。希望这个例子有所帮助。
import akka.actor._
object HelloRemote extends App {
val system = ActorSystem("HelloRemoteSystem")
val remoteActor = system.actorOf(Props[RemoteActor], name = "RemoteActor")
remoteActor ! "The RemoteActor is alive"
val port = system.asInstanceOf[ExtendedActorSystem].provider.getDefaultAddress.port.get
println(s"RemoteActor port '$port'")
}
class RemoteActor extends Actor {
def receive = {
case msg: String =>
println(s"RemoteActor received message '$msg'")
sender ! "Hello from the RemoteActor"
}
}