Scala 有没有办法了解阿克卡演员的背景?
我有一个演员有一些背景,例如Scala 有没有办法了解阿克卡演员的背景?,scala,akka,actor,Scala,Akka,Actor,我有一个演员有一些背景,例如 def step2: Receive = { case _ => log error "Unhandled message" } def step1: Receive = { case _ => log error "Unhandled message" } 有没有办法知道参与者当前处于哪个状态(步骤1、步骤2)? (我可以将值存储在字符串上,但我想知道是否有更好的方法。) 如果不可能,我想了解为什么,因为这种状态应该保留在某个地方。FSM
def step2: Receive = {
case _ => log error "Unhandled message"
}
def step1: Receive = {
case _ => log error "Unhandled message"
}
有没有办法知道参与者当前处于哪个状态(步骤1、步骤2)?
(我可以将值存储在字符串上,但我想知道是否有更好的方法。)
如果不可能,我想了解为什么,因为这种状态应该保留在某个地方。FSM
您可以使用FSM
<代码>状态名
给出状态的名称
使用在每个状态中处理的一些特殊消息Ping Actor以发送stateName
sealed trait ExampleState
case object State1 extends ExampleState
case object State2 extends ExampleState
case object C extends ExampleState
import akka.actor.{Actor, FSM}
import akka.event.EventHandler
import akka.util.duration._
case object Move
class ABC extends Actor with FSM[ExampleState, Unit] {
import FSM._
startWith(State1, Unit)
when(State1) {
case Ev(Move) =>
EventHandler.info(this, "Go to B and move on after 5 seconds")
goto(state2) forMax (5 seconds)
}
when(State2) {
case Ev(StateTimeout) =>
EventHandler.info(this, "Moving to C")
goto(C)
}
when(C) {
case Ev(Move) =>
EventHandler.info(this, "Stopping")
stop
}
initialize // this checks validity of the initial state and sets up timeout if needed
}
黑胶溶液
Akka Actor不存储有关PartialFunction的任何特定信息。因此,我不认为会有一个akka库函数随时可用于此
在参与者内部设置state
,然后在参与者试图成为某个人物时更新状态
class FooBar extends Actor with ActorLogging {
var state: Option[String] = Some("receive")
override def receive: Receive = {
case _ => context become state1()
}
def state1: () => Receive = {
() => {
state = Some("state1")
{
case _ => log error "ignore"
}
}
}
def state2: () => Receive = {
() => {
state = Some("state2")
{
case _ => log error "ignore"
}
}
}
}
好的,如果没有人给出其他解决方案,我会接受你的答案,但是这个状态必须保存在某个地方,并且参与者可以访问,那么为什么不可能获得它呢?@Simon你可以在每个状态下都有一个案例,它将使用参与者将状态数据发送到参与者之外message@Simon你可以使用FSM。是的;如果无法在传统参与者中获得stateName,那么这可能是一个更干净的解决方案。我很想知道为什么在传统演员身上不可能做到,所以我明天会接受你的回答。谢谢;)@Simon traditional actor不维护stateName,而FSM维护stateName。我的黑客解决方案更接近fsm的实现,它被称为封装,这是一个特性:对不起,我不理解你的评论。