FSM actor is not firing onTransition after goto to

2019-07-07 08:44发布

问题:

According to Akka FSM docs:

Note Same-state transitions can be implemented (when currently in state S) using goto(S) or stay(). The difference between those being that goto(S) will emit an event S->S event that can be handled by onTransition, whereas stay() will not.

I have created actor by extending FSM. When my actor is in state "On" with state data Data3, it switch to state "On" with state data Data2. I use "goto(On) using Data2. I have expected that method "onTransition On->On" will be performed but it is not. Event On->On should be emitted. Here is an output when I run this 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

Any idea what I'm doing wrong?

Here is the source:

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:

This issue is reported and will be fixed in version 2.4



标签: scala akka