spring-projects / spring-statemachine

Spring Statemachine is a framework for application developers to use state machine concepts with Spring.
1.53k stars 599 forks source link

Is there a way to validate when the event received not corresponds to the desired state? #1020

Closed gustavodaquino closed 2 years ago

gustavodaquino commented 2 years ago

Good afternoon!

On the example below, suppose that I start a new instance of a SM, and now it is on S1 state. If now I send a S3 event, how can I validate this wrong attempt of transition?

image

I searched in docs, in some questions posted in SO, and none of the approaches sounds good for me, because all of them involves write some arbitrary data on context and treat accordingly to take some decision, throw an exception, et cetera.

There is a clean way on framework to deal with this situation?

se0kjun commented 2 years ago

If you configure statemachine as you've attached one, it has never transited to S3 state from S1 state even you send S3 event, different S3 state, to transit to S3 state.

There are no triggers, events or timer, on you statemachine configuration to transit from S1 state to S3 state. If you want to validate or control when event is not accepted, you may implement eventNotAccepted on StateMachineListener.

...
  /**
   * Notified when event was not accepted.
   *
   * @param event the event
   */
  void eventNotAccepted(Message<E> event);
...
gustavodaquino commented 2 years ago

Thanks, @se0kjun , but I'm wondering if there is another way to validate this incorrect event.

When I send stateMachine.sendEvent("S3"), I would like to my stateMachine object alerts my application in the same thread execution, where I could return a boolean false, or throw an exception.

In this function you reference, an exception throw is handled only by state machine, is it correct?