Reputation: 5962
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.
Upvotes: 1
Views: 878
Reputation: 26486
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")
...
}
...
}
Upvotes: 2