4

I am using Spring Statemachine to provide a user's workflow. I need to persist the state changes so that user's state is not lost across restarts. Now, I can do this based on the examples provided, however one thing missing is how to recreate the state if a crash does occur.

Basically, I want to create the state machine and tell it set itself to the last state it had before the crash and copy any extended state variables from the database. Is there a way to do this?

3
  • Features around this are getting better in future releases, but based on example you followed, you can reset machine state. You didn't mention if you have multiple users or if machine is a static just for a one user. Persist example is using machine to update DB and it will reset state(thought it's very manual at this moment). Think you can do same? Oct 9, 2015 at 7:36
  • Right, I was wondering if that was just a hack or if it would work ok. Right now, each user has their own state machine that lives in their session. When they start I need to check the database to see if they have an incomplete workflow and create a state machine in the same state as they left it. I will try using the same reset mechanism as in the example
    – teknokrat
    Oct 10, 2015 at 10:40
  • I added a simple answer. If you have any ideas how to make machine/db integration easier, feel free to create a github ticket and we can continue there. I do hate the fact that you need to use jdbcTemplate manually. It'd be nice if we could somehow do things automatically, so any ideas over this would be nice! Oct 10, 2015 at 11:17

2 Answers 2

8

Maybe this can help you:

stateMachine
                .getStateMachineAccessor()
                .doWithAllRegions(access -> {
                    access.resetStateMachine(new DefaultStateMachineContext<>({ResetState}, null, null, null, null));
                });
stateMachine.start();
stateMachine.sendEvent({NewEventFromResetState});
1
  • was this option removed from latest version? I dont see the access.resetStateMachine while using 2.4.0 @legoscia Aug 3, 2021 at 22:09
5

Persist sample is using PersistStateMachineHandler recipe to update stuff in a db when state machine is transitioning between states. One important thing to remember in this recipe is that it is using interceptor instead of listener to hook into state changes. If db update is done within an interceptor callback, in case of error/exception, transition in a state machine is denied, while if one would use listener error would cause state machine and db become inconsistent between each others.

One other thing is that this recipe allows to reset machine state into a particular state and then continue from there.

It really doesn't matter if new machine is created for every update if user doesn't care about speed and garbage. State machine instantiation is relatively expensive so simply using one instance and then reseting its state is relatively light operation.

How you interact with db from state machine hooks is very low level at this moment because you need to do everything manually in terms of how to interact with db. There's no automatic tweaks currently because we simply don't know what's in db and how rows would be updated.

3
  • Right, I don't see a huge number of options available. I have gone for the interceptor option to persist the current state and extended state in a redis cluster on every state change. I currently use the state machine id in the header fields to check that the machine the user gets is what the user has been using. If there is a mismatch, I load the data of the old machine and reset the new one to it. It works in testing so far.
    – teknokrat
    Oct 11, 2015 at 22:04
  • I am still a beginner. I was looking at the persist recipe and sample code. I am still wondering where exactly is this 'reset' happening. Oct 18, 2016 at 11:14
  • hey @janne-valkealahti let's say I have a task management system that uses a state manage to validate the workflow and persist transitions for several users, I'm not sure which approach is best. The way I understand is that there are 2 approaches: 1 )each workflow will have its state machine persisted, I need to acquire it from DB and push the new event to it 2) i have a single instance of state machine in memory and need to feed(rehydrate) that same instance with context from the latest state. Machine instantiation is expensive but single machine is blocking for multiple users, is that right? Jul 6, 2021 at 15:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.