繁体   English   中英

阿卡演员不能发送自己的PoisonPill

[英]Akka actor cannot send self a PoisonPill

Java / Akka在这里。 我有以下演员:

public class MyActor extends AbstractActor {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    public MyActor() {
        super();
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
            .match(Init.class, init -> {
                log.info("Sending myself a Poison Pill...");
                self().tell(PoisonPill.getInstance(), self());
            }).match(PoisonPill.class, poisonPill -> {
                log.info("Got the Poison Pill...");
                context().system().terminate();
            }).build();
    }
}

当它收到Init消息时,我看到写入以下日志语句:

Sending myself a Poison Pill...

但我从未见过:

Got the Poison Pill...

此外,该应用程序只是坐在那里,并没有按预期关闭。 我使用self().tell(PoisonPill.getInstance(), self())会阻止它接收消息并关闭吗?

由于PoisonPillAutoReceivedMessage因此不会显示日志消息。 AutoReceivedMessage是Akka内部处理的特殊类型的消息,并不意味着在用户代码中进行模式匹配。

一旦actor被“中毒”/停止,关闭actor系统的一种方法是覆盖actor的postStop()方法:

@Override
public Receive createReceive() {
  return receiveBuilder()
    .match(Init.class, init -> {
      log.info("Sending myself a Poison Pill...");
      self().tell(PoisonPill.getInstance(), ActorRef.noSender());
    })
    .build();
}

@Override
public void postStop() {
  getContext().getSystem().terminate();
}

暂无
暂无

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

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