Skip to content

Instantly share code, notes, and snippets.

@naiello
Created January 11, 2018 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naiello/f97a77398e5bce8fe9937dad5171a999 to your computer and use it in GitHub Desktop.
Save naiello/f97a77398e5bce8fe9937dad5171a999 to your computer and use it in GitHub Desktop.
Actors do not appear to be unsubscribed from DistributedPubSub topics when they terminate.
package com.sandbox.pubsub
import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, PoisonPill, Props}
import akka.cluster.pubsub.DistributedPubSub
import akka.cluster.pubsub.DistributedPubSubMediator._
import com.typesafe.config.ConfigFactory
object PubsubUnsubscribeOnTerminateDemo {
def main(args: Array[String]): Unit = {
val config = ConfigFactory parseString
"""
| akka {
| actor.provider = "akka.cluster.ClusterActorRefProvider"
| cluster.pub-sub {
| send-to-dead-letters-when-no-subscribers = off
| remove-time-to-live = 100 ms
| }
| }
""".stripMargin
val system = ActorSystem("PubsubTest", config)
val pubsub = DistributedPubSub(system).mediator
implicit val sender: ActorRef = system.deadLetters
val actor1 = system.actorOf(Props(classOf[Subscriber]), "actor1")
Thread sleep 1000
pubsub ! Subscribe("TOPIC", actor1)
pubsub ! Publish("TOPIC", "Ping")
Thread sleep 1000
actor1 ! PoisonPill
Thread sleep 1000
pubsub ! Publish("TOPIC", "Ping")
// after running, the log reads:
// [INFO] [akka.tcp://PubsubTest@xx.xx.xx.xx:xxxx/user/actor1] Ping
// [INFO] [akka://PubsubTest/user/actor1] Message [java.lang.String] without sender to Actor[akka://PubsubTest/user/actor1#-1945572019] 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'.
// the DeadLetter indicates that the actor was not properly unsubscribed from the topic on termination
}
class Subscriber extends Actor with ActorLogging {
override def receive: Receive = {
case "Ping" => log.info("Ping")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment