FSM Actor does not fire Toggle after going to the same state

According to Akka FSM :

Note Transitions with the same state can be implemented (currently in state S) using goto (S) or stay (). The difference is that goto (S) will trigger S-> S events, which can be handled by onTransition, whereas stay () will not.

I created an actor by extending FSM. When my actor is "On" with state data Data3, it switches to state "On" with state data Data2. I'm using "goto (On)" using Data2. I expected this "onTransition On-> On" method to be executed, but it doesn't. The On-> On event should be selected. Here is the 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 am 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()
}

      

+3


source to share


1 answer


This issue is reported and will be fixed in version 2.4



+3


source







All Articles