通过Akka以编程方式获取临时端口

编程入门 行业动态 更新时间:2024-10-10 17:29:46
本文介绍了通过Akka以编程方式获取临时端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我在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以编程方式获取临时端口

本文发布于:2023-11-25 10:24:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629384.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:端口   方式   Akka

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!