简体   繁体   English

在收到特定邮件时我们从哪里获得发件人演员?

[英]From where we get sender actor when a particular message is received?

Whenever an actor receives a message in scala, we can access the sender of the actor by using a keyword 'sender' which is an an object of trait AbstractActor. 每当actor在scala中收到消息时,我们都可以使用关键字'sender'来访问actor的发送者,该关键字是trait AbstractActor的对象。

My question how is this 'sender' becoming accessible whenever a message is received.? 我的问题是,每当收到邮件时,这个“发件人”如何变得可访问。

and also, can we override this implementation where along with sender some other data is also accessible such as ipaddress, port from where the data came . 而且,我们是否可以覆盖此实现,其中还有一些其他数据也可以访问,例如ipaddress,数据来自的端口。

As far as i know, there is no way you can get ipaddress and port from where the message has come .. Is there any way by which we can obtain ipaddress of sender and port number from this 'sender' object ? 据我所知,你无法从消息来源的地方获取ipaddress和端口..有没有办法从这个'发送者'对象中获取发件人和端口号的ipaddress?

Thanks for the help. 谢谢您的帮助。

(You didn't really say which actors, so I'm assuming an Akka answer is okay as well) (你真的没说哪个演员,所以我假设Akka的答案也没关系)

The sender method gives you the ActorRef which was implicitly or explicitly picked up at the sending site: if you use ! sender方法为您提供在发送站点隐式或明确拾取的ActorRef :如果您使用! within an actor, its implicit val self: ActorRef is found and used. 在一个actor中,它的implicit val self: ActorRef找到并使用了implicit val self: ActorRef If that sender lives in a different ActorSystem than the recipient, ie it is a remote message send, then the sender ref will contain all information necessary for replying, just look at its path: 如果该发件人与收件人位于不同的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

So, in short, sender.path.address.host (and analog for port) should give you what you need. 因此,简而言之, sender.path.address.host (以及模拟端口)应该sender.path.address.host您的需求。

In AKKA actor system, ! 在AKKA演员系统中,! is overloaded as def !(message : scala.Any)(implicit sender : akka.actor.ActorRef) where sender is the actor that sent the message. 被重载为def!(message:scala.Any)(隐式发送者:akka.actor.ActorRef)其中sender是发送消息的actor。 After that you can call path method on the ActorRef, but I don't think you will be able to get real IP address from there. 之后你可以在ActorRef上调用path方法,但我认为你不能从那里获得真正的IP地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM