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();
}
}
Run Code Online (Sandbox Code Playgroud)
收到Init消息后,我看到写入以下日志语句:
Sending myself a Poison Pill...
Run Code Online (Sandbox Code Playgroud)
但我从未见过:
Got the Poison Pill...
Run Code Online (Sandbox Code Playgroud)
此外,该应用程序只是坐在那里,并没有按预期关闭.我使用的是self().tell(PoisonPill.getInstance(), self())什么阻止它接收消息并关闭?
日志消息不会出现,因为PoisonPill是AutoReceivedMessage.An AutoReceivedMessage是一种特殊类型的消息,Akka在内部处理,并不意味着在用户代码中进行模式匹配.
一旦actor被"中毒"/停止,关闭actor系统的postStop()一种方法是覆盖actor的方法:
@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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
168 次 |
| 最近记录: |