Spring State Machine send-event returns true when guard is false

1.8k views Asked by At

I'm implementing a state machine with Spring Boot SM. One of the transitions only happens when a Guard constraint is successful:

.withExternal().source(ProjectStatus.STATE2).target(StatutProjet.STATE3).event(ProjectEvent.next).guard(quoteSelected());

when I send an event :

if (stateMachine.sendEvent(message)) {
   // Event is accepted so I could return OK response
} else {
   // Event is not accepted so I could return some error handling process
}

The event is accepted even if the guard returns false and the transition doesn't happen. This is a problem because this behaviour looks like I'm having a self-target transition.

Did I miss something?

1

There are 1 answers

0
Dauren D On

Accepting event does not mean the processing event itself. Here, returning boolean value Spring tells you based on the current state of SM I can accept this event for further validation and processing.
You should consider a state machine as an async component because all guards and actions are executed in special task executor.
See Transition Action Error Handling for detailes.

In your case, as I understand you need to give immediate feedback to the initiator of the event. I can suggest you use State Machine's setStateMachineError and hasStateMachineError methods to get that feedback.