I want to avoid dead letters message to a terminated actor and avoid sending message to that actor
class PingActor extends Actor with ActorLogging {
import PingActor._
var counter = 0
var sendMessages = true
val pongActor = context.actorOf(PongActor.props, "pongActor")
context.watch(pongActor)
def receive = {
case Terminated(pong) =>
sendMessages = false
case Initialize =>
println("In PingActor - starting ping-pong")
pongActor ! PingMessage("ping")
case PongActor.PongMessage(text) =>
println("In PingActor - received message: {}", text)
counter += 1
if (counter == 10 ) context.system.shutdown()
else {
context.actorSelection(pongActor.path) ! PingMessage("ping")
}
}
}
class PongActor extends Actor with ActorLogging {
import PongActor._
var counter = 0
def receive = {
case PingActor.PingMessage(text) =>
println(s"In PongActor - received message: $text counter = $counter \n reply with pong message")
if (counter < 5) {
counter = counter + 1
}
else
{
println("Oh crap , bye bye ")
context.stop(self)
}
sender() ! PongMessage("pong")
}
the actorSelection did not work as I expected , and the last message sent from ping still ends to dead letters :
[INFO] [09/16/2016 00:47:46.237] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/pingActor/pongActor] Message [com.example.PingActor$PingMessage] from Actor[akka://MyActorSystem/user/pingActor#1697177867] to Actor[akka://MyActorSystem/user/pingActor/pongActor#524615423] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.