龙空技术网

Spring Statemachine状态机接入说明

七星院长 1550

前言:

眼前我们对“java状态机模式”大约比较关心,小伙伴们都需要学习一些“java状态机模式”的相关资讯。那么小编也在网摘上收集了一些对于“java状态机模式””的相关知识,希望看官们能喜欢,朋友们快快来学习一下吧!

1.引入pom

 <dependency>     <groupId>org.springframework.statemachine</groupId>     <artifactId>spring-statemachine-starter</artifactId>     <version>2.5.0</version> </dependency>
2.新建状态和事件枚举
public enum States {    STATE1, STATE2, STATE3, STATE4, STATE5}public enum Events {    EVENT1, EVENT2, EVENT3, EVENT4, EVENT5}

3.新建订单状态机的构建组件(编程式)

@Slf4j@Componentpublic class OrderMachineBuilder {    private final static String MACHINE_ID = "orderStateMachine";    /**     * 编程式     *     * @return     * @throws Exception     */    public StateMachine<States, Events> buildMachine(BeanFactory beanFactory, States presentStates) throws Exception {        StateMachineBuilder.Builder<States, Events> builder = StateMachineBuilder.builder();        // 配置状态机监听器,可监听多种事件        builder.configureConfiguration()                .withConfiguration()                .machineId(MACHINE_ID)                .beanFactory(beanFactory);        // 设置状态机初始状态为STATE1、共有EnumSet.allOf(States.class)种状态        builder.configureStates()                .withStates()                .initial(presentStates) // 初始状态(TODO 当前状态从持久化介质中实时获取)                .states(EnumSet.allOf(States.class)); // 指定状态集        // 设置状态转移 原始状态STATE1触发EVENT1事件状态转移为STATE2、原始状态STATE2触发EVENT2事件状态转移为STATE1        builder.configureTransitions()                .withExternal()                .source(States.STATE1).target(States.STATE2)                .event(Events.EVENT1).action(action())                .and()                .withExternal()                .source(States.STATE2).target(States.STATE1)                .event(Events.EVENT2)                .and()                .withExternal()                .source(States.STATE1).target(States.STATE3)                .event(Events.EVENT3);        return builder.build();    }    @Bean    public Action<States, Events> action() {        return (stateContext) -> log.info("action:{}", stateContext);    }}

4.新建订单状态机的事件监听

@Slf4j@WithStateMachine(id = "orderStateMachine")public class OrderEventListener {    /**     * 拒绝     *     * @param message 消息(事件、上下文)     */    @OnTransition(source = "STATE2", target = "STATE1")    public void reject(Message<Events> message) {        log.info("传递的参数:" + message);        if (null != message) {            log.info("传递的参数:" + message.getHeaders().get("order"));        }        log.info("审批拒绝");    }    /**     * 同意     *     * @param message 消息(事件、上下文)     */    @OnTransition(source = "STATE1", target = "STATE2")    public void agree(Message<Events> message) {        log.info("传递的参数:" + message);        if (null != message) {            log.info("传递的参数:" + message.getHeaders().get("order"));        }        log.info("审批通过");    }}
5.使用
    @Autowired    private BeanFactory beanFactory;    @Autowired    private ProgrammingMachineBuilder programmingMachineBuilder;    @Test    public void testDemo2() throws Exception {        // 构建状态机对象        StateMachine<States, Events> stateMachine = programmingMachineBuilder.buildMachine(beanFactory,States.STATE1);        // 启动        stateMachine.start();        // 第一种写法        stateMachine.sendEvent(Events.EVENT1);        // 第二种写法        OrderMainDO order = new OrderMainDO();        order.setOrderNo("O123");        Message<Events> message = MessageBuilder.withPayload(Events.EVENT1)                .setHeader("order", order).build();        stateMachine.sendEvent(message);    }

标签: #java状态机模式