19. Using StateContext

StateContext is a one of a most important objects when working with a state machine as it is passed into various methods and callbacks to give status of a current state of a state machine and where it is possibly going. If simplifying things a little it can be considered to be a snapshot of a current state machine stage where it is at a time StateContext is passed on.

[Note]Note

In Spring Statemachine 1.0.x StateContext usage were relatively naive in terms of how it was used to just pass stuff around as a simple POJO. Starting from Spring Statemachine 1.1.x its role has been greatly improved by making it a first class citizen in a state machine.

In overall StateContext can be used as.

StateContext is passed into various components interacting with user like Action and Guard.

19.1 Stages

Stage is representation of a stage on which a state machine is currently interacting with a user. Current stages are EVENT_NOT_ACCEPTED, EXTENDED_STATE_CHANGED, STATE_CHANGED, STATE_ENTRY, STATE_EXIT, STATEMACHINE_ERROR, STATEMACHINE_START, STATEMACHINE_STOP, TRANSITION, TRANSITION_START and TRANSITION_END which look very familiar as those match how user can interact with listeners as described in Chapter 21, Listening State Machine Events.