1

在上一章的例子中,我们实现了多个状态机并存执行,不同的订单有各自的状态机运行,但只有一种状态机,这显然不能满足实际业务的要求,比如我就遇到了订单流程和公文审批流程在同一个项目的情况,所以我们这一章讲怎么让多种状态机共存。

我们先把上一章的例子状态机再复习一下,这是个订单状态机,流程图如下:

![](https://oscimg.oschina.net/oscnet/e60cfa4b1956ed1863632b34b7f0d7a60ff.jpg)

定义这个状态机我们用到了OrderEvents,OrderStates来表达状态(states)和事件(events),用OrderStateMachineBuilder来描述初始状态和状态变化流程,用OrderEventConfig来描述这个流程和状态变化过程中需要做的业务。

现在我们再弄一个新的状态机流程,表单状态机,流程图如下:

为此,我们同样配套了和订单状态机一样的表单四件套,events,states,StateMachineBuilder和eventConfig。

public enum FormStates {

BLANK_FORM, // 空白表单
FULL_FORM, // 填写完表单
CONFIRM_FORM, // 校验表单
SUCCESS_FORM// 成功表单

}

public enum FormEvents {

WRITE, // 填写
CONFIRM, // 校验
SUBMIT // 提交

}

import java.util.EnumSet;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.stereotype.Component;

/**

  • 订单状态机构建器

*/
@Component
public class FormStateMachineBuilder {

private final static String MACHINEID = "formMachine";

 /**
  * 构建状态机
  * 
 * @param beanFactory
 * @return
 * @throws Exception
 */
public StateMachine<FormStates, FormEvents> build(BeanFactory beanFactory) throws Exception {
     StateMachineBuilder.Builder<FormStates, FormEvents> builder = StateMachineBuilder.builder();

     System.out.println("构建表单状态机");

     builder.configureConfiguration()
             .withConfiguration()
             .machineId(MACHINEID)
             .beanFactory(beanFactory);

     builder.configureStates()
                 .withStates()
                 .initial(FormStates.BLANK_FORM)
                 .states(EnumSet.allOf(FormStates.class));

     builder.configureTransitions()
                 .withExternal()
                    .source(FormStates.BLANK_FORM).target(FormStates.FULL_FORM)
                    .event(FormEvents.WRITE)
                    .and()
                .withExternal()
                    .source(FormStates.FULL_FORM).target(FormStates.CONFIRM_FORM)
                    .event(FormEvents.CONFIRM)
                    .and()
                .withExternal()
                    .source(FormStates.CONFIRM_FORM).target(FormStates.SUCCESS_FORM)
                    .event(FormEvents.SUBMIT);

     return builder.build();
 }

}

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;

@WithStateMachine(id="formMachine")
public class FormEventConfig {
private Logger logger = LoggerFactory.getLogger(getClass());

/**
 * 当前状态BLANK_FORM
 */
@OnTransition(target = "BLANK_FORM")
public void create() {
    logger.info("---空白表单---");
}

/**
 * BLANK_FORM->FULL_FORM 执行的动作
 */
@OnTransition(source = "BLANK_FORM", target = "FULL_FORM")
public void write(Message<FormEvents> message) {
    logger.info("---填写完表单---");
}

/**
 * FULL_FORM->CONFIRM_FORM 执行的动作
 */
@OnTransition(source = "FULL_FORM", target = "CONFIRM_FORM")
public void confirm(Message<FormEvents> message) {
    logger.info("---校验表单---");
}

/**
 * CONFIRM_FORM->SUCCESS_FORM 执行的动作
 */
@OnTransition(source = "CONFIRM_FORM", target = "SUCCESS_FORM")
public void submit(Message<FormEvents> message) {
    logger.info("---表单提交成功---");
}

}
从代码可以看到深深的套路感,里面除了对流程状态的描述不同外,另外一个不同点就是MACHINEID,在不同的状态机流程中,用MACHINEID来标识不同就能使用多种状态机了,对比一下就很清楚。在builder里面通过MACHINEID来区分

private final static String MACHINEID = "orderMachine";

public StateMachine<OrderStates, OrderEvents> build(BeanFactory beanFactory) throws Exception {
     StateMachineBuilder.Builder<OrderStates, OrderEvents> builder = StateMachineBuilder.builder();

     System.out.println("构建订单状态机");

     builder.configureConfiguration()
             .withConfiguration()
             .machineId(MACHINEID)
             .beanFactory(beanFactory);

...

private final static String MACHINEID = "formMachine";

public StateMachine<FormStates, FormEvents> build(BeanFactory beanFactory) throws Exception {
     StateMachineBuilder.Builder<FormStates, FormEvents> builder = StateMachineBuilder.builder();

     System.out.println("构建表单状态机");

     builder.configureConfiguration()
             .withConfiguration()
             .machineId(MACHINEID)
             .beanFactory(beanFactory);

...
对应的在eventconfig里面

@WithStateMachine(id="orderMachine")
public class OrderEventConfig {
...

@WithStateMachine(id="formMachine")
public class FormEventConfig {
通过@WithStateMachine注解的id参数就区分出来了不同的状态机,这个id就是builder里面定义的MACHINEID。然后就是怎么引用的问题了,我们来看controller

@Autowired
private OrderStateMachineBuilder orderStateMachineBuilder;

@Autowired
private FormStateMachineBuilder formStateMachineBuilder;

这样,不同的builder就能同时引用,两种状态机就互不干扰的各自运行了,这是运行的代码:

@RequestMapping("/testOrderState")

public void testOrderState(String orderId) throws Exception {

    StateMachine<OrderStates, OrderEvents> stateMachine = orderStateMachineBuilder.build(beanFactory);
    System.out.println(stateMachine.getId());

    // 创建流程
    stateMachine.start();

    // 触发PAY事件
    stateMachine.sendEvent(OrderEvents.PAY);

    // 触发RECEIVE事件
    stateMachine.sendEvent(OrderEvents.RECEIVE);

    // 获取最终状态
    System.out.println("最终状态:" + stateMachine.getState().getId());
}

@RequestMapping("/testFormState")
public void testFormState() throws Exception {

    StateMachine<FormStates, FormEvents> stateMachine = formStateMachineBuilder.build(beanFactory);
    System.out.println(stateMachine.getId());

    // 创建流程
    stateMachine.start();

    stateMachine.sendEvent(FormEvents.WRITE);

    stateMachine.sendEvent(FormEvents.CONFIRM);

    stateMachine.sendEvent(FormEvents.SUBMIT);

    // 获取最终状态
    System.out.println("最终状态:" + stateMachine.getState().getId());
}

分别执行

http://localhost:9991/statemachine/testOrderState 使用StateMachineBuilder创建的多个状态机演示
http://localhost:9991/statemachine/testFormState 多种状态机的演示(上面都是order的状态机,这个是form的状态机)

在日志里面就能看到各自状态机的运行结果了。

目前为止,多个状态机和多种状态机都可以在spring statemachine里面实现了,下一章我们来解决下状态机和实际业务间的数据传输问题,毕竟我们不是为了让状态机自个独自玩耍,和业务数据互通有无才是企业开发的正道。

码云配套代码地址


弯月残照飞檐
31 声望9 粉丝

淡云散天几丝闲