Guide
Concept
- State Machine
状态机,将状态结合、转移、事件整合到一起的机器 - State
一个不变的状态模型
主要由一个事件修改实体的状态 - Extended State
扩展状态是一组特殊的变量保存在一个状态机中,可以保存业务数据。 - Transition
源状态和目标状态之间的关系,它可能是复合过度的一部分,它规定在一个特定的事件发生时某个状态转移的情况。 - Event
事件,驱动状态转移的实体。 - Initial State
初始状态 - End State
终态 - History State
记录状态机最后的活动状态,存在两种模式 - Choice State
状态转移选择,相当于if/else - Fork State
状态分支 - Join State
状态合并 - Region
区域,一个父状态与数个子状态组成的状态区 - guard
状态保护机制,对一个状态转移进行评估,评估值为true允许状态转移,评估值为false禁止转移 - Action
状态转移动作,在转移进行的时候触发的动作
适用情况
下列情况适合使用状态机:
- 应用或者其一部分可以用状态表示。
- 希望将复杂的逻辑拆分,获得更清晰更简单的逻辑。
- 应用遇到并发问题,即异步导致的问题。
初步尝试适用状态机:
- 使用布尔标志和枚举建立状态模型
- 仅使用在应用生命周期内具有意义的状态。
- 使用有限的状态,每种状态或状态的组合都有意义和相应的处理方法。
Example
StateMachineConfigurer
-
StateMachineModel
-
StateMachineConfiguration
-
StateMachineState
-
StateMachineTransition
StateMachine && StateMachineFactory
区别
StateMachine Persit Or Set State
StateMachine Repository
状态机持久化
StateMachineTransitionConfigurer
在初始化配置状态机时,我们必须配置此配置,此配置从表面来看就是状态机进行转换过滤时的配置。
配置时的方向是:
- External
- Internal
- Local
- Choice
- Junction
- Fork
- Join
- Entry
- Exit
- History
/**
* Configurer interface exposing different type of transitions.
*
* @author Janne Valkealahti
*
* @param <S> the type of state
* @param <E> the type of event
*/
public interface StateMachineTransitionConfigurer<S, E> {
/**
* Gets a configurer for external transition.
*
* @return {@link ExternalTransitionConfigurer} for chaining
* @throws Exception if configuration error happens
* @see #withLocal()
*/
ExternalTransitionConfigurer<S, E> withExternal() throws Exception;
/**
* Gets a configurer for internal transition. Internal transition is used
* when action needs to be executed without causing a state transition. With
* internal transition source and target state is always a same and it is
* identical with self-transition in the absence of state entry and exit
* actions.
*
* @return {@link InternalTransitionConfigurer} for chaining
* @throws Exception if configuration error happens
*/
InternalTransitionConfigurer<S, E> withInternal() throws Exception;
/**
* Gets a configurer for local transition. Local transition doesn’t cause
* exit and entry to source state if target state is a substate of a source
* state. Other way around, local transition doesn’t cause exit and entry to
* target state if target is a superstate of a source state.
*
* @return {@link LocalTransitionConfigurer} for chaining
* @throws Exception if configuration error happens
*/
LocalTransitionConfigurer<S, E> withLocal() throws Exception;
/**
* Gets a configurer for transition from a choice pseudostate.
*
* @return {@link ChoiceTransitionConfigurer} for chaining
* @throws Exception if configuration error happens
*/
ChoiceTransitionConfigurer<S, E> withChoice() throws Exception;
/**
* Gets a configurer for transition from a junction pseudostate.
*
* @return {@link JunctionTransitionConfigurer} for chaining
* @throws Exception if configuration error happens
*/
JunctionTransitionConfigurer<S, E> withJunction() throws Exception;
/**
* Gets a configurer for transition from a fork pseudostate.
*
* @return {@link ForkTransitionConfigurer} for chaining
* @throws Exception if configuration error happens
*/
ForkTransitionConfigurer<S, E> withFork() throws Exception;
/**
* Gets a configurer for transition from a join pseudostate.
*
* @return {@link JoinTransitionConfigurer} for chaining
* @throws Exception if configuration error happens
*/
JoinTransitionConfigurer<S, E> withJoin() throws Exception;
/**
* Gets a configurer for transition from an entrypoint pseudostate.
*
* @return {@link EntryTransitionConfigurer} for chaining
* @throws Exception if configuration error happens
*/
EntryTransitionConfigurer<S, E> withEntry() throws Exception;
/**
* Gets a configurer for transition from an exitpoint pseudostate.
*
* @return {@link ExitTransitionConfigurer} for chaining
* @throws Exception if configuration error happens
*/
ExitTransitionConfigurer<S, E> withExit() throws Exception;
/**
* Gets a configurer for default history transition.
*
* @return {@link HistoryTransitionConfigurer} for chaining
* @throws Exception if configuration error happens
*/
HistoryTransitionConfigurer<S, E> withHistory() throws Exception;
}
Using StateMachineRuntimePersister
Link
关于订单的github项目 https://github.com/otrosien/spring-statemachine-jpa-and-rest.git
例子: https://github.com/cjqCN/spring-statemachine-learning.git
二级审批流:https://github.com/kmyhy/statemachine
Spring StateMachine Persit
StateMachine Create By State
public synchronized boolean handleEventWithState(Message<E> event, S state) {
stateMachine.stop();
List<StateMachineAccess<S, E>> withAllRegions = stateMachine.getStateMachineAccessor()
.withAllRegions();
for (StateMachineAccess<S, E> a : withAllRegions) {
a.resetStateMachine(new DefaultStateMachineContext<>(state, null, null, null));
}
stateMachine.start();
return stateMachine.sendEvent(event);
}