Reputation: 4231
I am trying to send a message to an actor using the actor selection . but it keeps getting to dead letters . This how the actor is created (using the activator template)
class PongActor extends Actor with ActorLogging {
import PongActor._
def receive = {
case PingActor.PingMessage(text) =>
log.info("In PongActor - received message: {}", text)
sender() ! PongMessage("pong")
}
}
object PongActor {
val props = Props[PongActor]
case class PongMessage(text: String)
}
This is the sending actor :
class PingActor extends Actor with ActorLogging {
import PingActor._
var counter = 0
val pongActor = context.actorOf(PongActor.props, "pongActor")
val tester = context.actorSelection("../pongActor") //getting the actor by name using the actorSelection
def receive = {
case Initialize =>
log.info("In PingActor - starting ping-pong")
tester.tell(PingMessage("ping"),self)//going to dead letters
tester ! PingMessage("ping")//also going to dead letters
case PongActor.PongMessage(text) =>
log.info("In PingActor - received message: {}", text)
counter += 1
if (counter == 3) context.system.shutdown()
else sender() ! PingMessage("ping")
}
}
object PingActor {
val props = Props[PingActor]
case object Initialize
case class PingMessage(text: String)
}
why the messages are getting to the dead letters
how can I verify that the actor really exists ?
Upvotes: 1
Views: 1018
Reputation: 3887
From akka- docs
actorSelection
only ever looks up existing actors when messages are delivered, i.e. does not create actors, or verify existence of actors when the selection is created.
A better option is to use resolveOne
val timeout = 1.seconds
val tester1 = context.actorSelection("pongActor").resolveOne(timeout)
Upvotes: 3
Reputation: 15074
Your line val tester = context.actorSelection("../pongActor")
is unnecessary, as the previous line sets pongActor
to the actorRef
you need. Just use pongActor.tell
and pongActor !
instead of tester.tell
and tester !
.
As for why the actorSelection
lookup is failing: when you create a child actor inside a parent actor (here Ping
is the parent, Pong
is the child), the path to the child is the same as the path to the parent, with the child's name appended. So, relative to Ping
, the path to Pong
is just "pongActor", not "../pongActor".
Upvotes: 2