Spring statemachine provides following type of actions. Can someone please explain me which type of action should be used in which scenario? And, If an action fails then does statemachine move to next state or not?
- Event action - Action attached to a transition when an event is generated.
- State entry action
- State exit action
- State DoAction
My scenario is that when statemachine comes in S1 state it needs to perform some operation and if its successful then only go into S2. Should it be state action or event action?
S1--->S2
CodePudding user response:
Event action: This is most the common case, you have StateA and want to move to StateB when EventAB is received. Action will be fired if the machine is on StateA and you submit the EventAB to it. Its possible (and optional) to provide an error action to do personal processing in case exception is throw from regular action
State entry action: Action will be hit every time machine reaches specified states. I've seen people using it to provide Initial state custom behavior
Same as above, but this is hit on every state exit
I haven't used this one, but according to the documentation (snippet below), its similar to state entry (in case you provide single action), but can also work with an error action.
The caveats is that actions can be attached to transitions (i.e from A to B, or from B to A), so it matters the FROM and TO, while actions configured for specific state do not care where it came from, but solely related to state enter/exit
//Specify a state S with state behaviour Action. Currently synonym for state(Object, Action).
StateConfigurer<S, E> stateDo(S state, Action<S, E> action);
In case an entry/transition action fails (and you're not in a choice pseudo-state), machine stays in the same state (doesn't move).
For your scenario, it depends, is S1 initial state? if that is, you might need a state action. Otherwise, transition action between S1 -> S2 is just fine.