简体   繁体   English

Akka-受监督演员的重新启动

[英]Akka - restart of supervised actors

I am using Akka (Java) in my project for providing a retry scheme. 我在我的项目中使用Akka(Java)提供重试方案。 So I have a supervisor actor, which on receiving a message delegates it to a supervised actor. 因此,我有一个主管演员,该主管在收到消息后会将其委托给受监督的演员。 I have a one-for-one strategy on the supervisor to restart (unconditionally). 我对主管有一个一对一的策略来重新启动(无条件)。

The supervised actor has a preRestart hook, which sends the message to self. 受监管的actor具有preRestart挂钩,该挂钩将消息发送给self。

@Override
    public void preRestart(Throwable reason, Option<Object> message){
        //send it to the supervisor so that it can be enqueued once again to retry
        if(reason.getCause() instanceof SQLException){
              log.error("Not retrying since there seems to be a problem with the SQL itself!");
              super.preRestart(reason, message);

        }
        else{
            log.warn(""+state+" Trying to redo a processing: "+((RecordData)message.get()).getRecord());

            getSelf().tell(message.get(), getSender());
            super.preRestart(reason, message);
        }
    }

Now I want to preserve the internal state of the failed actor as well! 现在,我也想保留失败演员的内部状态! I understand the state will be preserved only if my strategy is 'resume', but in that case the onRestart hook won't be invoked and the message will be lost. 我知道只有在我的策略为“恢复”时状态才会被保留,但是在那种情况下,onRestart挂钩将不会被调用,并且消息将会丢失。

Question: 题:

1. Is the only way to achieve this, is to set the the state in the message itself and restart? 1.实现此目的的唯一方法是在消息本身中设置状态并重新启动吗?

2. If the state maintains a sequential order, then I need to provide an 'ordered' mailbox implementation (dequeue based). 2.如果状态保持顺序,则需要提供“有序”邮箱实现(基于出队)。 Is that a correct understanding? 这是正确的理解吗?

I doubt if you can send a message to an Actor that is in preRestart phase. 我怀疑您是否可以将消息发送到处于preRestart阶段的Actor。

Your strategy should be that the actor is reset upon crash/restart. 您的策略应该是在崩溃/重启后重置actor。 If you want to keep the internal state, you have to pass it to somewhere else (other actor, database) to ask it again when the new actor is started. 如果要保留内部状态,则必须将其传递到其他地方(其他参与者,数据库),以便在启动新参与者时再次询问它。

Why not indeed implement some more of the logic in the supervisor? 为什么不真正在主管中实施更多逻辑呢?

I would post some example code here, if I knew what version of Akka are you using exactly. 如果我确切知道您使用的是哪个版本的Akka,我将在此处发布一些示例代码。

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

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