简体   繁体   English

在 Akka 演员重启时重新发送“init”消息

[英]Resend "init" message on Akka actor restart

I have a child "consumer" actor that connects to some external data stream, parses its messages and forwards them further inside the application.我有一个子“消费者”actor,它连接到一些外部数据流,解析它的消息并在应用程序内部进一步转发它们。 This "producer" system has a pub-sub architecture, but does not restore subscriptions after reconnect.这个“生产者”系统具有发布-订阅架构,但在重新连接后不会恢复订阅。 Currently I store these subscriptions in parent actor and resend them in supervisor, but the problem is that while the child is restarting, they get forwarded to dead letter queue.目前我将这些订阅存储在父角色中并在主管中重新发送它们,但问题是当孩子重新启动时,它们被转发到死信队列。 I could have tried scheduling these to parent after some delay, but this may interfere with subscription order, which is important.我本可以尝试在延迟一段时间后将这些安排给父母,但这可能会干扰订阅顺序,这很重要。

So how do I do deliver these "resubscription" messages to the child while it's restarting?那么如何在重新启动时将这些“重新订阅”消息传递给孩子?

You can use Restart Hooks ( http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Restart_Hooks ): preRestart and postRestart api.您可以使用 Restart Hooks ( http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Restart_Hooks ):preRestart 和 postRestart api。

on preRestart on the child actor, you need to inform the supervisor that child actor will be restarting and supervisor should suspend it's sending of message.在子actor上的preRestart上,您需要通知主管子actor将重新启动,并且主管应该暂停它的消息发送。

on postRestart on the child actor, you need to inform the supervisor that child is available and supervisor should resume sending message.在子actor上的postRestart上,您需要通知主管孩子可用并且主管应该继续发送消息。

You can subscribe to dead letters with您可以订阅死信

context.system.eventStream.subscribe(myListenerActorRef, classOf[DeadLetter])
...
def receive = {
  case DeadLetter(msg, from, to) =>
  //Do my custom stuff here
}

, store and send them again. ,存储并再次发送它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM