gpt4 book ai didi

scala - preStart 钩子(Hook) : a message to the actor itself

转载 作者:行者123 更新时间:2023-12-04 02:42:10 25 4
gpt4 key购买 nike

假设我覆盖了 preStart钩住并向self发送消息:

Class SomeActor extends Actor {

override def preStart(): Unit = {
self ! SomeMessage
}

...

}

我可以期待 SomeMessage将是队列中的第一条消息?

最佳答案

不,因为actor创建是异步发生的,有人可能在构造函数或preStart之前将消息排入队列。实际运行。如果您需要确保先处理此消息,则需要使用 becomestash :

self ! SomeMessage

def receive = initial

def initial: Receive = {
case SomeMessage =>
// do stuff
unstashAll()
context become initialized
case _ => stash()
}

def initialized: Receive = {
// your normal behavior
}

您需要混合 akka.actor.Stash特征并配置此actor以使用 DequeBasedMailbox .

关于scala - preStart 钩子(Hook) : a message to the actor itself,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16337551/

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