简体   繁体   English

我应该只使用现代的Akka从演员内部发送演员信息吗?

[英]Should I only send actor messages from inside an actor using modern Akka?

On the question about Actor Worst Practices , one of the answers says: 关于Actor Worst Practices的问题,其中一个答案是:

Always send a message from an Actor-subsystem thread. 始终从Actor子系统线程发送消息。 If this means creating a transient Actor via the Actor.actor method then so be it: 如果这意味着通过Actor.actor方法创建一个临时Actor,那么就这样吧:

 case ButtonClicked(src) => Actor.actor { controller ! SaveTrade(trdFld.text) } 

The answer was written in 2009. Is this currently a good practice? 答案写于2009年。这目前是一个好习惯吗? Does it apply to Akka actors? 它适用于Akka演员吗?

I don't think my question is a duplicate of Is it bad practice to send an actor a message from something which is not an actor? 我不认为我的问题是重复的是从一个不是演员的东西发送一个演员的消息是不好的做法? because that is referring to an older implementation of the actor system. 因为那指的是演员系统的较旧实现。 It does explain the rationale behind the "Worst Practices" post. 它确实解释了“最差实践”背后的理由。

No, Akka does not have the issue that the old Scala actors had, where an ActorProxy could potentially be created for each thread, that would then leak memory in its ever-growing mailbox. 不,Akka没有老Scala演员的问题,可能会为每个线程创建ActorProxy ,然后在其不断增长的邮箱中泄漏内存。

Let's look at ! 我们来看看吧! :

def !(message: Any)(implicit sender: ActorRef = Actor.noSender): Unit

If you are sending a message, and you aren't inside another Actor , the the sender defaults to Actor.noSender . 如果您要发送消息,并且您不在另一个Actor ,则发件人默认为Actor.noSender To quote the docs: 引用文档:

Default placeholder (null) used for "!" 默认占位符(null)用于“!” to indicate that there is no sender of the message, that will be translated to the receiving system's deadLetters. 表示没有该消息的发送者,该消息将被转换为接收系统的deadLetters。

If the Actor you are sending to tries to reply, you will get dead letters. 如果您要发送的演员试图回复,您将收到死信。 These can be logged if desired, but they won't result in a memory leak. 如果需要,可以记录这些,但它们不会导致内存泄漏。

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

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