I have a spring statemachine (version 3.0.1) with an own instance for each user. With every HTTP request the statemachine is acquired by a DefaultStateMachineService
from a JpaStateMachineRepository
. At the end of the HTTP request the statemachine is released again. The state machine is configured to use a StateMachineRuntimePersister
.
At one special state of the statemachine there is a transition triggered by a timer:
.withExternal()
.source( "S1" )
.target( "S2" )
.timerOnce( 60000 )
I want this timer to be executed only if the statemachine is still in state S1
after one minute (another cloud instance handling a later step of this statemachine instance could have changed the state in the meantime, making the timer obsolete).
- I tried to release the statemachine without stopping it (
stateMachineService.releaseStateMachine( machineId, false );
). Then the timer always triggers after one minute, because it does not refresh the statemachine from the database leading to an inconsistent state. - I tried to release the statemachine stopped. Then the timer is killed and will not trigger anymore.
One solution could be to switch the trigger from timerOnce
to event
and to use a separate mechanism (e.g. Quartz scheduler) that reads the statemachine from database, checks the state and then explicitly fires the event.
But this would make the timer feature of spring statemachine useless in a cloud environment, so I guess there should be a better solution.
What would be the best solution here?