Spring Statemachine: how to handle two transitions that has the same source state?

1.5k Views Asked by At

How does Spring Statemachine handle two transitions that has the same source state?

For example:

  • Source State: ready
  • Event: start
  • One transition will be lead to the target state running, another one will be failure

enter image description here

Any help would be appreciated.

1

There are 1 best solutions below

0
On

The scenario you described above can be achieved in two ways.

In case of failure to process the incoming event (Start), machine would remain in the original state (ready). The transition is only completed when event is process successfully.

However, if you really need to get away from ready state and move to running or failure, one can use the Guard condition. You can use an evaluation function to define if the Start event was successful or not and define to which state machine should go to.

This can be achieved using a Choice pseudo-state that is protected by a Guard class, based on machine configuration and Guard evaluation, machine would move to the expected state

The official doc for reference on how to make the implementation is here