问题
I need the same SM to serve various records from the same db-table (can't create SM per each record). Would this be appropriate way to reinitialize SM with new states from another record OR can you advice a better approach please ?
public static <S, E> void reinitStateMachine(Integer key, IStateMachineEnabledService sees, StateMachine<S, E> stateMachine, Class<? extends Enum> statesClazz) {
String dbReadState;
try {
dbReadState = sees.findStateById(key);
} catch (Exception e) {
throw new MissingResourceException("Error while trying to load record with state, no record found: "+key+". Extra info:"+e.getMessage(),sees.toString(),String.valueOf(key));
}
S currentState = (S) Enum.valueOf(statesClazz, dbReadState);
stateMachine.stop();
((AbstractStateMachine<S, E>) stateMachine).resetStateMachine(new DefaultStateMachineContext<S, E>(currentState, null, null, null));
stateMachine.start();
}
Thanks !
PS: I am aware of persist and restore interfaces in 1.1.0, but persisting SMContext works only for string state machine, while i use enum.
来源:https://stackoverflow.com/questions/38262665/the-right-way-to-reload-and-restart-state-machine-with-different-data-set