简体   繁体   中英

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:

Always send a message from an Actor-subsystem thread. If this means creating a transient Actor via the Actor.actor method then so be it:

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

The answer was written in 2009. Is this currently a good practice? Does it apply to Akka actors?

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.

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 . To quote the docs:

Default placeholder (null) used for "!" to indicate that there is no sender of the message, that will be translated to the receiving system's 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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