[英]From where we get sender actor when a particular message is received?
每当actor在scala中收到消息时,我们都可以使用关键字'sender'来访问actor的发送者,该关键字是trait AbstractActor的对象。
我的问题是,每当收到邮件时,这个“发件人”如何变得可访问。
而且,我们是否可以覆盖此实现,其中还有一些其他数据也可以访问,例如ipaddress,数据来自的端口。
据我所知,你无法从消息来源的地方获取ipaddress和端口..有没有办法从这个'发送者'对象中获取发件人和端口号的ipaddress?
谢谢您的帮助。
(你真的没说哪个演员,所以我假设Akka的答案也没关系)
sender
方法为您提供在发送站点隐式或明确拾取的ActorRef
:如果您使用!
在一个actor中,它的implicit val self: ActorRef
找到并使用了implicit val self: ActorRef
。 如果该发件人与收件人位于不同的ActorSystem
,即它是远程邮件发送,则sender
引用将包含回复所需的所有信息,只需查看其路径:
val s = sender // Actor[akka://<system>@<host>:<port>/user/path/to/actor]
val p = s.path // akka://<system>@<host>:<port>/user/path/to/actor
val a = p.address // akka://<system>@<host>:<port>
val host = a.host // Some(<host>), where <host> is the listen address as configured for the remote system
val port = a.port // Some(<port>), where <port> is the actual listen port of the remote system
因此,简而言之, sender.path.address.host
(以及模拟端口)应该sender.path.address.host
您的需求。
在AKKA演员系统中,! 被重载为def!(message:scala.Any)(隐式发送者:akka.actor.ActorRef)其中sender是发送消息的actor。 之后你可以在ActorRef上调用path方法,但我认为你不能从那里获得真正的IP地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.