2
votes

I am trying to create a simple application that has two actors:

  • Master actor that handles some App actions
  • DeadLettersListener that is supposed to handle all dead or unhandled messages

Here is the code that works perfectly:

object Hw extends App {
  // creating Master actor
  val masterActorSystem = ActorSystem("Master")
  val master = masterActorSystem.actorOf(Props[Master], "Master")

  // creating Dead Letters listener actor
  val deadLettersActorSystem = ActorSystem.create("DeadLettersListener")
  val listener = deadLettersActorSystem.actorOf(Props[DeadLettersListener])

  // subscribe listener to Master's DeadLetters
  masterActorSystem.eventStream.subscribe(listener, classOf[DeadLetter])
  masterActorSystem.eventStream.subscribe(listener, classOf[UnhandledMessage])
}

According to the akka manual though, ActorSystem is a heavy object and we should create only one per application. But when I replace these lines:

val deadLettersActorSystem = ActorSystem.create("DeadLettersListener")
val listener = deadLettersActorSystem.actorOf(Props[DeadLettersListener])

with this code:

val listener = masterActorSystem.actorOf(Props[DeadLettersListener], "DeadLettersListener")

The subscription does not work any more and DeadLettersListener is not getting any Dead or Unhandled messages.

Can you please explain what am I doing wrong and give an advice how to subscribe to Dead Letters in this case?

1

1 Answers

2
votes

I can't really imagine what are you doing wrong, I created a small example, and it seems to work:

object Hw extends App {

  class Master extends Actor {
    override def receive: Receive = {
      case a => println(s"$a received in $self")
    }
  }

  class DeadLettersListener extends Actor {
    override def receive: Actor.Receive = {
      case a => println(s"$a received in $self")
    }
  }

  // creating Master actor
  val masterActorSystem = ActorSystem("Master")
  val master = masterActorSystem.actorOf(Props[Master], "Master")
  val listener = masterActorSystem.actorOf(Props[DeadLettersListener])

  // subscribe listener to Master's DeadLetters
  masterActorSystem.eventStream.subscribe(listener, classOf[DeadLetter])
  masterActorSystem.eventStream.subscribe(listener, classOf[UnhandledMessage])

  masterActorSystem.actorSelection("/unexistingActor") ! "yo"
}

Could you try it?