Ste*_* H. 2 spring-statemachine
我正在测试 Spring Statemachine,特别是我对应用状态机来管理我的对象的状态很感兴趣。
我的状态机类型为StateMachine<EpisodeState, EpisodeEvent>。
我的业务对象Episode具有类型为 的枚举属性 ( state) EpisodeState,它应该保存剧集的状态机状态。我有一个批处理,它将在初始化时获取一个 Statemachine 的实例。我想遵循基本流程:
Episode从数据库加载一个EpisodeState即在该Episode实例。EpisodeState在我的Episode实例中设置。Episode实例。文档提到了一个extendedState属性,在我的测试中它是空的,但似乎支持任意对象的映射,我想我可以用它来保存我的主键Episode,但我不知道如何设置当前状态状态机中的EpisodeState值Episode。
我已经用 配置了状态机StateMachineInterceptorAdapter<EpisodeState, EpisodeEvent>,我可以在 pre/post stateChange 和 pre/post Transition 以及preEvent.
进一步研究(不在 Spring Statemachine 文档中),我找到了一种设置状态机状态的方法:
假设您在名为 的变量中有所需的开始状态startingState,您可以这样做:
stateMachine.stop();
stateMachine
.getStateMachineAccessor()
.doWithAllRegions(access ->
access.resetStateMachine(new DefaultStateMachineContext<>(startingState, null, null, null)));
stateMachine.start();
Run Code Online (Sandbox Code Playgroud)