转到同一状态后,FSM actor不会触发onTransition

时间:2014-12-08 11:08:25

标签: scala akka

根据Akka FSM docs

  

请注意   可以使用goto(S)或stay()来实现相同状态转换(当前处于状态S时)。那些goto(S)之间的区别将发出一个事件S-> S事件,可以由onTransition处理,而stay()则不会。

我通过扩展FSM创建了actor。当我的actor处于状态“On”状态数据Data3时,它切换到状态数据Data2的状态“On”。我使用“goto(On)”使用Data2。我预计会执行“onTransition On-> On”方法,但事实并非如此。应该发出Event On-> On。 这是我运行这个actor时的输出:

Off, event Message! with state Data1
onTransition: Off->On Data3
On, timeout with Data3
On, timeout with Data2
On, timeout with Data1
onTransition: On ->Off Data1

知道我做错了吗?

以下是来源:

import akka.actor.FSM
import scala.concurrent.duration._

trait State
case object On extends State
case object Off extends State

sealed trait Data
case object Data1 extends Data
case object Data2 extends Data
case object Data3 extends Data

class SomeFsm extends FSM[State,Data] {

  startWith(Off,Data1)

  when(On,stateTimeout = 1 second) {
    case Event(StateTimeout,Data3) => println("On, timeout with Data3");goto(On) using Data2
    case Event(StateTimeout,Data2) => println("On, timeout with Data2");goto(On) using Data1
    case Event(StateTimeout,Data1) => println("On, timeout with Data1");goto(Off) using Data1
  }

  when(Off){
    case Event(m,s) => println(s"Off, event $m with state $s");goto(On) using Data3
  }

  whenUnhandled{
    case Event(e, s) =>
      log.warning("received unhandled request {} in state {}/{}", e, stateName, s)
      stay()
  }

  onTransition{
    case On -> On =>
      print("onTransition: ")
      nextStateData match {
      case Data3=> println("On ->On (Data3")
      case Data2=> println("On ->On (Data2")
      case Data1=> println("On ->On (Data1")
    }
    case On-> Off => println(s"onTransition: On ->Off $nextStateData")
    case Off-> On => println(s"onTransition: Off->On $nextStateData")
  }

  initialize()
}

1 个答案:

答案 0 :(得分:3)

issue已报告,将在version 2.4

中修复