我想使用 FSM Akka Acctor 进行存储/取消存储。我不知道在哪里放stash()
and unstashAll()
。
我在下面有一个简化的例子:
import akka.actor.{ActorSystem, FSM, Props, Stash}
trait TestState
case object StateA extends TestState
case object StateB extends TestState
case class TestData()
case class MessageA(msg: String)
case class MessageB(msg: String)
case object ChangeState
class TestFSM extends FSM[TestState, TestData] with Stash {
startWith(StateA, TestData())
when(StateA) {
case Event(MessageA(msgA), _) =>
println(s"In StateA: $msgA")
stay()
case Event(ChangeState, _) =>
println("Changing state from A to B")
goto(StateB)
}
when(StateB) {
case Event(MessageB(msgB), _) =>
println(s"In StateB: $msgB")
stay()
}
whenUnhandled {
case Event(e, _) =>
println(s"Unhandled event: $e")
stay()
}
}
object TestFSM extends App {
val system = ActorSystem("test-system")
val actor = system.actorOf(Props[TestFSM])
actor ! MessageA("Apple 1")
actor ! MessageB("Banana 1")
actor ! MessageA("Apple 2")
actor ! ChangeState
actor ! MessageB("Banana 2")
}
初始状态为StateA
。当在StateA
actor 中时,应该只处理 type 的消息MessageA
。如果它收到任何其他类型的消息(除了ChangeState
),它应该将其隐藏。收到消息ChangeState
后,actor 应更改为StateB
. 从 更改为StateA
后StateB
,它应该取消隐藏所有消息。当在StateB
actor 中时,应该只处理 type 的消息MessageB
。
我不确定在哪里使用stash()
andunstashAll()
来实现这一点。
我运行的输出是:
In StateA: Apple 1
Unhandled event: MessageB(Banana 1)
In StateA: Apple 2
Changing state from A to B
In StateB: Banana 2
我想看到的输出是:
In StateA: Apple 1
In StateA: Apple 2
Changing state from A to B
In StateB: Banana 1
In StateB: Banana 2
非常感谢。