1

I've just started experimenting with the Spring state machine library. I have a simple state machine I'm trying to model, however I want to be notified whenever I provide an event that isn't a valid transition for the state the machine is currently in.

For example, if my state machine only allows transitioning from A to B if I receive event X, then I would like to be notified if the state is A but event Y is received because this is an error.

I know this would be possible by setting up transitions for all possible negative states and handling it there, but ideally there would be some way to only allow the transitions I explicitly define and alert me if anything else happens. Is this possible?

0

2 Answers 2

3

you can use a statemachine listener to catch a not accepted event:

...    

@Override
public void configure(
    StateMachineConfigurationConfigurer
                            <States, Events> config) throws Exception {

    config.withConfiguration()
          .listener(listener()) 
          .autoStartup(true);
}

...

private StateMachineListener<States, Events> listener() {
        return new StateMachineListenerAdapter<States, Events>(){

                @Override
                public void eventNotAccepted(Message<Events> event) { 
                        log.error("event not accepted: {}", event);
                }
        };
}
Sign up to request clarification or add additional context in comments.

Comments

2

The sendEvent method on the state machine returns a boolean signifying if the transition succeeded.

boolean accepted = stateMachine.sendEvent("event");

In this case "accepted" would tell you if the transition succeeded. If the boolean comes back as true that means the transition happened, if it comes back as false that means the transition was invalid and nothing was done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.