1
votes

Is there any way to determine if a message is a tell or an ask?

Use case:

I'm seeing cases where a library is used from outside of an actor and tells are used for the message. Because the library will reply with the result for success/failure, yet me don't care about it in the usage, we're using tell and receiving back a message to a context that isn't an actor. By default the message goes to deadLetters which is fine but it logs the message. You can adjust the logging to shut up the messages but I'm wondering if there is any way to determine if the message is a tell or an ask from inside the library without doing something like having a specific message type for fire and forget vs asks.

1
Can't test atm but, think ActorRef.noSender().equals(sender()) ?tariksbl
You could try looking at the path of the sender. I think the temporary actors created by the ask pattern start with /system and regular actors crated by you will start with /user. I wouldn't really advise using this because I don't think it matters either way but if you really needed it it might work.cmbaxter
Ya I thought about parsing the actor path You're correct that temporary actors (eg on ask) are registered in the temp path. Seems to couple really hard to Akka's specific details though.JasonG

1 Answers

2
votes

I always include a "reply-to" field in my message types. Sometimes I make it an Option[ActorRef] but often I use a repeated parameter (which generalizes the response transmission and makes all cases cleaner–no None / Some(replyTo) / List(replyOne, replyTwo …) etc.) The repeated parameter approach is facilitated by an implicit class that allows fanout by providing a !* method.

So, something like this:

case class Req(i: Int, s: String, replyTo: ActorRef*)
case class Resp(mesg: String)

class MyActor extends Actor {
  ...
  def receive = {
    case Req(i, s, replyTo) => ... replyTo !* Resp("Consider it handled")
    ...
  }
  ...
}