2017-02-02 129 views
1

我正在测试Spring Statemachine,特别是在应用状态机来管理我的对象状态时,我感到很有趣。Spring Statemachine持久性

我的Statemachine的类型是StateMachine<EpisodeState, EpisodeEvent>

我的业务对象Episode拥有EpisodeState类型的枚举属性(state),该属性应该保存该集的状态机状态。我有一个批处理过程,它将在初始化时获得一个Statemachine的实例。我想遵循的基本流程:

  • 从数据库装载
  • EpisodeState这是在Episode情况下设置的statemachine的当前状态的Episode
  • 发送一个事件给Statemachine。
  • 从Statemachine获得结果状态(post事件),并在我的Episode实例中设置EpisodeState
  • 保存Episode实例。

说明文档中提到的extendedState属性,它在我的测试是空的,但似乎支持地图,我想我可以用它来握住我的Episode的主键任意对象,但我很茫然至于如何将状态机的当前状态设置为Episode中的EpisodeState值。

我已经配置了状态机与StateMachineInterceptorAdapter<EpisodeState, EpisodeEvent>,我可以看到在pre/post stateChange和pre/post过渡时的信息,以及preEvent

回答

2

进一步的研究(不是春的statemachine文档),我找到了一种方法来设置的statemachine的状态:

假设你有希望的开始状态在一个名为startingState变量,你会做这样的:

stateMachine.stop(); 
stateMachine 
    .getStateMachineAccessor() 
    .doWithAllRegions(access -> 
     access.resetStateMachine(new DefaultStateMachineContext<>(startingState, null, null, null))); 
stateMachine.start();