Steve H.
Steve H.

Reputation: 6947

Spring Statemachine persistence

I am testing Spring Statemachine and in particular I am interesting in applying the statemachine to manage the state of my objects.

My Statemachine is of type StateMachine<EpisodeState, EpisodeEvent>.

My business object, Episode, has an enum property (state) of type EpisodeState, which should hold the statemachine state of the episode. I have a batch process that will obtain an instance of Statemachine at initialization. I would like to follow the basic flow:

The documentation mentions an extendedState property, which in my tests is empty, but seems to support a map of arbitrary objects which I suppose I could use to hold the primary key of my Episode, but I am at a loss as to how to set the current state of the statemachine to the EpisodeState value in the Episode.

I have configured the statemachine with a StateMachineInterceptorAdapter<EpisodeState, EpisodeEvent>, and I can see the information at pre/post stateChange and at pre/post Transition, as well as the preEvent.

Upvotes: 2

Views: 1437

Answers (1)

Steve H.
Steve H.

Reputation: 6947

Further research (not in Spring Statemachine docs), I found a way to set the state of the statemachine:

Assuming you have the desired beginning state in a variable called startingState, you would do it like this:

stateMachine.stop();
stateMachine
    .getStateMachineAccessor()
    .doWithAllRegions(access ->
        access.resetStateMachine(new DefaultStateMachineContext<>(startingState, null, null, null)));
stateMachine.start();

Upvotes: 6

Related Questions