Dead letters in a message from parent-actor to child-actor when parent stops after sending message

Let's look at the sample code (version 1). Here, the parent actor (ActorA) sends a message to the child actor (ActorB) and then stops itself. Due to the self-stopping of the parent actor under high load, the child actor dies even before the message is received from the mailbox, and therefore the message becomes a "dead letter" (see Sample Result 1 below).

For some reason, I am unable to redesign the application to remove the self-stop of the parent actor.

Version 1

import akka.actor.Actor
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy.Stop
import akka.actor.Props
import akka.actor.ActorSystem

object AkkaTest extends App {

  val system = ActorSystem("AkkaTest")

  for (i <- 1 to 5) {
    system.actorOf(Props[ActorA]) ! i  
  }

}

class ActorA extends Actor {

  def receive = {
    case i: Int => { 
      context.actorOf(Props[ActorB]) ! i
      context.stop(self)
    }
  }

  override def postStop = println("ActorA - stopped")

}

class ActorB extends Actor {

  def receive = {
    case i: Int => {
      println("ActorB - processing msg - " + i)
    }
  }

  override def postStop = println("ActorB - stopped")

}

      

Output example 1

ActorB - processing msg - 2
ActorB - processing msg - 1
ActorB - stopped
ActorB - stopped
ActorB - stopped
ActorB - stopped
ActorB - processing msg - 3
ActorB - stopped
ActorA - stopped
ActorA - stopped
[INFO] [09/09/2014 08:26:56.101] [AkkaTest-akka.actor.default-dispatcher-6] [akka://AkkaTest/user/$e/$a] Message [java.lang.Integer] from Actor[akka://AkkaTest/user/$e#-289783076] to Actor[akka://AkkaTest/user/$e/$a#-86921027] 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'.
[INFO] [09/09/2014 08:26:56.101] [AkkaTest-akka.actor.default-dispatcher-3] [akka://AkkaTest/user/$d/$a] Message [java.lang.Integer] from Actor[akka://AkkaTest/user/$d#-1255514179] to Actor[akka://AkkaTest/user/$d/$a#402128903] was not delivered. [2] 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'.
ActorA - stopped
ActorA - stopped
ActorA - stopped

      

Now consider below code modifications (version 2). By injecting an acknowledgment message from the child actor to the parent actor, and then automatically stopping the parent actor after receiving that acknowledgment, it gets rid of the dead letters (see Example Output 2).

Version 2

import akka.actor.Actor
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy.Stop
import akka.actor.Props
import akka.actor.ActorSystem

object AkkaTest extends App {

  val system = ActorSystem("AkkaTest")

  for (i <- 1 to 5) {
    system.actorOf(Props[ActorA]) ! i  
  }

}

class ActorA extends Actor {

  def receive = {
    case i: Int => context.actorOf(Props[ActorB]) ! i
    case m: MsgAck => context.stop(self) 
  }

  override def postStop = println("ActorA - stopped")

}

class ActorB extends Actor {

  def receive = {
    case i: Int => {
      sender ! MsgAck()
      println("ActorB - processing msg - " + i)
    }
  }

  override def postStop = println("ActorB - stopped")

}

case class MsgAck()

      

Output example 2

ActorB - processing msg - 4
ActorB - processing msg - 2
ActorB - processing msg - 5
ActorB - processing msg - 3
ActorB - processing msg - 1
ActorB - stopped
ActorB - stopped
ActorB - stopped
ActorB - stopped
ActorB - stopped
ActorA - stopped
ActorA - stopped
ActorA - stopped
ActorA - stopped
ActorA - stopped

      

Now my question is, is there any other way to achieve the same? I mean getting rid of dead letters.

+3


source to share


1 answer


When a parent is stopped, all of his children will be stopped. This brings up dead letters. The only way to make sure there are no dead letters is to ensure that the parent is alive until the child receives the message. I can't think of anything other than messages ack

.



+1


source







All Articles