有没有办法知道 Akka actor 的上下文?
Is there a way to know the context of an Akka actor?
我有一个具有某些上下文的演员,即
def step2: Receive = {
case _ => log error "Unhandled message"
}
def step1: Receive = {
case _ => log error "Unhandled message"
}
有没有办法知道actor当前处于哪个状态(step1,step2)?
(我可以将价值储存在 string
上,但我想知道是否有更好的方法。)
如果不可能,我想明白为什么这个状态应该保存在某个地方。
有限状态机
您可以使用 FSM
。 stateName
给出州名。
使用在每个状态处理的一些特殊消息 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
}
Hacky 解决方案
Akka Actor 不存储有关 PartialFunction 的任何特定信息。所以我认为不会有现成的 akka lib 函数。
将 state
放入 actor 中,然后在 Actor 尝试变成某物时更新状态。
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"
}
}
}
}
我有一个具有某些上下文的演员,即
def step2: Receive = {
case _ => log error "Unhandled message"
}
def step1: Receive = {
case _ => log error "Unhandled message"
}
有没有办法知道actor当前处于哪个状态(step1,step2)?
(我可以将价值储存在 string
上,但我想知道是否有更好的方法。)
如果不可能,我想明白为什么这个状态应该保存在某个地方。
有限状态机
您可以使用 FSM
。 stateName
给出州名。
使用在每个状态处理的一些特殊消息 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
}
Hacky 解决方案
Akka Actor 不存储有关 PartialFunction 的任何特定信息。所以我认为不会有现成的 akka lib 函数。
将 state
放入 actor 中,然后在 Actor 尝试变成某物时更新状态。
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"
}
}
}
}