Skip to content

Instantly share code, notes, and snippets.

@DanielaSfregola
Last active May 23, 2018 19:03
Show Gist options
  • Save DanielaSfregola/1cb7cb11a1b4f795853e to your computer and use it in GitHub Desktop.
Save DanielaSfregola/1cb7cb11a1b4f795853e to your computer and use it in GitHub Desktop.
An example on how to use the Dead Letters Channel in Akka. See article http://danielasfregola.com/2015/05/04/akka-dead-letters-channel/
import akka.actor._
class EchoActor extends Actor {
def receive = {
case msg => println(s"${self.path.name} - New msg received: $msg")
}
}
object Main extends App {
implicit val system = ActorSystem("dead-letters-usage-example")
val deadLettersSubscriber = system.actorOf(Props[EchoActor], name = "dead-letters-subscriber")
val echoActor = system.actorOf(Props[EchoActor], name = "generic-echo-actor")
system.eventStream.subscribe(deadLettersSubscriber, classOf[DeadLetter])
echoActor ! "First Message"
// generic-echo-actor - New msg received: First Message
echoActor ! PoisonPill
echoActor ! "Second Message"
// dead-letters-subscriber - New msg received: DeadLetter(Second Message,Actor[akka://dead-letters-usage-example/deadLetters],Actor[akka://dead-letters-usage-example/user/generic-echo-actor#317003256])
// INFO [RepointableActorRef]: Message [java.lang.String] from Actor[akka://dead-letters-usage-example/deadLetters] to Actor[akka://dead-letters-usage-example/user/generic-echo-actor#317003256] 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'.
system.deadLetters ! "Dead Message"
// dead-letters-subscriber - New msg received: DeadLetter(Dead Message,Actor[akka://dead-letters-usage-example/deadLetters],Actor[akka://dead-letters-usage-example/deadLetters])
// INFO [DeadLetterActorRef]: Message [java.lang.String] from Actor[akka://dead-letters-usage-example/deadLetters] to Actor[akka://dead-letters-usage-example/deadLetters] 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'.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment