先从状态机的定义入手,StateMachine,其中:
状态机的相关概念
每次用到的时候新创建一个状态机,太奢侈了,官方文档里面也提到过这点。而且创建出来的实例,其状态也跟当前订单的不符;spring statemachine暂时不支持每次创建时指定当前状态,所以对状态机引擎实例的持久化,就成了必须要考虑的问题。
spring statemachine 本身支持了内存、redis及db的持久化,内存持久化就不说了,看源码实现就是放在了hashmap里,平时也没谁项目中可以这么奢侈,啥啥都放在内存中,而且一旦重启…(嘿嘿嘿)。下面详细说下利用redis进行的持久化操作。
spring statemachine持久化时,采用了三层结构设计,persister —>persist —>repository。
接口
public interface StateMachinePersister<S, E, T> {
void persist(StateMachine<S, E> var1, T var2) throws Exception;
StateMachine<S, E> restore(StateMachine<S, E> var1, T var2) throws Exception;
}
接口点进去其抽象类的实现,可以看到使用的是StateMachinePersist的read与write方法
public abstract class AbstractStateMachinePersister<S, E, T> implements StateMachinePersister<S, E, T> {
private final StateMachinePersist<S, E, T> stateMachinePersist;
public final void persist(StateMachine<S, E> stateMachine, T contextObj) throws Exception {
this.stateMachinePersist.write(this.buildStateMachineContext(stateMachine), contextObj);
}
public final StateMachine<S, E> restore(StateMachine<S, E> stateMachine, T contextObj) throws Exception {
final StateMachineContext<S, E> context = this.stateMachinePersist.read(contextObj);
stateMachine.stop();
stateMachine.getStateMachineAccessor().doWithAllRegions(new StateMachineFunction<StateMachineAccess<S, E>>() {
public void apply(StateMachineAccess<S, E> function) {
function.resetStateMachine(context);
}
});
stateMachine.start();
return stateMachine;
}
}
public interface StateMachinePersist<S, E, T> {
void write(StateMachineContext<S, E> var1, T var2) throws Exception;
StateMachineContext<S, E> read(T var1) throws Exception;
}
自定义实现
@Component
public class OrderStateMachinePersist implements StateMachinePersist<OrderStatusEnum, OrderChangeEventEnum, PersisterDO> {
@Autowired
private TbUserOrderMapper userOrderMapper;
//将订单状态写入数据库
@Override
public void write(StateMachineContext<OrderStatusEnum, OrderChangeEventEnum> stateMachineContext, PersisterDO persisterDO) throws Exception {
String orderNumber = persisterDO.getOrderNumber();
if(orderNumber == null){
throw new RuntimeException("orderNumber 为空");
}
QueryWrapper<TbUserOrder> wrapper = new QueryWrapper<>();
wrapper.eq("order_number",orderNumber);
TbUserOrder order = userOrderMapper.selectOne(wrapper);
if(order != null){
order.setOrderState(stateMachineContext.getState().name());
order.setUpdateTime(new Date());
userOrderMapper.updateById(order);
}
}
@Override
public StateMachineContext<OrderStatusEnum, OrderChangeEventEnum> read(PersisterDO persisterDO) throws Exception {
//将数据从数据库中读出,然后将订单的状态设置给参数orderStatusEnum
OrderStatusEnum orderStatusEnum;
QueryWrapper<TbUserOrder> wrapper = new QueryWrapper<>();
wrapper.eq("order_number",persisterDO.getOrderNumber());
TbUserOrder order = userOrderMapper.selectOne(wrapper);
if(order != null){
orderStatusEnum = OrderStatusEnum.valueOf(order.getOrderState());
}else{
orderStatusEnum = OrderStatusEnum.CREATE;
}
return new DefaultStateMachineContext<>(orderStatusEnum, null, null, null, null, persisterDO.getMachineId());
}
}
状态枚举就是当前订单所处的状态,事件会导致订单的状态发生改变(但是也不一定,有些是内部事件,并不会导致状态发生变化)
public enum OrderStatusEnum {
CREATE("新建"),
WAIT_PAYMENT("待付款"),
WAIT_SEND("待发货"),
WAIT_RECEIVE("待收货"),
COMPLETED("已完成"),
CANCEL("取消");
private final String name;
}
public enum OrderChangeEventEnum {
CREATE_ORDER("创建订单"),
PAY_ORDER("支付订单"),
PAY_CANCEL("取消支付"),
SEND_GOODS("发货"),
RECEIVE_GOODS("收货"),
;
private final String name;
}
状态机的配置有两种方式
自定义一个Builder接口,用来规范不同业务状态机的配置
public interface IStateMachineBuilder<S, E> {
String getName();
StateMachine<S, E> build(BeanFactory beanFactory) throws Exception;
//订单状态机构造器
String ORDER_BUILDER_NAME = "orderStateMachineBuilder";
}
设置对应业务的Builder,外部调用的使用直接使用build()方法就行
这个里面的Guard就可以相当于一个条件,如果不满足Guard(即返回false)那么就不会执行接下来的action
Action就相当于一个执行的过程,其中errorHandlerAction就是对异常的处理,其中比较重要的是
public class OrderStateMachineBuilder implements IStateMachineBuilder<OrderStatusEnum, OrderChangeEventEnum> {
@Autowired
private Guard<OrderStatusEnum, OrderChangeEventEnum> orderCreateGuard;
@Autowired
private Action<OrderStatusEnum, OrderChangeEventEnum> errorHandlerAction;
@Autowired
private Action<OrderStatusEnum, OrderChangeEventEnum> orderCreateAction;
@Override
public StateMachine<OrderStatusEnum, OrderChangeEventEnum> build(BeanFactory beanFactory) throws Exception {
StateMachineBuilder.Builder<OrderStatusEnum, OrderChangeEventEnum> builder = StateMachineBuilder.builder();
//设置id
builder.configureConfiguration()
.withConfiguration()
.autoStartup(true)
.beanFactory(beanFactory)
.machineId(ORDER_BUILDER_NAME + "Id");
//初始化状态机,并指定状态集合
builder.configureStates()
.withStates()
//初始状态
.initial(OrderStatusEnum.CREATE)
.end(OrderStatusEnum.COMPLETED)
.states(EnumSet.allOf(OrderStatusEnum.class));
//定义状态机节点,即迁移动作
builder.configureTransitions()
.withExternal()
.source(OrderStatusEnum.CREATE)
.target(OrderStatusEnum.WAIT_PAYMENT)
.event(OrderChangeEventEnum.CREATE_ORDER)
.guard(orderCreateGuard)
.action(orderCreateAction, errorHandlerAction)
...这可以使用and()继续写
;
return builder.build();
}
@Override
public String getName() {
return ORDER_BUILDER_NAME;
}
}
在实际项目中一般都会有多个状态机并发执行,比如订单,同一时刻会有不止一个订单在运行,而每个订单都有自己的订单状态机流程。所以如果使用配置类的话就只有一个状态机,所以需要使用Builder,同时因为可以会有多种类型的状态机,所以定义了一个接口,后续类型的状态机只要实现这个状态机接口就可以开发
@Component
public class StateMachineBuildFactory<S, E> implements ApplicationContextAware {
@Autowired
private BeanFactory beanFactory;
/**
* 用来存储builder-name及builder的map
*/
public static final Map<String, IStateMachineBuilder> builderMap = new ConcurrentHashMap<>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
String[] beanNamesForType = applicationContext.getBeanNamesForType(IStateMachineBuilder.class);
for (String beanName : beanNamesForType) {
IStateMachineBuilder bean = (IStateMachineBuilder) applicationContext.getBean(beanName);
builderMap.put(bean.getName(), bean);
}
}
public StateMachine<S, E> createStateMachine(String machineType) throws Exception{
if (StringUtils.isBlank(org.apache.commons.lang3.StringUtils.trim(machineType))) {
throw new RuntimeException("无效的状态机类型");
}
IStateMachineBuilder builder = builderMap.get(machineType);
StateMachine<S, E> stateMachine;
try {
stateMachine = builder.build(beanFactory);
} catch (Exception e) {
throw new RuntimeException("创建状态机异常");
}
return stateMachine;
}
}
外部调用状态机需要以下三步
需要注意的是当发送完message之后,spring statemachine才会通过监听器来监听走那个action,只有在message完成之后才会更新为target状态
所以为了更加方便管理使用,所以做了以下代码封装
这规定了这个Manager必须实现的方法,就是来发送message
public interface IStateMachineEventManager<E>{
/**
* 状态发生改变变更事件
* @param statusModelDO
* @param eventEnum
* @param <R>
* @return
*/
<R> R sendStatusChangeEvent(StatusModelDO statusModelDO, E eventEnum);
}
这个里面主要实现初始化状态机,构造message与发送message
public abstract class AbstractStateMachineEventManager<S, E> implements IStateMachineEventManager<E> {
@Autowired
private StateMachineBuildFactory<S, E> stateMachineBuildFactory;
//用于状态机上下文持久化
public abstract void stateMachinePersist(StateMachine<S, E> stateMachine, PersisterDO persisterDO) throws Exception;
//用于状态机上下文初始化
public abstract StateMachine<S, E> stateMachineRestore(StateMachine<S, E> stateMachine, PersisterDO persisterDO) throws Exception;
@Override
@SuppressWarnings("unchecked")
public <R> R sendStatusChangeEvent(StatusModelDO statusModelDO, E eventEnum) {
StateMachine<S, E> stateMachine = initializeMachine(statusModelDO);
Boolean result = statusChangeExecute(stateMachine, statusModelDO, eventEnum);
if (!result) {
throw new RuntimeException("状态机状态执行失败");
}
RuntimeException exception = (RuntimeException) stateMachine.getExtendedState().getVariables().get(RuntimeException.class);
if (exception != null) {
}
try {
PersisterDO persisterDO = new PersisterDO();
persisterDO.setOrderNumber(statusModelDO.getOrderNumber());
stateMachinePersist(stateMachine, persisterDO);
} catch (Exception e) {
throw new RuntimeException("状态机持久化失败");
}
return (R) stateMachine.getExtendedState().getVariables().get(StateMachineConstants.RETURN_PARAM);
}
private StateMachine<S, E> initializeMachine(StatusModelDO statusModelDO) {
StateMachine<S, E> stateMachine;
try {
StateMachine<S, E> srcStateMachine = stateMachineBuildFactory.createStateMachine(getStateMachineType());
PersisterDO persisterDO = new PersisterDO();
persisterDO.setOrderNumber(statusModelDO.getOrderNumber());
persisterDO.setCurrentState(statusModelDO.getCurrentState());
stateMachine = stateMachineRestore(srcStateMachine, persisterDO);
} catch (Exception e) {
throw new RuntimeException("初始化状态机失败");
}
if (stateMachine == null) {
throw new RuntimeException("没有找到可用的状态机");
}
return stateMachine;
}
private boolean statusChangeExecute(StateMachine<S, E> stateMachine, StatusModelDO statusModelDO, E eventEnum) {
Message<E> eventMsg = MessageBuilder.withPayload(eventEnum)
.setHeader(StateMachineConstants.STATE_MODEL_DTO, statusModelDO).build();
if (!acceptEvent(stateMachine, eventMsg)) {
throw new RuntimeException("找不到对应状态机事件触发定义");
}
return stateMachine.sendEvent(eventMsg);
}
private static <S, E> boolean acceptEvent(StateMachine<S, E> stateMachine, Message<E> eventMsg) {
//获取当前状态
State<S, E> currentState = stateMachine.getState();
for (Transition<S, E> transition : stateMachine.getTransitions()) {
State<S, E> source = transition.getSource();
Trigger<S, E> trigger = transition.getTrigger();
if (currentState != null && trigger != null &&
StateMachineUtils.containsAtleastOne(source.getIds(), currentState.getIds()) &&
trigger.evaluate(new DefaultTriggerContext<>(eventMsg.getPayload()))) {
return true;
}
}
return false;
}
public abstract String getStateMachineType();
}
这里主要实现的是上下文的初始化与持久化,这个就可以直接调用状态机的持久化里面的read与write就行
@Component
public class OrderStateMachineEventManager extends AbstractStateMachineEventManager<OrderStatusEnum, OrderChangeEventEnum> {
@Resource(name = "orderStateMachinePersister")
private StateMachinePersister<OrderStatusEnum, OrderChangeEventEnum, PersisterDO> stateMachinePersister;
@Override
public void stateMachinePersist(StateMachine<OrderStatusEnum, OrderChangeEventEnum> stateMachine,
PersisterDO persisterDO) throws Exception {
stateMachinePersister.persist(stateMachine, persisterDO);
}
@Override
public StateMachine<OrderStatusEnum, OrderChangeEventEnum> stateMachineRestore(
StateMachine<OrderStatusEnum, OrderChangeEventEnum> stateMachine,
PersisterDO persisterDO) throws Exception {
return stateMachinePersister.restore(stateMachine, persisterDO);
}
@Override
public String getStateMachineType() {
return IStateMachineBuilder.ORDER_BUILDER_NAME;
}
}