gpt4 book ai didi

java - Akka actor 不能给自己发送 PoisonPill

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:00:48 26 4
gpt4 key购买 nike

这里是 Java/Akka。我有以下 Actor :

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()) 时是否有什么东西阻止它接收消息并关闭?

最佳答案

日志消息没有出现,因为 PoisonPill是一个 AutoReceivedMessage . 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();
}

关于java - Akka actor 不能给自己发送 PoisonPill,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53125065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com