Interface FSM<S,D>
-
- All Superinterfaces:
Actor,ActorLogging,Listeners
- All Known Subinterfaces:
LoggingFSM<S,D>
- All Known Implementing Classes:
AbstractFSM,AbstractFSMWithStash,AbstractLoggingFSM,ClusterSingletonManager
public interface FSM<S,D> extends Actor, Listeners, ActorLogging
Finite State Machine actor trait. Use as follows:object A { trait State case class One extends State case class Two extends State case class Data(i : Int) } class A extends Actor with FSM[A.State, A.Data] { import A._ startWith(One, Data(42)) when(One) { case Event(SomeMsg, Data(x)) => ... case Event(SomeOtherMsg, _) => ... // when data not needed } when(Two, stateTimeout = 5 seconds) { ... } initialize() }Within the partial function the following values are returned for effecting state transitions:
-
stayfor staying in the same state -stay using Data(...)for staying in the same state, but with different data -stay forMax 5.millisfor staying with a state timeout; can be combined withusing-goto(...)for changing into a different state; also supportsusingandforMax-stopfor terminating this FSM actorEach of the above also supports the method
replying(AnyRef)for sending a reply before changing state.While changing state, custom handlers may be invoked which are registered using
onTransition. This is meant to enable concentrating different concerns in different places; you may choose to usewhenfor describing the properties of a state, including of course initiating transitions, but you can describe the transitions usingonTransitionto avoid having to duplicate that code among multiple paths which lead to a transition:onTransition { case Active -> _ => cancelTimer("activeTimer") }Multiple such blocks are supported and all of them will be called, not only the first matching one.
Another feature is that other actors may subscribe for transition events by sending a
SubscribeTransitionCallbackmessage to this actor. Stopping a listener without unregistering will not remove the listener from the subscription list; useUnsubscribeTransitionCallbackbefore stopping the listener.State timeouts set an upper bound to the time which may pass before another message is received in the current state. If no external message is available, then upon expiry of the timeout a StateTimeout message is sent. Note that this message will only be received in the state for which the timeout was set and that any message received will cancel the timeout (possibly to be started again by the next transition).
Another feature is the ability to install and cancel single-shot as well as repeated timers which arrange for the sending of a user-specified message:
setTimer("tock", TockMsg, 1 second, true) // repeating setTimer("lifetime", TerminateMsg, 1 hour, false) // single-shot cancelTimer("tock") isTimerActive("tock")
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classFSM.$minus$greater$This extractor is just convenience for matching a (S, S) pair, including a reminder what the new state is.static classFSM.CurrentState<S>Message type which is sent directly to the subscribed actor inFSM.SubscribeTransitionCallBackbefore sending anyFSM.Transitionmessages.static classFSM.CurrentState$static classFSM.Event<D>All messages sent to theFSMwill be wrapped inside anEvent, which allows pattern matching to extract both state and data.static classFSM.Event$static classFSM.FailureSignifies that theFSMis shutting itself down because of an error, e.g.static classFSM.Failure$static classFSM.FixedDelayMode$INTERNAL APIstatic classFSM.FixedRateMode$INTERNAL APIstatic classFSM.LogEntry<S,D>Log Entry of theLoggingFSM, can be obtained by callinggetLog.static classFSM.LogEntry$static classFSM.Normal$Default reason if callingstop().static classFSM.NullFunction$A partial function value which does not match anything and can be used to “reset”whenUnhandledandonTerminationhandlers.static interfaceFSM.ReasonReason why thisFSMis shutting down.static classFSM.Shutdown$Reason given when someone was callingsystem.stop(fsm)from outside; also applies toStopsupervision directive.static classFSM.SilentState<S,D>INTERNAL API Using a subclass for binary compatibility reasonsstatic classFSM.SingleMode$INTERNAL APIstatic classFSM.State<S,D>static classFSM.State$This captures all of the managed state of theFSM: the state name, the state data, possibly custom timeout, stop reason and replies accumulated while processing the last message.static classFSM.StateTimeout$This case object is received in case of a state timeout.static classFSM.StopEvent<S,D>Case class representing the state of theFSMwithin theonTerminationblock.static classFSM.StopEvent$static classFSM.SubscribeTransitionCallBackSend this to anFSMto request first theFSM.CurrentStateand then a series ofFSM.Transitionupdates.static classFSM.SubscribeTransitionCallBack$static classFSM.TimerINTERNAL APIstatic classFSM.Timer$static interfaceFSM.TimerModeINTERNAL APIstatic classFSM.TransformHelperstatic classFSM.Transition<S>Message type which is used to communicate transitions between states to all subscribed listeners (useFSM.SubscribeTransitionCallBack).static classFSM.Transition$static classFSM.UnsubscribeTransitionCallBackUnsubscribe fromFSM.Transitionnotifications which was effected by sending the correspondingFSM.SubscribeTransitionCallBack.static classFSM.UnsubscribeTransitionCallBack$-
Nested classes/interfaces inherited from interface akka.actor.Actor
Actor.emptyBehavior$, Actor.ignoringBehavior$
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description FSM.$minus$greater$$minus$greater()This extractor is just convenience for matching a (S, S) pair, including a reminder what the new state is.voidakka$actor$FSM$_setter_$Event_$eq(FSM.Event$ x$1)voidakka$actor$FSM$_setter_$StateTimeout_$eq(FSM.StateTimeout$ x$1)This case object is received in case of a state timeout.voidakka$actor$FSM$_setter_$StopEvent_$eq(FSM.StopEvent$ x$1)voidapplyState(FSM.State<S,D> nextState)voidcancelTimer(java.lang.String name)voidcurrentState_$eq(FSM.State<S,D> x$1)booleandebugEvent()FSM.Event$Event()voidgeneration_$eq(long x$1)voidhandleEvent_$eq(scala.PartialFunction<FSM.Event<D>,FSM.State<S,D>> x$1)voidhandleTransition(S prev, S next)voidinitialize()booleanisStateTimerActive()booleanisTimerActive(java.lang.String name)voidlogTermination(FSM.Reason reason)By defaultFSM.Failureis logged at error level and other reason types are not logged.voidmakeTransition(FSM.State<S,D> nextState)voidnextState_$eq(FSM.State<S,D> x$1)DnextStateData()voidonTermination(scala.PartialFunction<FSM.StopEvent<S,D>,scala.runtime.BoxedUnit> terminationHandler)voidonTransition(scala.PartialFunction<scala.Tuple2<S,S>,scala.runtime.BoxedUnit> transitionHandler)voidpostStop()User overridable callback.voidprocessEvent(FSM.Event<D> event, java.lang.Object source)voidprocessMsg(java.lang.Object value, java.lang.Object source)scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit>receive()Scala API: This defines the initial actor behavior, it must return a partial function with the actor logic.voidregister(S name, scala.PartialFunction<FSM.Event<D>,FSM.State<S,D>> function, scala.Option<scala.concurrent.duration.FiniteDuration> timeout)voidsetStateTimeout(S state, scala.Option<scala.concurrent.duration.FiniteDuration> timeout)voidsetTimer(java.lang.String name, java.lang.Object msg, scala.concurrent.duration.FiniteDuration timeout, boolean repeat)Deprecated.Use startSingleTimer, startTimerWithFixedDelay or startTimerAtFixedRate instead.booleansetTimer$default$4()voidstartSingleTimer(java.lang.String name, java.lang.Object msg, scala.concurrent.duration.FiniteDuration delay)voidstartTimer(java.lang.String name, java.lang.Object msg, scala.concurrent.duration.FiniteDuration timeout, FSM.TimerMode mode)voidstartTimerAtFixedRate(java.lang.String name, java.lang.Object msg, scala.concurrent.duration.FiniteDuration interval)voidstartTimerWithFixedDelay(java.lang.String name, java.lang.Object msg, scala.concurrent.duration.FiniteDuration delay)voidstartWith(S stateName, D stateData, scala.Option<scala.concurrent.duration.FiniteDuration> timeout)Set initial state.scala.Option<scala.concurrent.duration.FiniteDuration>startWith$default$3()DstateData()SstateName()FSM.StateTimeout$StateTimeout()This case object is received in case of a state timeout.FSM.State<S,D>stay()Produce "empty" transition descriptor.FSM.State<S,D>stop()Produce change descriptor to stop this FSM actor with reason "Normal".FSM.State<S,D>stop(FSM.Reason reason)Produce change descriptor to stop this FSM actor including specified reason.FSM.State<S,D>stop(FSM.Reason reason, D stateData)Produce change descriptor to stop this FSM actor including specified reason.FSM.StopEvent$StopEvent()voidsuper$postStop()CallonTerminationhook; if you want to retain this behavior when overriding make sure to callsuper.postStop().voidterminate(FSM.State<S,D> nextState)voidterminateEvent_$eq(scala.PartialFunction<FSM.StopEvent<S,D>,scala.runtime.BoxedUnit> x$1)voidtimeoutFuture_$eq(scala.Option<Cancellable> x$1)scala.PartialFunction<scala.Tuple2<S,S>,scala.runtime.BoxedUnit>total2pf(scala.Function2<S,S,scala.runtime.BoxedUnit> transitionHandler)FSM.TransformHelpertransform(scala.PartialFunction<FSM.Event<D>,FSM.State<S,D>> func)voidtransitionEvent_$eq(scala.collection.immutable.List<scala.PartialFunction<scala.Tuple2<S,S>,scala.runtime.BoxedUnit>> x$1)voidwhen(S stateName, scala.concurrent.duration.FiniteDuration stateTimeout, scala.PartialFunction<FSM.Event<D>,FSM.State<S,D>> stateFunction)Insert a new StateFunction at the end of the processing chain for the given state.scala.concurrent.duration.FiniteDurationwhen$default$2()voidwhenUnhandled(scala.PartialFunction<FSM.Event<D>,FSM.State<S,D>> stateFunction)-
Methods inherited from interface akka.actor.Actor
akka$actor$Actor$_setter_$context_$eq, akka$actor$Actor$_setter_$self_$eq, aroundPostRestart, aroundPostStop, aroundPreRestart, aroundPreStart, aroundReceive, context, postRestart, preRestart, preStart, self, sender, supervisorStrategy, unhandled
-
Methods inherited from interface akka.actor.ActorLogging
_log_$eq, log
-
Methods inherited from interface akka.routing.Listeners
akka$routing$Listeners$_setter_$listeners_$eq, gossip, gossip$default$2, listenerManagement, listeners
-
-
-
-
Method Detail
-
akka$actor$FSM$_setter_$Event_$eq
void akka$actor$FSM$_setter_$Event_$eq(FSM.Event$ x$1)
-
akka$actor$FSM$_setter_$StopEvent_$eq
void akka$actor$FSM$_setter_$StopEvent_$eq(FSM.StopEvent$ x$1)
-
akka$actor$FSM$_setter_$StateTimeout_$eq
void akka$actor$FSM$_setter_$StateTimeout_$eq(FSM.StateTimeout$ x$1)
This case object is received in case of a state timeout.
-
super$postStop
void super$postStop()
CallonTerminationhook; if you want to retain this behavior when overriding make sure to callsuper.postStop().Please note that this method is called by default from
preRestart(), so override that one ifonTerminationshall not be called during restart.
-
Event
FSM.Event$ Event()
-
StopEvent
FSM.StopEvent$ StopEvent()
-
$minus$greater
FSM.$minus$greater$ $minus$greater()
This extractor is just convenience for matching a (S, S) pair, including a reminder what the new state is.
-
StateTimeout
FSM.StateTimeout$ StateTimeout()
This case object is received in case of a state timeout.
-
when
void when(S stateName, scala.concurrent.duration.FiniteDuration stateTimeout, scala.PartialFunction<FSM.Event<D>,FSM.State<S,D>> stateFunction)
Insert a new StateFunction at the end of the processing chain for the given state. If the stateTimeout parameter is set, entering this state without a differing explicit timeout setting will trigger a StateTimeout event; the same is true when using #stay.- Parameters:
stateName- designator for the statestateTimeout- default state timeout for this statestateFunction- partial function describing response to input
-
when$default$2
scala.concurrent.duration.FiniteDuration when$default$2()
-
startWith
void startWith(S stateName, D stateData, scala.Option<scala.concurrent.duration.FiniteDuration> timeout)
Set initial state. Call this method from the constructor before theinitialize()method. If different state is needed after a restart this method, followed byinitialize(), can be used in the actor life cycle hooksActor.preStart()andActor.postRestart(java.lang.Throwable).- Parameters:
stateName- initial state designatorstateData- initial state datatimeout- state timeout for the initial state, overriding the default timeout for that state
-
startWith$default$3
scala.Option<scala.concurrent.duration.FiniteDuration> startWith$default$3()
-
stay
FSM.State<S,D> stay()
Produce "empty" transition descriptor. Return this from a state function when no state change is to be effected.No transition event will be triggered by
stay(). If you want to trigger an event likeS -> SforonTransitionto handle usegotoinstead.- Returns:
- descriptor for staying in current state
-
stop
FSM.State<S,D> stop(FSM.Reason reason)
Produce change descriptor to stop this FSM actor including specified reason.
-
stop
FSM.State<S,D> stop(FSM.Reason reason, D stateData)
Produce change descriptor to stop this FSM actor including specified reason.
-
startTimerWithFixedDelay
void startTimerWithFixedDelay(java.lang.String name, java.lang.Object msg, scala.concurrent.duration.FiniteDuration delay)
-
startTimerAtFixedRate
void startTimerAtFixedRate(java.lang.String name, java.lang.Object msg, scala.concurrent.duration.FiniteDuration interval)
-
startSingleTimer
void startSingleTimer(java.lang.String name, java.lang.Object msg, scala.concurrent.duration.FiniteDuration delay)
-
setTimer
void setTimer(java.lang.String name, java.lang.Object msg, scala.concurrent.duration.FiniteDuration timeout, boolean repeat)Deprecated.Use startSingleTimer, startTimerWithFixedDelay or startTimerAtFixedRate instead. This has the same semantics as startTimerAtFixedRate, but startTimerWithFixedDelay is often preferred. Since 2.6.0.
-
setTimer$default$4
boolean setTimer$default$4()
-
startTimer
void startTimer(java.lang.String name, java.lang.Object msg, scala.concurrent.duration.FiniteDuration timeout, FSM.TimerMode mode)
-
cancelTimer
void cancelTimer(java.lang.String name)
-
isTimerActive
boolean isTimerActive(java.lang.String name)
-
setStateTimeout
void setStateTimeout(S state, scala.Option<scala.concurrent.duration.FiniteDuration> timeout)
-
isStateTimerActive
boolean isStateTimerActive()
-
onTransition
void onTransition(scala.PartialFunction<scala.Tuple2<S,S>,scala.runtime.BoxedUnit> transitionHandler)
-
total2pf
scala.PartialFunction<scala.Tuple2<S,S>,scala.runtime.BoxedUnit> total2pf(scala.Function2<S,S,scala.runtime.BoxedUnit> transitionHandler)
-
onTermination
void onTermination(scala.PartialFunction<FSM.StopEvent<S,D>,scala.runtime.BoxedUnit> terminationHandler)
-
whenUnhandled
void whenUnhandled(scala.PartialFunction<FSM.Event<D>,FSM.State<S,D>> stateFunction)
-
initialize
void initialize()
-
stateName
S stateName()
-
stateData
D stateData()
-
nextStateData
D nextStateData()
-
debugEvent
boolean debugEvent()
-
timeoutFuture_$eq
void timeoutFuture_$eq(scala.Option<Cancellable> x$1)
-
generation_$eq
void generation_$eq(long x$1)
-
register
void register(S name, scala.PartialFunction<FSM.Event<D>,FSM.State<S,D>> function, scala.Option<scala.concurrent.duration.FiniteDuration> timeout)
-
terminateEvent_$eq
void terminateEvent_$eq(scala.PartialFunction<FSM.StopEvent<S,D>,scala.runtime.BoxedUnit> x$1)
-
transitionEvent_$eq
void transitionEvent_$eq(scala.collection.immutable.List<scala.PartialFunction<scala.Tuple2<S,S>,scala.runtime.BoxedUnit>> x$1)
-
receive
scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> receive()
Description copied from interface:ActorScala API: This defines the initial actor behavior, it must return a partial function with the actor logic.
-
processMsg
void processMsg(java.lang.Object value, java.lang.Object source)
-
postStop
void postStop()
Description copied from interface:ActorUser overridable callback. Is called asynchronously after 'actor.stop()' is invoked. Empty default implementation.
-
logTermination
void logTermination(FSM.Reason reason)
By defaultFSM.Failureis logged at error level and other reason types are not logged. It is possible to override this behavior.
-
-