我需要同一个 SM 来为同一个数据库表中的各种记录提供服务(不能为每条记录创建 SM)。这是否是用另一条记录中的新状态重新初始化 SM 的合适方法,或者您能建议一个更好的方法吗?
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();
}
谢谢 !
PS:我知道 1.1.0 中的持久化和恢复接口,但持久化 SMContext 仅适用于字符串状态机,而我使用枚举。