scalaakkaakka-actor

How to check if message has sender in Akka Actor


Sometimes messages to actors do not have a sender, for example if they are sent like this:

actorRef.tell(Message(...), Actor.noSender)

One use case for this is to indicate that you are not interested in a response to this Message.

In the receive method of an Actor, how can I check if the sender() is an actor, opposed to noSender?

The best I've come up with is the following test based on the actor path, but I'm not sure that I can rely on this to work in all cases and future changes, Akka cluster etc.

if(sender.path.elements != "deadLetters" :: Nil) ...

If there is no sender, I don't want the response to go to deadLetters, because in my system I am treating undelivered messages as a warning that something is wrong.

Is there a better and more reliable way to check if there is a sender?


Solution

  • I found the solution:

    val hasSender = sender != context.system.deadLetters