如果我在Akka中将ActorSystem配置为使用临时tcp端口(更具体地说,我将http端口配置为0),那么一旦ActorSystem启动,是否可以通过编程方式获取此端口?
If I configure an ActorSystem in Akka to use an emphemeral tcp port (more specifically, I configure the http port to 0), is there a way to programmatically obtain this port once the ActorSystem is started?
任何尝试使用actorOf创建一个actor,然后打印出actor路径的操作都会显示一个本地引用的actor。另外,我尝试在事件流上注册RemoteLifeCycleEvent侦听器,但这只能在服务器启动后才能完成,因此会错过RemoteServerStarted事件。
Any attempts to create an actor using actorOf, and then printing out the actor path shows a locally-referenced actor. Additionally, I tried registering a RemoteLifeCycleEvent listener on the event stream, but this can only be done after the server has been started, and hence misses RemoteServerStarted event.
推荐答案在这里:
class MyExtensionImpl(system: ExtendedActorSystem) extends Extension { def address = system.provider match { case rarp: RemoteActorRefProvider => rarp.transport.address case _ => system.provider.rootPath.address } } object MyExtension extends ExtensionKey[MyExtensionImpl] val address = MyExtension(system).address val port = address.port.getOrElse(sys.error("not a remote actor system"))(请注意,此代码适用于Akka2.0.x。在2.1.x中,您可以使用 system.provider避免通过 RemoteActorRefProvider 进行操作。 getDefaultAddress )
(note this code works with Akka 2.0.x. In 2.1.x you can avoid going through RemoteActorRefProvider by using system.provider.getDefaultAddress)
更多推荐
通过Akka以编程方式获取临时端口
发布评论