@RestController
class StateMachineController {
@Autowired
private lateinit var stateMachine: StateMachine<String, String>
@Autowired
private lateinit var runtimePersister: StateMachineRuntimePersister<String, String, String>
@GetMapping("/state")
fun state(String, @RequestParam("event") event: String): String {
resetStateMachineFromStore()
feedStateMachine(event)
return stateMachine.state.toString()
}
private fun resetStateMachineFromStore() {
val context = runtimePersister.read("ItemSM")
if (context != null) {
stateMachine.stop()
stateMachine.stateMachineAccessor.doWithRegion {
it.resetStateMachine(context)
}
stateMachine.start()
}
}
private fun feedStateMachine(event: String) {
stateMachine.sendEvent(event)
}
}
If I trigger the events in the following order the following SQL statements are being executed:
'E_IS1' -> UPDATE STATE_MACHINE SET STATE='S1', [...] WHERE MACHINE_ID='ItemSM'
'E_FRK' -> UPDATE STATE_MACHINE SET STATE='GROUP', [...] WHERE MACHINE_ID='ItemSM'
-> UPDATE STATE_MACHINE SET STATE='F1I', [...] WHERE MACHINE_ID='ItemSM#R1'
-> UPDATE STATE_MACHINE SET STATE='F3I', [...] WHERE MACHINE_ID='ItemSM#R3'
-> UPDATE STATE_MACHINE SET STATE='F2I', [...] WHERE MACHINE_ID='ItemSM#R2'
'E_F1IS' -> UPDATE STATE_MACHINE SET STATE='F1S', [...] WHERE MACHINE_ID='ItemSM'
'E_F2IS' -> UPDATE STATE_MACHINE SET STATE='F2S', [...] WHERE MACHINE_ID='ItemSM'
For the last two events (E_F1IS/E_F2IS) I would have expected the region rows ItemSM#R1 and ItemSM#R2 to get updated to F1S and F2S. Instead the main ItemSM row gets updated twice.
This leads to the problem that the state machine cannot be restored properly. The resulting state machine is loaded with state GROUP but the region states are all in the initial F1I,F2I,F3I states.
Is there a problem with my state machine config or what is the reason for this behavior?
I have the following StateMachine configuration:
And my controller looks like this:
If I trigger the events in the following order the following SQL statements are being executed:
For the last two events (
E_F1IS/E_F2IS
) I would have expected the region rowsItemSM#R1
andItemSM#R2
to get updated toF1S
andF2S
. Instead the mainItemSM
row gets updated twice.This leads to the problem that the state machine cannot be restored properly. The resulting state machine is loaded with state
GROUP
but the region states are all in the initialF1I,F2I,F3I
states.Is there a problem with my state machine config or what is the reason for this behavior?