簡體   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