我注意到演员最初发送了有关状态变化的消息,后来确实已经改变了这种状态。这是对的吗?
class MyActor extends Actor {
def receive = idle(Set.empty)
def idle(isInSet: Set[String]): Receive = {
case Add(key) =>
// sending the result as a message back to our actor
validate(key).map(Validated(key, _)).pipeTo(self)
// waiting for validation
context.become(waitForValidation(isInSet, sender()))
}
def waitForValidation(set: Set[String], source: ActorRef): Receive = {
case Validated(key, isValid) =>
val newSet = if (isValid) set + key else set
// sending acknowledgement of completion
source ! Continue
这里发生了发送通知
// go back to idle, accepting new requests
context.become(idle(newSet))
后来改变了状态
case Add(key) =>
sender() ! Rejected
}
def validate(key: String): Future[Boolean] = ???
}
// Messages
case class Add(key: String)
case class Validated(key: String, isValid: Boolean)
case object Continue
case object Rejected
答案 0 :(得分:0)
如果希望actor在waitForValidation状态下接收消息,您应该考虑在pipeTo(self)之前移动become():
context.become(waitForValidation(isInSet, sender()))
validate(key).map(Validated(key, _)).pipeTo(self)
我同意管道消息将把它放入队列中,并且当对象处理它时,对象应该处于新状态,但是我看到的大多数示例在管道之前调用了保持安全。