I'm using Plays play.api.mvc.WebSocket
s in combination with akka.contrib.pattern.DistributedPubSub
events and this works fine already.
class SomeSocketActor(out: ActorRef) extends Actor {
val mediator = DistributedPubSubExtension(context.system).mediator
mediator ! Subscribe("some_group", self)
def receive: Actor.Receive = {
case SubscribeAck(Subscribe("some_group", None, `self`)) =>
context become ready
}
def ready: Actor.Receive = {
// ...
}
override def postStop(): Unit = {
mediator ! Unsubscribe("some_group", self)
}
}
Once a socket gets close it sends Unsubscribe
. Once the Unsubscribe
is received by DistributedPubSubMediator
it answers with UnsubscribeAck
. However since at that moment (which is after postStop
) the actor is stopped already and the UnsubscribeAck
is moved Akkas dead letters mailbox and my log is spammed with something like:
Message [akka.contrib.pattern.DistributedPubSubMediator$UnsubscribeAck] from Actor[akka://application/user/distributedPubSubMediator#261175455] to Actor[akka://application/system/websockets/24/handler#325798268] 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'.
I know I could just follow the advice in the log message, but that doesn't seem like good practice. Is there any way to tell the actor inside the postStop
method to wait for the UnsubscribeAck
before stop?