Closed
Description
I am developing a game using spring statemachine, and want to restore the state machine from the db record;
I have checked the similar question from The right way to reload and restart State Machine with different data set
But it seems the timer transition is not trigger when reset. Below is My code.
There are three state, the transition is timer base.
Ready(1 second) -> A (1 second)-> B.
My Question is:
We expected when restore the state machine with state A, it will transit to the state B after one second, but it kept in the state A all the time.
public enum TestState {
READY, A, B;
}
public void testRestore() throws Exception {
StateMachineBuilder.Builder<TestState, Event> builder = StateMachineBuilder.builder();
// @formatter:off
builder.configureConfiguration()
.withVerifier().enabled(true)
.and()
.withConfiguration()
.taskScheduler(threadPoolTaskScheduler)
.taskExecutor(threadPoolTaskExecutor)
.beanFactory(applicationContext);
builder.configureStates()
.withStates()
.initial(TestState.READY)
.states(new HashSet<>(Arrays.asList(TestState.values())));
builder.configureTransitions()
.withExternal()
.source(TestState.READY).target(TestState.A)
.timerOnce(1000)
.and()
.withExternal()
.source(TestState.A).target(TestState.B)
.timerOnce(1000);
// @formatter:on
StateMachine<TestState, Event> stateMachine = builder.build();
stateMachine.addStateListener(new StateMachineListenerAdapter<TestState, Event>() {
@Override
public void stateChanged(org.springframework.statemachine.state.State<TestState, Event> from,
org.springframework.statemachine.state.State<TestState, Event> to) {
log.info("[Testcmd=stateChanged,from={}, to={}]", from.getId(), to.getId());
}
});
stateMachine.stop();
stateMachine.getStateMachineAccessor()
.doWithAllRegions(new StateMachineFunction<StateMachineAccess<TestState, Event>>() {
@Override
public void apply(StateMachineAccess<TestState, Event> function) {
function.resetStateMachine(
new DefaultStateMachineContext<TestState, Event>(TestState.A, null, null, null));
}
});
stateMachine.start();
Thread.sleep(1000);
log.info("Test, Current State is {}",stateMachine.getState().getId());
synchronized (this) {
wait();
}
}