通过主管策略重新启动actor后,如何重新发送消息 [英] Howto resend message after actor is restarted by supervisor strategy

查看:57
本文介绍了通过主管策略重新启动actor后,如何重新发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父演员(A)和两个子演员(B)。
我在A中制定了主管策略,因此,如果B中发生特定异常,B将重新启动。

I have parent actor (A) with two child actors (B). I made a supervisor strategy in A, so in case a specific exception happens in B, B will be restarted.

我如何重新发送导致

推荐答案

我在B中所做的就是在preRestart中再次将消息发送给B ,请参见下面的代码。

What I've done in B is to send the message again to B in preRestart, see code below.

  @Override
  public void preRestart(final Throwable reason, final scala.Option<Object> message) throws Exception
  {
    getSelf().tell(message.get(), getSender());
  };

为了确保我不会陷入无限循环,请按以下步骤在A中配置主管策略:

To ensure I don't end in an endless loop, I configure the supervisor strategy in A as follows:

  private final SupervisorStrategy strategy = new OneForOneStrategy(3, Duration.Inf(),
      new Function<Throwable, SupervisorStrategy.Directive>()
  {   
    @Override
    public Directive apply(final Throwable t) throws Exception
    {
      if (t instanceof SpecificException)
      {
        return SupervisorStrategy.restart();
      }
      return SupervisorStrategy.escalate();
    }
  });    

这应该保证,有问题的消息仅被重复发送三次。如果这是好的做法,有人可以给我建议吗?

This should gurarantee, that the problematic message is resent only three times. Could somebody give me an advice if this is good practice or link me to a better solution?

这篇关于通过主管策略重新启动actor后,如何重新发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆